core_api_client/models/query_models/
filter_operator.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// `FilterOperator` enum defines the types of comparison operations
5/// that can be used in a `SearchQuery` filter.
6///
7/// This enum is parametrized over two types `T1` and `T2` where `T1` is
8/// the key to be filtered upon and `T2` is the value against which the key 
9/// will be compared. Both `T1` and `T2` must implement the `ToString` trait.
10///
11/// The operators provided by this enum include:
12/// 
13/// * `Smaller`: Checks if the value of a key is smaller than the provided value.
14/// * `Bigger`: Checks if the value of a key is greater than the provided value.
15/// * `Eq`: Checks if the value of a key equals the provided value.
16/// * `SmallerEq`: Checks if the value of a key is smaller than or equal to the provided value.
17/// * `BiggerEq`: Checks if the value of a key is greater than or equal to the provided value.
18/// * `Exists`: Checks if a key exists in the data.
19/// * `HasValue`: Checks if the value of a key equals the provided value.
20///
21/// ```rust
22/// use core_api_client::FilterOperator;
23/// 
24/// let filter = FilterOperator::Eq("age", 30);
25/// ```
26///
27/// The string representation follows the convention of `key<operator>value` for most operators,
28/// `_exists_:key` for the `Exists` operator, and `key:value` for the `HasValue` operator.
29
30#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd, Hash)]
31pub enum FilterOperator<T1, T2>
32where
33    T1: ToString,
34    T2: ToString,
35{
36    Smaller(T1, T2),
37    Bigger(T1, T2),
38    Eq(T1, T2),
39    SmallerEq(T1, T2),
40    BiggerEq(T1, T2),
41    Exists(T1),
42    HasValue(T1, T2),
43}
44
45
46impl<T1, T2> FilterOperator<T1, T2>
47where
48    T1: ToString,
49    T2: ToString, 
50{
51    pub(crate) fn parse(self) -> String {
52        match self {
53            FilterOperator::Smaller(key, value) => format!("{}<{}", key.to_string(), value.to_string()),
54            FilterOperator::Bigger(key, value) => format!("{}>{}", key.to_string(), value.to_string()),
55            FilterOperator::Eq(key, value) => format!("{}={}", key.to_string(), value.to_string()),
56            FilterOperator::SmallerEq(key, value) => format!("{}<={}", key.to_string(), value.to_string()),
57            FilterOperator::BiggerEq(key, value) => format!("{}>={}", key.to_string(), value.to_string()),
58            FilterOperator::Exists(key) => format!("_exists_:{}", key.to_string()),
59            FilterOperator::HasValue(key, value) => format!("{}:{}", key.to_string(), value.to_string()),
60        }
61    }
62}
63
64impl<T1, T2> fmt::Display for FilterOperator<T1, T2>
65where
66    T1: ToString,
67    T2: ToString,
68{
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        match self {
71            FilterOperator::Smaller(t1, t2) => write!(f, "Smaller({}, {})", t1.to_string(), t2.to_string()),
72            FilterOperator::Bigger(t1, t2) => write!(f, "Bigger({}, {})", t1.to_string(), t2.to_string()),
73            FilterOperator::Eq(t1, t2) => write!(f, "Eq({}, {})", t1.to_string(), t2.to_string()),
74            FilterOperator::SmallerEq(t1, t2) => write!(f, "SmallerEq({}, {})", t1.to_string(), t2.to_string()),
75            FilterOperator::BiggerEq(t1, t2) => write!(f, "BiggerEq({}, {})", t1.to_string(), t2.to_string()),
76            FilterOperator::Exists(t1) => write!(f, "Exists({})", t1.to_string()),
77            FilterOperator::HasValue(t1, t2) => write!(f, "HasValue({}, {})", t1.to_string(), t2.to_string()),
78        }
79    }
80}