cognite/dto/filter/aggregate.rs
1use serde::{Deserialize, Serialize};
2use serde_with::skip_serializing_none;
3
4use crate::RawValue;
5
6#[skip_serializing_none]
7#[derive(Serialize, Deserialize, Debug, Clone)]
8#[serde(rename_all = "camelCase")]
9/// Filter for aggregating values.
10pub enum AggregateFilter {
11 /// Require the value to be in the list of `values`
12 In {
13 /// List of values.
14 values: RawValue,
15 },
16 /// Require the value to be text and start with `prefix`.
17 Prefix {
18 /// String prefix.
19 prefix: String,
20 },
21 /// Require the value to be greater than `gt`, greater than or equal to `gte`,
22 /// less than `lt` and less than or equal to `lte`
23 Range {
24 /// Greater than or equal to
25 gte: Option<RawValue>,
26 /// Greater than
27 gt: Option<RawValue>,
28 /// Less than or equal to
29 lte: Option<RawValue>,
30 /// Less than
31 lt: Option<RawValue>,
32 },
33 /// Require all these filters to match
34 And(Vec<AggregateFilter>),
35 /// Require at least one of these filters to match.
36 Or(Vec<AggregateFilter>),
37 /// Require this filter _not_ to match.
38 Not(Box<AggregateFilter>),
39}
40
41impl AggregateFilter {
42 /// Construct an `In` filter.
43 ///
44 /// # Arguments
45 ///
46 /// * `values` - List of values.
47 pub fn is_in(values: impl Into<RawValue>) -> Self {
48 Self::In {
49 values: values.into(),
50 }
51 }
52 /// Construct a `Prefix` filter.
53 ///
54 /// # Arguments
55 ///
56 /// * `prefix` - String prefix.
57 pub fn prefix(prefix: impl Into<String>) -> Self {
58 Self::Prefix {
59 prefix: prefix.into(),
60 }
61 }
62 /// Construct a `range` filter.
63 ///
64 /// # Arguments
65 ///
66 /// * `gte` - Greater than or equal to.
67 /// * `gt` - Greater than.
68 /// * `lte` - Less than or equal to.
69 /// * `lt` - Less than.
70 pub fn range(
71 gte: Option<impl Into<RawValue>>,
72 gt: Option<impl Into<RawValue>>,
73 lte: Option<impl Into<RawValue>>,
74 lt: Option<impl Into<RawValue>>,
75 ) -> Self {
76 Self::Range {
77 gte: gte.map(|v| v.into()),
78 gt: gt.map(|v| v.into()),
79 lte: lte.map(|v| v.into()),
80 lt: lt.map(|v| v.into()),
81 }
82 }
83 #[allow(clippy::should_implement_trait)]
84 /// Construct a `not` filter.
85 ///
86 /// # Arguments
87 ///
88 /// * `filter` - Filter to invert.
89 pub fn not(filter: AggregateFilter) -> Self {
90 match filter {
91 Self::Not(n) => *n,
92 _ => Self::Not(Box::new(filter)),
93 }
94 }
95 /// Construct an `and` filter from this filter and another filter.
96 ///
97 /// # Arguments
98 ///
99 /// * `filter` - AND with this filter.
100 pub fn and(mut self, filter: AggregateFilter) -> Self {
101 match &mut self {
102 Self::And(a) => {
103 a.push(filter);
104 self
105 }
106 _ => Self::And(vec![self, filter]),
107 }
108 }
109
110 /// Construct an `or` filter from this filter and another filter.
111 ///
112 /// # Arguments
113 ///
114 /// * `filter` - OR with this filter.
115 pub fn or(mut self, filter: AggregateFilter) -> Self {
116 match &mut self {
117 Self::Or(a) => {
118 a.push(filter);
119 self
120 }
121 _ => Self::Or(vec![self, filter]),
122 }
123 }
124}