Skip to main content

cratestack_axum/rpc/
synthesize.rs

1//! Reverse mapping: `RpcListInput` → URL query string for the existing
2//! list handler's parser.
3
4use super::inputs::RpcListInput;
5
6/// Synthesize a URL query string from an [`RpcListInput`] in exactly the
7/// shape the macro-generated `parse_model_list_query` parses. Returns
8/// `None` when the input has no fields set (the existing handler treats a
9/// missing query the same as an empty one — no point allocating).
10pub fn synthesize_list_query(input: &RpcListInput) -> Option<String> {
11    let mut pairs: Vec<(String, String)> = Vec::new();
12    if let Some(limit) = input.limit {
13        pairs.push(("limit".to_owned(), limit.to_string()));
14    }
15    if let Some(offset) = input.offset {
16        pairs.push(("offset".to_owned(), offset.to_string()));
17    }
18    if let Some(fields) = &input.fields {
19        pairs.push(("fields".to_owned(), fields.join(",")));
20    }
21    if let Some(include) = &input.include {
22        pairs.push(("include".to_owned(), include.join(",")));
23    }
24    for (relation, fields) in &input.include_fields {
25        pairs.push((format!("includeFields[{relation}]"), fields.join(",")));
26    }
27    if let Some(sort) = &input.sort {
28        pairs.push(("sort".to_owned(), sort.clone()));
29    }
30    if let Some(where_expr) = &input.where_expr {
31        pairs.push(("where".to_owned(), where_expr.clone()));
32    }
33    if let Some(or) = &input.or {
34        pairs.push(("or".to_owned(), or.clone()));
35    }
36    for predicate in &input.filters {
37        pairs.push((predicate.key.clone(), predicate.value.clone()));
38    }
39
40    if pairs.is_empty() {
41        return None;
42    }
43
44    Some(
45        url::form_urlencoded::Serializer::new(String::new())
46            .extend_pairs(pairs.iter().map(|(k, v)| (k.as_str(), v.as_str())))
47            .finish(),
48    )
49}