Skip to main content

sift_query_core/policy/
names.rs

1use crate::ConfigError;
2pub(crate) use sift_query_algorithm::is_ascii_name as is_name;
3
4/// Names of protocol-level control parameters.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct ParamNames {
7    pub page: String,
8    pub size: String,
9    pub offset: String,
10    pub cursor: String,
11    pub sort: String,
12    pub order: String,
13    pub order_by: String,
14    pub group_expr: String,
15    pub only_select: String,
16    pub select_exclude: String,
17}
18
19impl Default for ParamNames {
20    fn default() -> Self {
21        Self {
22            page: "page".to_owned(),
23            size: "size".to_owned(),
24            offset: "offset".to_owned(),
25            cursor: "cursor".to_owned(),
26            sort: "sort".to_owned(),
27            order: "order".to_owned(),
28            order_by: "orderBy".to_owned(),
29            group_expr: "gexpr".to_owned(),
30            only_select: "onlySelect".to_owned(),
31            select_exclude: "selectExclude".to_owned(),
32        }
33    }
34}
35
36impl ParamNames {
37    pub fn validate(&self) -> Result<(), ConfigError> {
38        let names = self.all();
39        for name in names {
40            if !is_name(name) {
41                return Err(ConfigError::Invalid(format!(
42                    "invalid control parameter name: {name}"
43                )));
44            }
45        }
46        for (index, name) in names.iter().enumerate() {
47            if names[index + 1..].contains(name) {
48                return Err(ConfigError::Invalid(format!(
49                    "duplicate control parameter name: {name}"
50                )));
51            }
52        }
53        Ok(())
54    }
55
56    #[must_use]
57    pub fn all(&self) -> [&str; 10] {
58        [
59            &self.page,
60            &self.size,
61            &self.offset,
62            &self.cursor,
63            &self.sort,
64            &self.order,
65            &self.order_by,
66            &self.group_expr,
67            &self.only_select,
68            &self.select_exclude,
69        ]
70    }
71}