Skip to main content

zulip_api/filter/narrow/
tuple.rs

1use serde::{Deserialize, Serialize};
2
3use super::{Operand, Operator};
4
5/// Represents the narrow filter used to restrict results.
6#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
7pub struct NarrowTuple(Operator, Operand);
8
9impl NarrowTuple {
10    /// Creates a new [NarrowTuple].
11    pub const fn new() -> Self {
12        Self(Operator::new(), Operand::new())
13    }
14
15    /// Creates a new [NarrowTuple] from the provide parameters.
16    pub const fn create(operator: Operator, operand: Operand) -> Self {
17        Self(operator, operand)
18    }
19
20    /// Gets the [NarrowTuple] operator.
21    pub const fn operator(&self) -> &Operator {
22        &self.0
23    }
24
25    /// Sets the [NarrowTuple] operator.
26    pub fn set_operator(&mut self, val: Operator) {
27        self.0 = val;
28    }
29
30    /// Builder function that sets the [NarrowTuple] operator.
31    pub fn with_operator(self, val: Operator) -> Self {
32        Self(val, self.1)
33    }
34
35    /// Gets the [NarrowTuple] operand.
36    pub const fn operand(&self) -> &Operand {
37        &self.1
38    }
39
40    /// Sets the [NarrowTuple] operand.
41    pub fn set_operand(&mut self, val: Operand) {
42        self.1 = val;
43    }
44
45    /// Builder function that sets the [NarrowTuple] operand.
46    pub fn with_operand(self, val: Operand) -> Self {
47        Self(self.0, val)
48    }
49}
50
51impl Default for NarrowTuple {
52    fn default() -> Self {
53        Self::new()
54    }
55}
56
57impl std::fmt::Display for NarrowTuple {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        serde_json::to_string(self)
60            .map_err(|_| std::fmt::Error)
61            .and_then(|s| write!(f, "{s}"))
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_narrow_tuple() {
71        [
72            (Operator::Channel, Operand::from(1)),
73            (Operator::Channel, Operand::from("general")),
74            (Operator::Channels, Operand::from([1, 2])),
75            (Operator::Channels, Operand::from(["general", "test"])),
76            (Operator::Dm, Operand::from(1)),
77            (Operator::Dm, Operand::from("test_user")),
78            (Operator::Is, Operand::from(1)),
79            (Operator::NotIs, Operand::from(["general", "test"])),
80            (Operator::Near, Operand::from(1)),
81        ]
82        .into_iter()
83        .for_each(|(opor, opand)| {
84            let opor_json = serde_json::to_string(&opor).unwrap();
85            let opand_json = serde_json::to_string(&opand).unwrap();
86
87            let narrow = NarrowTuple::create(opor, opand.clone());
88            let narrow_json = format!(r#"[{opor_json},{opand_json}]"#);
89
90            assert_eq!(narrow.operator(), &opor);
91            assert_eq!(narrow.operand(), &opand);
92
93            assert_eq!(serde_json::to_string(&narrow).unwrap(), narrow_json);
94            assert_eq!(
95                serde_json::from_str::<NarrowTuple>(&narrow_json).unwrap(),
96                narrow
97            );
98        });
99    }
100}