1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright Stalwart Labs Ltd. See the COPYING
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */

pub mod get;
pub mod helpers;
pub mod query;
pub mod set;

use crate::core::set::{list_not_set, map_not_set, string_not_set};
use ahash::AHashMap;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

use crate::{
    core::{changes::ChangesObject, Object},
    Get, Set,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Principal<State = Get> {
    #[serde(skip)]
    _create_id: Option<usize>,

    #[serde(skip)]
    _state: std::marker::PhantomData<State>,

    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<String>,

    #[serde(rename = "type")]
    #[serde(skip_serializing_if = "Option::is_none")]
    ptype: Option<Type>,

    #[serde(skip_serializing_if = "string_not_set")]
    name: Option<String>,

    #[serde(skip_serializing_if = "string_not_set")]
    description: Option<String>,

    #[serde(skip_serializing_if = "string_not_set")]
    email: Option<String>,

    #[serde(skip_serializing_if = "string_not_set")]
    timezone: Option<String>,

    #[serde(skip_serializing_if = "list_not_set")]
    capabilities: Option<Vec<String>>,

    #[serde(skip_serializing_if = "list_not_set")]
    aliases: Option<Vec<String>>,

    #[serde(skip_serializing_if = "string_not_set")]
    secret: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    dkim: Option<DKIM>,

    #[serde(skip_serializing_if = "Option::is_none")]
    quota: Option<u32>,

    #[serde(skip_serializing_if = "string_not_set")]
    picture: Option<String>,

    #[serde(skip_serializing_if = "list_not_set")]
    members: Option<Vec<String>>,

    #[serde(skip_serializing_if = "map_not_set")]
    acl: Option<AHashMap<String, Vec<ACL>>>,

    #[serde(flatten)]
    #[serde(skip_deserializing)]
    #[serde(skip_serializing_if = "Option::is_none")]
    property_patch: Option<AHashMap<String, bool>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Copy)]
pub enum Property {
    #[serde(rename = "id")]
    Id = 0,
    #[serde(rename = "type")]
    Type = 1,
    #[serde(rename = "name")]
    Name = 2,
    #[serde(rename = "description")]
    Description = 3,
    #[serde(rename = "email")]
    Email = 4,
    #[serde(rename = "timezone")]
    Timezone = 5,
    #[serde(rename = "capabilities")]
    Capabilities = 6,
    #[serde(rename = "aliases")]
    Aliases = 7,
    #[serde(rename = "secret")]
    Secret = 8,
    #[serde(rename = "dkim")]
    DKIM = 9,
    #[serde(rename = "quota")]
    Quota = 10,
    #[serde(rename = "picture")]
    Picture = 11,
    #[serde(rename = "members")]
    Members = 12,
    #[serde(rename = "acl")]
    ACL = 13,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Copy)]
pub enum ACL {
    #[serde(rename = "read")]
    Read = 0,
    #[serde(rename = "modify")]
    Modify = 1,
    #[serde(rename = "delete")]
    Delete = 2,
    #[serde(rename = "readItems")]
    ReadItems = 3,
    #[serde(rename = "addItems")]
    AddItems = 4,
    #[serde(rename = "modifyItems")]
    ModifyItems = 5,
    #[serde(rename = "removeItems")]
    RemoveItems = 6,
    #[serde(rename = "createChild")]
    CreateChild = 7,
    #[serde(rename = "administer")]
    Administer = 8,
    #[serde(rename = "submit")]
    Submit = 10,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Type {
    #[serde(rename = "individual")]
    Individual,
    #[serde(rename = "group")]
    Group,
    #[serde(rename = "resource")]
    Resource,
    #[serde(rename = "location")]
    Location,
    #[serde(rename = "domain")]
    Domain,
    #[serde(rename = "list")]
    List,
    #[serde(rename = "other")]
    Other,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DKIM {
    #[serde(rename = "dkimSelector")]
    dkim_selector: Option<String>,
    #[serde(rename = "dkimExpiration")]
    dkim_expiration: Option<i64>,
}

impl DKIM {
    pub fn new(dkim_selector: Option<impl Into<String>>, dkim_expiration: Option<i64>) -> DKIM {
        DKIM {
            dkim_selector: dkim_selector.map(Into::into),
            dkim_expiration,
        }
    }

    pub fn selector(&self) -> Option<&str> {
        self.dkim_selector.as_deref()
    }

    pub fn expiration(&self) -> Option<i64> {
        self.dkim_expiration
    }
}

impl Display for Property {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Property::Id => write!(f, "id"),
            Property::Type => write!(f, "type"),
            Property::Name => write!(f, "name"),
            Property::Description => write!(f, "description"),
            Property::Email => write!(f, "email"),
            Property::Timezone => write!(f, "timezone"),
            Property::Capabilities => write!(f, "capabilities"),
            Property::Aliases => write!(f, "aliases"),
            Property::Secret => write!(f, "secret"),
            Property::DKIM => write!(f, "dkim"),
            Property::Quota => write!(f, "quota"),
            Property::Picture => write!(f, "picture"),
            Property::Members => write!(f, "members"),
            Property::ACL => write!(f, "acl"),
        }
    }
}

impl Display for ACL {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ACL::Read => write!(f, "read"),
            ACL::Modify => write!(f, "modify"),
            ACL::Delete => write!(f, "delete"),
            ACL::ReadItems => write!(f, "readItems"),
            ACL::AddItems => write!(f, "addItems"),
            ACL::ModifyItems => write!(f, "modifyItems"),
            ACL::RemoveItems => write!(f, "removeItems"),
            ACL::CreateChild => write!(f, "createChild"),
            ACL::Administer => write!(f, "administer"),
            ACL::Submit => write!(f, "submit"),
        }
    }
}

impl Object for Principal<Set> {
    type Property = Property;

    fn requires_account_id() -> bool {
        true
    }
}

impl Object for Principal<Get> {
    type Property = Property;

    fn requires_account_id() -> bool {
        true
    }
}

impl ChangesObject for Principal<Set> {
    type ChangesResponse = ();
}

impl ChangesObject for Principal<Get> {
    type ChangesResponse = ();
}