zulip_api/filter/
narrow.rs1use serde::{Deserialize, Serialize};
2
3use super::{Operand, Operator};
4
5mod tuple;
6
7pub use tuple::NarrowTuple;
8
9#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
11pub struct Narrow {
12 operator: Operator,
13 operand: Operand,
14}
15
16impl Narrow {
17 pub const fn new() -> Self {
19 Self {
20 operator: Operator::new(),
21 operand: Operand::new(),
22 }
23 }
24
25 pub const fn create(operator: Operator, operand: Operand) -> Self {
27 Self { operator, operand }
28 }
29
30 pub const fn operator(&self) -> &Operator {
32 &self.operator
33 }
34
35 pub fn set_operator(&mut self, val: Operator) {
37 self.operator = val;
38 }
39
40 pub fn with_operator(self, val: Operator) -> Self {
42 Self {
43 operator: val,
44 ..self
45 }
46 }
47
48 pub const fn operand(&self) -> &Operand {
50 &self.operand
51 }
52
53 pub fn set_operand(&mut self, val: Operand) {
55 self.operand = val;
56 }
57
58 pub fn with_operand(self, val: Operand) -> Self {
60 Self {
61 operand: val,
62 ..self
63 }
64 }
65}
66
67impl Default for Narrow {
68 fn default() -> Self {
69 Self::new()
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_narrow() {
79 [
80 (Operator::Channel, Operand::from(1)),
81 (Operator::Channel, Operand::from("general")),
82 (Operator::Channels, Operand::from([1, 2])),
83 (Operator::Channels, Operand::from(["general", "test"])),
84 (Operator::Dm, Operand::from(1)),
85 (Operator::Dm, Operand::from("test_user")),
86 (Operator::Is, Operand::from(1)),
87 (Operator::NotIs, Operand::from(["general", "test"])),
88 (Operator::Near, Operand::from(1)),
89 ]
90 .into_iter()
91 .for_each(|(opor, opand)| {
92 let opor_json = serde_json::to_string(&opor).unwrap();
93 let opand_json = serde_json::to_string(&opand).unwrap();
94
95 let narrow = Narrow::create(opor, opand.clone());
96 let narrow_json = format!(r#"{{"operator":{opor_json},"operand":{opand_json}}}"#);
97
98 assert_eq!(narrow.operator(), &opor);
99 assert_eq!(narrow.operand(), &opand);
100
101 assert_eq!(serde_json::to_string(&narrow).unwrap(), narrow_json);
102 assert_eq!(
103 serde_json::from_str::<Narrow>(&narrow_json).unwrap(),
104 narrow
105 );
106 });
107 }
108}