email/
config.rs

1use std::{collections::HashMap, path::PathBuf};
2
3use crate::{
4    account::{config::AccountConfig, Error},
5    Result,
6};
7
8#[derive(Clone, Debug, Default, Eq, PartialEq)]
9#[cfg_attr(
10    feature = "derive",
11    derive(serde::Serialize, serde::Deserialize),
12    serde(rename_all = "kebab-case", deny_unknown_fields)
13)]
14pub struct Config {
15    /// The default display name of the user.
16    ///
17    /// It usually corresponds to the full name of the user. This
18    /// display name is used by default for all accounts.
19    pub display_name: Option<String>,
20
21    /// The default email signature of the user.
22    ///
23    /// It can be either a path to a file (usually `~/.signature`) or
24    /// a raw string. This signature is used by default for all
25    /// accounts.
26    pub signature: Option<String>,
27
28    /// The default email signature delimiter of the user signature.
29    ///
30    /// Defaults to `-- \n`. This signature delimiter is used by
31    /// default for all accounts.
32    pub signature_delim: Option<String>,
33
34    /// The default downloads directory.
35    ///
36    /// It is mostly used for downloading messages
37    /// attachments. Defaults to the system temporary directory
38    /// (usually `/tmp`). This downloads directory is used by default
39    /// for all accounts.
40    pub downloads_dir: Option<PathBuf>,
41
42    /// The map of account-specific configurations.
43    pub accounts: HashMap<String, AccountConfig>,
44}
45
46impl Config {
47    pub fn account(&self, name: impl AsRef<str>) -> Result<AccountConfig> {
48        let name = name.as_ref();
49
50        let account_config = self
51            .accounts
52            .get(name)
53            .ok_or_else(|| Error::GetAccountConfigNotFoundError(name.to_owned()))?;
54
55        Ok(AccountConfig {
56            name: name.to_owned(),
57            email: account_config.email.clone(),
58            display_name: account_config
59                .display_name
60                .as_ref()
61                .map(ToOwned::to_owned)
62                .or_else(|| self.display_name.as_ref().map(ToOwned::to_owned)),
63            signature_delim: account_config
64                .signature_delim
65                .as_ref()
66                .map(ToOwned::to_owned)
67                .or_else(|| self.signature_delim.as_ref().map(ToOwned::to_owned)),
68            signature: account_config
69                .signature
70                .as_ref()
71                .map(ToOwned::to_owned)
72                .or_else(|| self.signature.as_ref().map(ToOwned::to_owned)),
73            downloads_dir: account_config
74                .downloads_dir
75                .as_ref()
76                .map(ToOwned::to_owned)
77                .or_else(|| self.downloads_dir.as_ref().map(ToOwned::to_owned)),
78            folder: account_config.folder.clone(),
79            envelope: account_config.envelope.clone(),
80            flag: account_config.flag.clone(),
81            message: account_config.message.clone(),
82            template: account_config.template.clone(),
83            #[cfg(feature = "sync")]
84            sync: account_config.sync.clone(),
85            #[cfg(feature = "pgp")]
86            pgp: account_config.pgp.clone(),
87        })
88    }
89}