jmap_client/mailbox/
query.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12use serde::Serialize;
13
14use crate::{
15    core::query::{self, QueryObject},
16    Set,
17};
18
19use super::{Mailbox, QueryArguments, Role};
20
21#[derive(Serialize, Clone, Debug)]
22#[serde(untagged)]
23pub enum Filter {
24    ParentId {
25        #[serde(rename = "parentId")]
26        value: Option<String>,
27    },
28    Name {
29        #[serde(rename = "name")]
30        value: String,
31    },
32    Role {
33        #[serde(rename = "role")]
34        value: Option<Role>,
35    },
36    HasAnyRole {
37        #[serde(rename = "hasAnyRole")]
38        value: bool,
39    },
40    IsSubscribed {
41        #[serde(rename = "isSubscribed")]
42        value: bool,
43    },
44}
45
46#[derive(Serialize, Debug, Clone)]
47#[serde(tag = "property")]
48pub enum Comparator {
49    #[serde(rename = "name")]
50    Name,
51    #[serde(rename = "sortOrder")]
52    SortOrder,
53    #[serde(rename = "parentId")]
54    ParentId,
55}
56
57impl Filter {
58    pub fn parent_id(value: Option<impl Into<String>>) -> Self {
59        Filter::ParentId {
60            value: value.map(Into::into),
61        }
62    }
63
64    pub fn name(value: impl Into<String>) -> Self {
65        Filter::Name {
66            value: value.into(),
67        }
68    }
69
70    pub fn role(value: Role) -> Self {
71        Filter::Role {
72            value: if !matches!(value, Role::None) {
73                value.into()
74            } else {
75                None
76            },
77        }
78    }
79
80    pub fn has_any_role(value: bool) -> Self {
81        Filter::HasAnyRole { value }
82    }
83
84    pub fn is_subscribed(value: bool) -> Self {
85        Filter::IsSubscribed { value }
86    }
87}
88
89impl Comparator {
90    pub fn name() -> query::Comparator<Comparator> {
91        query::Comparator::new(Comparator::Name)
92    }
93
94    pub fn sort_order() -> query::Comparator<Comparator> {
95        query::Comparator::new(Comparator::SortOrder)
96    }
97
98    pub fn parent_id() -> query::Comparator<Comparator> {
99        query::Comparator::new(Comparator::ParentId)
100    }
101}
102
103impl QueryArguments {
104    pub fn sort_as_tree(&mut self, value: bool) -> &mut Self {
105        self.sort_as_tree = value;
106        self
107    }
108
109    pub fn filter_as_tree(&mut self, value: bool) -> &mut Self {
110        self.filter_as_tree = value;
111        self
112    }
113}
114
115impl QueryObject for Mailbox<Set> {
116    type QueryArguments = QueryArguments;
117
118    type Filter = Filter;
119
120    type Sort = Comparator;
121}