make one cargo feature per backend (#318)

This commit is contained in:
Clément DOUIN
2022-03-04 14:19:54 +01:00
parent c40dde6e9a
commit f06beb61ae
14 changed files with 103 additions and 110 deletions
+4 -2
View File
@@ -89,14 +89,16 @@ impl From<Iter<'_, String, DeserializedAccountConfig>> for Accounts {
fn from(map: Iter<'_, String, DeserializedAccountConfig>) -> Self {
let mut accounts: Vec<_> = map
.map(|(name, config)| match config {
#[cfg(feature = "imap-backend")]
DeserializedAccountConfig::Imap(config) => {
Account::new(name, "imap", config.default.unwrap_or_default())
}
#[cfg(feature = "maildir-backend")]
DeserializedAccountConfig::Maildir(config) => {
Account::new(name, "maildir", config.default.unwrap_or_default())
}
#[cfg(feature = "notmuch")]
DeserializedAccountConfig::Maildir(config) => {
#[cfg(feature = "notmuch-backend")]
DeserializedAccountConfig::Notmuch(config) => {
Account::new(name, "notmuch", config.default.unwrap_or_default())
}
})
+13 -4
View File
@@ -69,11 +69,13 @@ impl<'a> AccountConfig {
.accounts
.iter()
.find(|(_, account)| match account {
#[cfg(feature = "imap-backend")]
DeserializedAccountConfig::Imap(account) => account.default.unwrap_or_default(),
#[cfg(feature = "maildir-backend")]
DeserializedAccountConfig::Maildir(account) => {
account.default.unwrap_or_default()
}
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
DeserializedAccountConfig::Notmuch(account) => {
account.default.unwrap_or_default()
}
@@ -169,6 +171,7 @@ impl<'a> AccountConfig {
trace!("account config: {:?}", account_config);
let backend_config = match account {
#[cfg(feature = "imap-backend")]
DeserializedAccountConfig::Imap(config) => BackendConfig::Imap(ImapBackendConfig {
imap_host: config.imap_host.clone(),
imap_port: config.imap_port.clone(),
@@ -177,12 +180,13 @@ impl<'a> AccountConfig {
imap_login: config.imap_login.clone(),
imap_passwd_cmd: config.imap_passwd_cmd.clone(),
}),
#[cfg(feature = "maildir-backend")]
DeserializedAccountConfig::Maildir(config) => {
BackendConfig::Maildir(MaildirBackendConfig {
maildir_dir: shellexpand::full(&config.maildir_dir)?.to_string().into(),
})
}
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
DeserializedAccountConfig::Notmuch(config) => {
BackendConfig::Notmuch(NotmuchBackendConfig {
notmuch_database_dir: shellexpand::full(&config.notmuch_database_dir)?
@@ -315,13 +319,16 @@ impl<'a> AccountConfig {
/// Represents all existing kind of account (backend).
#[derive(Debug, Clone)]
pub enum BackendConfig {
#[cfg(feature = "imap-backend")]
Imap(ImapBackendConfig),
#[cfg(feature = "maildir-backend")]
Maildir(MaildirBackendConfig),
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
Notmuch(NotmuchBackendConfig),
}
/// Represents the IMAP backend.
#[cfg(feature = "imap-backend")]
#[derive(Debug, Default, Clone)]
pub struct ImapBackendConfig {
/// Represents the IMAP host.
@@ -338,6 +345,7 @@ pub struct ImapBackendConfig {
pub imap_passwd_cmd: String,
}
#[cfg(feature = "imap-backend")]
impl ImapBackendConfig {
/// Gets the IMAP password of the user account.
pub fn imap_passwd(&self) -> Result<String> {
@@ -350,6 +358,7 @@ impl ImapBackendConfig {
}
/// Represents the Maildir backend.
#[cfg(feature = "maildir-backend")]
#[derive(Debug, Default, Clone)]
pub struct MaildirBackendConfig {
/// Represents the Maildir directory path.
@@ -357,7 +366,7 @@ pub struct MaildirBackendConfig {
}
/// Represents the Notmuch backend.
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
#[derive(Debug, Default, Clone)]
pub struct NotmuchBackendConfig {
/// Represents the Notmuch database path.
+1 -4
View File
@@ -104,8 +104,6 @@ mod tests {
}
}
struct TestBackend;
let config = DeserializedConfig {
accounts: HashMap::from_iter([(
"account-1".into(),
@@ -119,14 +117,13 @@ mod tests {
let account_config = AccountConfig::default();
let mut printer = PrinterServiceTest::default();
let mut backend = TestBackend {};
assert!(list(None, &config, &account_config, &mut printer).is_ok());
assert_eq!(
concat![
"\n",
"NAME │BACKEND │DEFAULT \n",
"account-1 │imap │true \n",
"account-1 │imap │yes \n",
"\n"
],
printer.writter.content
+9 -3
View File
@@ -11,18 +11,22 @@ pub trait ToDeserializedBaseAccountConfig {
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum DeserializedAccountConfig {
#[cfg(feature = "imap-backend")]
Imap(DeserializedImapAccountConfig),
#[cfg(feature = "maildir-backend")]
Maildir(DeserializedMaildirAccountConfig),
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
Notmuch(DeserializedNotmuchAccountConfig),
}
impl ToDeserializedBaseAccountConfig for DeserializedAccountConfig {
fn to_base(&self) -> DeserializedBaseAccountConfig {
match self {
#[cfg(feature = "imap-backend")]
Self::Imap(config) => config.to_base(),
#[cfg(feature = "maildir-backend")]
Self::Maildir(config) => config.to_base(),
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
Self::Notmuch(config) => config.to_base(),
}
}
@@ -118,6 +122,7 @@ macro_rules! make_account_config {
make_account_config!(DeserializedBaseAccountConfig,);
#[cfg(feature = "imap-backend")]
make_account_config!(
DeserializedImapAccountConfig,
imap_host: String,
@@ -128,9 +133,10 @@ make_account_config!(
imap_passwd_cmd: String
);
#[cfg(feature = "maildir-backend")]
make_account_config!(DeserializedMaildirAccountConfig, maildir_dir: String);
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
make_account_config!(
DeserializedNotmuchAccountConfig,
notmuch_database_dir: String
+10 -3
View File
@@ -40,6 +40,7 @@ pub mod backends {
pub mod id_mapper;
pub use id_mapper::*;
#[cfg(feature = "imap-backend")]
pub mod imap {
pub mod imap_args;
@@ -62,8 +63,11 @@ pub mod backends {
pub mod msg_sort_criterion;
}
#[cfg(feature = "imap-backend")]
pub use self::imap::*;
#[cfg(feature = "maildir-backend")]
pub mod maildir {
pub mod maildir_backend;
pub use maildir_backend::*;
@@ -77,11 +81,11 @@ pub mod backends {
pub mod maildir_flag;
pub use maildir_flag::*;
}
#[cfg(feature = "maildir-backend")]
pub use self::maildir::*;
#[cfg(feature = "notmuch")]
pub use self::notmuch::*;
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
pub mod notmuch {
pub mod notmuch_backend;
pub use notmuch_backend::*;
@@ -92,6 +96,9 @@ pub mod backends {
pub mod notmuch_envelope;
pub use notmuch_envelope::*;
}
#[cfg(feature = "notmuch-backend")]
pub use self::notmuch::*;
}
pub mod smtp {
+37 -11
View File
@@ -3,7 +3,7 @@ use std::{convert::TryFrom, env};
use url::Url;
use himalaya::{
backends::{imap_args, imap_handlers, Backend, ImapBackend, MaildirBackend},
backends::Backend,
compl::{compl_args, compl_handlers},
config::{
account_args, account_handlers, config_args, AccountConfig, BackendConfig,
@@ -15,11 +15,17 @@ use himalaya::{
smtp::LettreService,
};
#[cfg(feature = "notmuch")]
#[cfg(feature = "imap-backend")]
use himalaya::backends::{imap_args, imap_handlers, ImapBackend};
#[cfg(feature = "maildir-backend")]
use himalaya::backends::MaildirBackend;
#[cfg(feature = "notmuch-backend")]
use himalaya::{backends::NotmuchBackend, config::MaildirBackendConfig};
fn create_app<'a>() -> clap::App<'a, 'a> {
clap::App::new(env!("CARGO_PKG_NAME"))
let app = clap::App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.author(env!("CARGO_PKG_AUTHORS"))
@@ -29,10 +35,14 @@ fn create_app<'a>() -> clap::App<'a, 'a> {
.args(&output_args::args())
.arg(mbox_args::source_arg())
.subcommands(compl_args::subcmds())
.subcommands(imap_args::subcmds())
.subcommands(account_args::subcmds())
.subcommands(mbox_args::subcmds())
.subcommands(msg_args::subcmds())
.subcommands(msg_args::subcmds());
#[cfg(feature = "imap-backend")]
let app = app.subcommands(imap_args::subcmds());
app
}
#[allow(clippy::single_match)]
@@ -50,22 +60,29 @@ fn main() -> Result<()> {
let url = Url::parse(&raw_args[1])?;
let mut smtp = LettreService::from(&account_config);
#[cfg(feature = "imap-backend")]
let mut imap;
#[cfg(feature = "maildir-backend")]
let mut maildir;
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
let maildir_config: MaildirBackendConfig;
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
let mut notmuch;
let backend: Box<&mut dyn Backend> = match backend_config {
#[cfg(feature = "imap-backend")]
BackendConfig::Imap(ref imap_config) => {
imap = ImapBackend::new(&account_config, imap_config);
Box::new(&mut imap)
}
#[cfg(feature = "maildir-backend")]
BackendConfig::Maildir(ref maildir_config) => {
maildir = MaildirBackend::new(&account_config, maildir_config);
Box::new(&mut maildir)
}
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
BackendConfig::Notmuch(ref notmuch_config) => {
maildir_config = MaildirBackendConfig {
maildir_dir: notmuch_config.notmuch_database_dir.clone(),
@@ -100,22 +117,29 @@ fn main() -> Result<()> {
.or_else(|| account_config.mailboxes.get("inbox").map(|s| s.as_str()))
.unwrap_or(DEFAULT_INBOX_FOLDER);
let mut printer = StdoutPrinter::try_from(m.value_of("output"))?;
#[cfg(feature = "imap-backend")]
let mut imap;
#[cfg(feature = "maildir-backend")]
let mut maildir;
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
let maildir_config: MaildirBackendConfig;
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
let mut notmuch;
let backend: Box<&mut dyn Backend> = match backend_config {
#[cfg(feature = "imap-backend")]
BackendConfig::Imap(ref imap_config) => {
imap = ImapBackend::new(&account_config, imap_config);
Box::new(&mut imap)
}
#[cfg(feature = "maildir-backend")]
BackendConfig::Maildir(ref maildir_config) => {
maildir = MaildirBackend::new(&account_config, maildir_config);
Box::new(&mut maildir)
}
#[cfg(feature = "notmuch")]
#[cfg(feature = "notmuch-backend")]
BackendConfig::Notmuch(ref notmuch_config) => {
maildir_config = MaildirBackendConfig {
maildir_dir: notmuch_config.notmuch_database_dir.clone(),
@@ -129,6 +153,8 @@ fn main() -> Result<()> {
let mut smtp = LettreService::from(&account_config);
// Check IMAP commands.
#[allow(irrefutable_let_patterns)]
#[cfg(feature = "imap-backend")]
if let BackendConfig::Imap(ref imap_config) = backend_config {
let mut imap = ImapBackend::new(&account_config, imap_config);
match imap_args::matches(&m)? {
+1 -70
View File
@@ -2,8 +2,7 @@
//!
//! This module regroups email address entities and converters.
use anyhow::{Context, Result};
use log::trace;
use anyhow::Result;
use mailparse;
use std::fmt::Debug;
@@ -63,71 +62,3 @@ pub fn from_addrs_to_sendable_addrs(addrs: &Addrs) -> Result<Vec<lettre::Address
}
Ok(sendable_addrs)
}
/// Converts a [`imap_proto::Address`] into an address.
pub fn from_imap_addr_to_addr(addr: &imap_proto::Address) -> Result<Addr> {
let name = addr
.name
.as_ref()
.map(|name| {
rfc2047_decoder::decode(&name.to_vec())
.context("cannot decode address name")
.map(Some)
})
.unwrap_or(Ok(None))?;
let mbox = addr
.mailbox
.as_ref()
.map(|mbox| {
rfc2047_decoder::decode(&mbox.to_vec())
.context("cannot decode address mailbox")
.map(Some)
})
.unwrap_or(Ok(None))?;
let host = addr
.host
.as_ref()
.map(|host| {
rfc2047_decoder::decode(&host.to_vec())
.context("cannot decode address host")
.map(Some)
})
.unwrap_or(Ok(None))?;
trace!("parsing address from imap address");
trace!("name: {:?}", name);
trace!("mbox: {:?}", mbox);
trace!("host: {:?}", host);
Ok(Addr::Single(mailparse::SingleInfo {
display_name: name,
addr: match host {
Some(host) => format!("{}@{}", mbox.unwrap_or_default(), host),
None => mbox.unwrap_or_default(),
},
}))
}
/// Converts a list of [`imap_proto::Address`] into a list of addresses.
pub fn from_imap_addrs_to_addrs(proto_addrs: &[imap_proto::Address]) -> Result<Addrs> {
let mut addrs = vec![];
for addr in proto_addrs {
addrs.push(
from_imap_addr_to_addr(addr).context(format!("cannot parse address {:?}", addr))?,
);
}
Ok(addrs.into())
}
/// Converts an optional list of [`imap_proto::Address`] into an optional list of addresses.
pub fn from_imap_addrs_to_some_addrs(
addrs: &Option<Vec<imap_proto::Address>>,
) -> Result<Option<Addrs>> {
Ok(
if let Some(addrs) = addrs.as_deref().map(from_imap_addrs_to_addrs) {
Some(addrs?)
} else {
None
},
)
}