cognite/dto/
params.rs

1use std::fmt::Display;
2
3use crate::Partition;
4
5/// Trait for query parameters.
6pub trait IntoParams {
7    /// Convert self to a list of query parameter tuples.
8    fn into_params(self) -> Vec<(String, String)>;
9}
10
11impl IntoParams for Vec<(String, String)> {
12    fn into_params(self) -> Vec<(String, String)> {
13        self
14    }
15}
16
17/// Push the item given in `item` to the query with name `name` if it is Some.
18pub fn to_query<T>(name: &str, item: &Option<T>, params: &mut Vec<(String, String)>)
19where
20    T: Display,
21{
22    if let Some(it) = item {
23        params.push((name.to_string(), it.to_string()));
24    }
25}
26
27/// Push a list of items to the query with name `name` if `item` is `Some`.
28pub fn to_query_vec(name: &str, item: &Option<Vec<String>>, params: &mut Vec<(String, String)>) {
29    if let Some(it) = item {
30        params.push((
31            name.to_string(),
32            format!(
33                "[{}]",
34                it.iter()
35                    .map(|val| { format!("\"{val}\"") })
36                    .collect::<Vec<String>>()
37                    .join(", ")
38            ),
39        ));
40    }
41}
42
43/// Push a list of numbers to the query with name `name` if `item` is `Some`.
44pub fn to_query_vec_i64(name: &str, item: &Option<Vec<i64>>, params: &mut Vec<(String, String)>) {
45    if let Some(it) = item {
46        params.push((
47            name.to_string(),
48            format!(
49                "[{}]",
50                it.iter()
51                    .map(|val| { format!("{val}") })
52                    .collect::<Vec<String>>()
53                    .join(", ")
54            ),
55        ))
56    }
57}
58
59/// Simple query with limit and cursor.
60#[derive(Debug, Default, Clone)]
61pub struct LimitCursorQuery {
62    /// Maximum number of results to return.
63    pub limit: Option<i32>,
64    /// Cursor for pagination.
65    pub cursor: Option<String>,
66}
67
68impl IntoParams for LimitCursorQuery {
69    fn into_params(self) -> Vec<(String, String)> {
70        let mut params = Vec::<(String, String)>::new();
71        to_query("limit", &self.limit, &mut params);
72        to_query("cursor", &self.cursor, &mut params);
73        params
74    }
75}
76
77/// Query with limt, cursor, and partition.
78#[derive(Debug, Default, Clone)]
79pub struct LimitCursorPartitionQuery {
80    /// Maximum number of results to return.
81    pub limit: Option<i32>,
82    /// Cursor for pagination.
83    pub cursor: Option<String>,
84    /// Partition count and number.
85    pub partition: Option<Partition>,
86}
87
88impl IntoParams for LimitCursorPartitionQuery {
89    fn into_params(self) -> Vec<(String, String)> {
90        let mut params = Vec::<(String, String)>::new();
91        to_query("limit", &self.limit, &mut params);
92        to_query("cursor", &self.cursor, &mut params);
93        to_query("partition", &self.partition, &mut params);
94        params
95    }
96}