Skip to main content

contentstack_api_client_rs/client/
params.rs

1use std::collections::HashMap;
2
3use serde::Serialize;
4use serde_json::Value;
5
6/// A JSON query filter map - keys are field UIDs, values are match conditions.
7///
8/// Contentstack expects this serialized as a JSON string in the `query` param.
9///
10/// # Example
11///
12/// ```
13/// use contentstack_api_client_rs::Query;
14/// use serde_json::json;
15///
16/// let mut q = Query::new();
17/// q.insert("title".into(), json!("Hello World"));       // equals
18/// q.insert("price".into(), json!({ "$gt": 100 }));      // greater than
19/// q.insert("status".into(), json!({ "$in": ["a","b"] })); // in array
20/// ```
21pub type Query = HashMap<String, Value>;
22
23/// Query parameters for fetching multiple entries.
24pub struct GetManyParams {
25    /// JSON query filter - serialized internally, no need to stringify manually.
26    pub query: Option<Query>,
27    /// Maximum number of entries to return.
28    pub limit: Option<u32>,
29    /// Number of entries to skip (for pagination).
30    pub skip: Option<u32>,
31    /// When `true`, includes the total entry count in the response.
32    pub include_count: Option<bool>,
33    /// Locale code to fetch entries for (e.g. `"en-us"`).
34    pub locale: Option<String>,
35}
36
37#[derive(Serialize)]
38pub(crate) struct SerializedGetManyParams {
39    pub query: Option<String>,
40    pub limit: Option<u32>,
41    pub skip: Option<u32>,
42    pub include_count: Option<bool>,
43    pub locale: Option<String>,
44}
45
46impl From<GetManyParams> for SerializedGetManyParams {
47    fn from(p: GetManyParams) -> Self {
48        Self {
49            query: p
50                .query
51                .as_ref()
52                .map(|q| serde_json::to_string(q).expect("Failed to serialize query to JSON")),
53            limit: p.limit,
54            skip: p.skip,
55            include_count: p.include_count,
56            locale: p.locale,
57        }
58    }
59}
60
61/// Query parameters for fetching a single entry by UID.
62pub struct GetOneParams {
63    /// JSON query filter - serialized internally, no need to stringify manually.
64    pub query: Option<Query>,
65    /// Locale code to fetch the entry for (e.g. `"en-us"`).
66    pub locale: Option<String>,
67}
68
69#[derive(Serialize)]
70pub(crate) struct SerializedGetOneParams {
71    pub query: Option<String>,
72    pub locale: Option<String>,
73}
74
75impl From<GetOneParams> for SerializedGetOneParams {
76    fn from(p: GetOneParams) -> Self {
77        Self {
78            query: p
79                .query
80                .as_ref()
81                .map(|q| serde_json::to_string(q).expect("Failed to serialize query to JSON")),
82            locale: p.locale,
83        }
84    }
85}