dust_mail/discover/
config.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4use crate::client::connection::ConnectionSecurity;
5#[cfg(feature = "json")]
6use crate::error::Result;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub enum ServerConfigType {
11    Imap,
12    Pop,
13    Smtp,
14    Exchange,
15}
16
17impl ServerConfigType {
18    pub fn is_outgoing(&self) -> bool {
19        match self {
20            Self::Smtp => true,
21            _ => false,
22        }
23    }
24}
25
26#[derive(Debug, Clone)]
27#[cfg_attr(
28    feature = "serde",
29    derive(Serialize, Deserialize),
30    serde(rename_all = "camelCase")
31)]
32pub struct ServerConfig {
33    r#type: ServerConfigType,
34    port: u16,
35    domain: String,
36    security: ConnectionSecurity,
37    auth_type: Vec<AuthenticationType>,
38}
39
40impl ServerConfig {
41    pub fn new<S: Into<String>>(
42        r#type: ServerConfigType,
43        port: u16,
44        domain: S,
45        security: ConnectionSecurity,
46        auth_type: Vec<AuthenticationType>,
47    ) -> Self {
48        Self {
49            r#type,
50            port,
51            domain: domain.into(),
52            security,
53            auth_type,
54        }
55    }
56
57    pub fn r#type(&self) -> &ServerConfigType {
58        &self.r#type
59    }
60
61    pub fn port(&self) -> &u16 {
62        &self.port
63    }
64
65    pub fn domain(&self) -> &str {
66        &self.domain
67    }
68
69    pub fn security(&self) -> &ConnectionSecurity {
70        &self.security
71    }
72
73    pub fn auth_type(&self) -> &Vec<AuthenticationType> {
74        &self.auth_type
75    }
76}
77
78#[derive(Debug, Clone)]
79#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
80pub enum AuthenticationType {
81    ClearText,
82    Encrypted,
83    OAuth2,
84    None,
85    Unknown,
86}
87
88#[derive(Debug, Serialize, Clone)]
89#[serde(rename_all = "camelCase")]
90pub struct OAuth2Config {
91    token_url: String,
92    oauth_url: String,
93    scopes: Vec<String>,
94}
95
96impl OAuth2Config {
97    pub fn new<S: Into<String>>(token_url: S, oauth_url: S, scopes: Vec<S>) -> Self {
98        let scopes = scopes.into_iter().map(|scope| scope.into()).collect();
99
100        Self {
101            oauth_url: oauth_url.into(),
102            token_url: token_url.into(),
103            scopes,
104        }
105    }
106    pub fn oauth_url(&self) -> &str {
107        &self.oauth_url
108    }
109
110    pub fn token_url(&self) -> &str {
111        &self.token_url
112    }
113
114    pub fn scopes(&self) -> &Vec<String> {
115        &self.scopes
116    }
117}
118
119#[derive(Debug, Serialize, Clone)]
120#[serde(rename_all = "camelCase")]
121pub enum ConfigType {
122    MultiServer {
123        incoming: Vec<ServerConfig>,
124        outgoing: Vec<ServerConfig>,
125    },
126}
127
128impl ConfigType {
129    pub fn new_multiserver(incoming: Vec<ServerConfig>, outgoing: Vec<ServerConfig>) -> Self {
130        ConfigType::MultiServer { incoming, outgoing }
131    }
132}
133
134#[derive(Debug, Serialize, Clone)]
135#[serde(rename_all = "camelCase")]
136pub struct Config {
137    r#type: ConfigType,
138    provider: String,
139    oauth2: Option<OAuth2Config>,
140    display_name: Option<String>,
141}
142
143impl Config {
144    pub fn new<S: Into<String>, D: Into<String>>(
145        r#type: ConfigType,
146        provider: S,
147        oauth2_config: Option<OAuth2Config>,
148        display_name: Option<D>,
149    ) -> Self {
150        Self {
151            display_name: display_name.map(|name| name.into()),
152            oauth2: oauth2_config,
153            provider: provider.into(),
154            r#type,
155        }
156    }
157
158    pub fn oauth2(&self) -> &Option<OAuth2Config> {
159        &self.oauth2
160    }
161
162    /// The kind of config
163    pub fn config_type(&self) -> &ConfigType {
164        &self.r#type
165    }
166
167    /// The email provider name
168    pub fn provider(&self) -> &str {
169        &self.provider
170    }
171
172    /// The display name for the email provider
173    pub fn display_name(&self) -> &Option<String> {
174        &self.display_name
175    }
176
177    #[cfg(feature = "json")]
178    pub fn to_json(&self) -> Result<String> {
179        parse::json::to_json(self)
180    }
181}