jmap_client/principal/
set.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 super::{Principal, Property, Type, ACL, DKIM};
13use crate::{core::set::SetObject, Get, Set};
14use ahash::AHashMap;
15
16impl Principal<Set> {
17    pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
18        self.name = name.into().into();
19        self
20    }
21
22    pub fn description(&mut self, description: Option<impl Into<String>>) -> &mut Self {
23        self.description = description.map(|s| s.into());
24        self
25    }
26
27    pub fn email(&mut self, email: impl Into<String>) -> &mut Self {
28        self.email = email.into().into();
29        self
30    }
31
32    pub fn secret(&mut self, secret: impl Into<String>) -> &mut Self {
33        self.secret = secret.into().into();
34        self
35    }
36
37    pub fn timezone(&mut self, timezone: Option<impl Into<String>>) -> &mut Self {
38        self.timezone = timezone.map(|s| s.into());
39        self
40    }
41
42    pub fn picture(&mut self, picture: Option<impl Into<String>>) -> &mut Self {
43        self.picture = picture.map(|s| s.into());
44        self
45    }
46
47    pub fn quota(&mut self, quota: Option<u32>) -> &mut Self {
48        self.quota = quota;
49        self
50    }
51
52    pub fn ptype(&mut self, ptype: Type) -> &mut Self {
53        self.ptype = ptype.into();
54        self
55    }
56
57    pub fn dkim(&mut self, dkim: DKIM) -> &mut Self {
58        self.dkim = dkim.into();
59        self
60    }
61
62    pub fn acl(&mut self, acl: Option<AHashMap<String, Vec<ACL>>>) -> &mut Self {
63        self.acl = acl;
64        self
65    }
66
67    pub fn aliases<T, U>(&mut self, aliases: Option<T>) -> &mut Self
68    where
69        T: IntoIterator<Item = U>,
70        U: Into<String>,
71    {
72        self.aliases = aliases.map(|l| l.into_iter().map(|v| v.into()).collect());
73        self
74    }
75
76    pub fn alias(&mut self, alias: &str, set: bool) -> &mut Self {
77        self.property_patch
78            .get_or_insert_with(AHashMap::new)
79            .insert(format!("{}/{}", Property::Aliases, alias), set);
80        self
81    }
82
83    pub fn capabilities<T, U>(&mut self, capabilities: Option<T>) -> &mut Self
84    where
85        T: IntoIterator<Item = U>,
86        U: Into<String>,
87    {
88        self.capabilities = capabilities.map(|l| l.into_iter().map(|v| v.into()).collect());
89        self
90    }
91
92    pub fn members<T, U>(&mut self, members: Option<T>) -> &mut Self
93    where
94        T: IntoIterator<Item = U>,
95        U: Into<String>,
96    {
97        self.members = members.map(|l| l.into_iter().map(|v| v.into()).collect());
98        self
99    }
100
101    pub fn member(&mut self, member: &str, set: bool) -> &mut Self {
102        self.property_patch
103            .get_or_insert_with(AHashMap::new)
104            .insert(format!("{}/{}", Property::Members, member), set);
105        self
106    }
107}
108
109impl SetObject for Principal<Set> {
110    type SetArguments = ();
111
112    fn new(_create_id: Option<usize>) -> Self {
113        Principal {
114            _create_id,
115            _state: Default::default(),
116            id: None,
117            ptype: None,
118            name: "".to_string().into(),
119            description: "".to_string().into(),
120            email: "".to_string().into(),
121            timezone: "".to_string().into(),
122            capabilities: Vec::with_capacity(0).into(),
123            aliases: Vec::with_capacity(0).into(),
124            secret: "".to_string().into(),
125            dkim: None,
126            quota: None,
127            picture: "".to_string().into(),
128            members: Vec::with_capacity(0).into(),
129            acl: AHashMap::with_capacity(0).into(),
130            property_patch: None,
131        }
132    }
133
134    fn create_id(&self) -> Option<String> {
135        self._create_id.map(|id| format!("c{}", id))
136    }
137}
138
139impl SetObject for Principal<Get> {
140    type SetArguments = ();
141
142    fn new(_create_id: Option<usize>) -> Self {
143        unimplemented!()
144    }
145
146    fn create_id(&self) -> Option<String> {
147        None
148    }
149}