email/account/config/pgp/
mod.rs

1//! Module dedicated to PGP configuration.
2//!
3//! This module contains everything related to PGP configuration.
4#[cfg(feature = "pgp-commands")]
5pub mod cmds;
6#[cfg(feature = "derive")]
7pub mod derive;
8#[cfg(feature = "pgp-gpg")]
9pub mod gpg;
10#[cfg(feature = "pgp-native")]
11pub mod native;
12
13use std::io;
14
15use mml::pgp::Pgp;
16
17#[cfg(feature = "pgp-commands")]
18#[doc(inline)]
19pub use self::cmds::PgpCommandsConfig;
20#[cfg(feature = "pgp-gpg")]
21#[doc(inline)]
22pub use self::gpg::PgpGpgConfig;
23#[cfg(feature = "pgp-native")]
24#[doc(inline)]
25pub use self::native::PgpNativeConfig;
26#[doc(inline)]
27pub use super::{Error, Result};
28
29/// The PGP configuration.
30#[derive(Clone, Debug, Default, Eq, PartialEq)]
31#[cfg_attr(
32    feature = "derive",
33    derive(serde::Serialize, serde::Deserialize),
34    serde(rename_all = "kebab-case", tag = "type"),
35    serde(from = "derive::PgpConfig")
36)]
37pub enum PgpConfig {
38    #[default]
39    None,
40    /// Commands configuration.
41    #[cfg(feature = "pgp-commands")]
42    Commands(PgpCommandsConfig),
43    /// GPG configuration.
44    #[cfg(feature = "pgp-gpg")]
45    Gpg(PgpGpgConfig),
46    /// Native configuration.
47    #[cfg(feature = "pgp-native")]
48    Native(PgpNativeConfig),
49}
50
51impl From<PgpConfig> for Pgp {
52    fn from(config: PgpConfig) -> Self {
53        match config {
54            PgpConfig::None => Pgp::None,
55            #[cfg(feature = "pgp-commands")]
56            PgpConfig::Commands(config) => Pgp::from(config),
57            #[cfg(feature = "pgp-gpg")]
58            PgpConfig::Gpg(config) => Pgp::from(config),
59            #[cfg(feature = "pgp-native")]
60            PgpConfig::Native(config) => Pgp::from(config),
61        }
62    }
63}
64
65impl PgpConfig {
66    pub async fn reset(&self) -> Result<()> {
67        match self {
68            Self::None => Ok(()),
69            #[cfg(feature = "pgp-commands")]
70            Self::Commands(..) => Ok(()),
71            #[cfg(feature = "pgp-gpg")]
72            Self::Gpg(..) => Ok(()),
73            #[cfg(feature = "pgp-native")]
74            Self::Native(config) => config.reset().await,
75        }
76    }
77
78    #[allow(unused)]
79    pub async fn configure(
80        &self,
81        email: impl ToString,
82        passwd: impl Fn() -> io::Result<String>,
83    ) -> Result<()> {
84        match self {
85            Self::None => Ok(()),
86            #[cfg(feature = "pgp-commands")]
87            Self::Commands(..) => Ok(()),
88            #[cfg(feature = "pgp-gpg")]
89            Self::Gpg(..) => Ok(()),
90            #[cfg(feature = "pgp-native")]
91            Self::Native(config) => config.configure(email, passwd).await,
92        }
93    }
94}