Skip to main content

cratestack_axum/rpc/
synthesize.rs

1//! Reverse mapping: `RpcListInput` / `RpcGetInput` → URL query string
2//! for the existing list/fetch handlers' parsers.
3
4use cratestack_core::rpc::{RpcGetInput, 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    if let Some(computed_params) = &input.computed_params {
40        pairs.push(("computedParams".to_owned(), computed_params.clone()));
41    }
42
43    if pairs.is_empty() {
44        return None;
45    }
46
47    Some(
48        url::form_urlencoded::Serializer::new(String::new())
49            .extend_pairs(pairs.iter().map(|(k, v)| (k.as_str(), v.as_str())))
50            .finish(),
51    )
52}
53
54/// Synthesize a URL query string from an [`RpcGetInput`] in exactly the
55/// shape the macro-generated `parse_model_fetch_query` parses — the
56/// direct `get` counterpart to [`synthesize_list_query`], so RPC `get`
57/// and REST `GET /<plural>/{id}` run one and the same validation and
58/// projection path. Returns `None` when no field is set.
59pub fn synthesize_get_query<Pk>(input: &RpcGetInput<Pk>) -> Option<String> {
60    let mut pairs: Vec<(String, String)> = Vec::new();
61    if let Some(fields) = &input.fields {
62        pairs.push(("fields".to_owned(), fields.join(",")));
63    }
64    if let Some(include) = &input.include {
65        pairs.push(("include".to_owned(), include.join(",")));
66    }
67    for (relation, fields) in &input.include_fields {
68        pairs.push((format!("includeFields[{relation}]"), fields.join(",")));
69    }
70    if let Some(computed_params) = &input.computed_params {
71        pairs.push(("computedParams".to_owned(), computed_params.clone()));
72    }
73
74    if pairs.is_empty() {
75        return None;
76    }
77
78    Some(
79        url::form_urlencoded::Serializer::new(String::new())
80            .extend_pairs(pairs.iter().map(|(k, v)| (k.as_str(), v.as_str())))
81            .finish(),
82    )
83}