mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 21:37:55 +08:00
add one cargo feature per backend feature
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
#[cfg(feature = "imap")]
|
||||
use email::imap::config::ImapConfig;
|
||||
#[cfg(feature = "maildir")]
|
||||
use email::maildir::config::MaildirConfig;
|
||||
#[cfg(feature = "notmuch")]
|
||||
use email::notmuch::config::NotmuchConfig;
|
||||
#[cfg(feature = "sendmail")]
|
||||
use email::sendmail::config::SendmailConfig;
|
||||
#[cfg(feature = "smtp")]
|
||||
use email::smtp::config::SmtpConfig;
|
||||
use email::{maildir::config::MaildirConfig, sendmail::config::SendmailConfig};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum BackendConfig {
|
||||
Maildir(MaildirConfig),
|
||||
#[cfg(feature = "imap")]
|
||||
Imap(ImapConfig),
|
||||
#[cfg(feature = "maildir")]
|
||||
Maildir(MaildirConfig),
|
||||
#[cfg(feature = "notmuch")]
|
||||
Notmuch(NotmuchConfig),
|
||||
#[cfg(feature = "smtp")]
|
||||
Smtp(SmtpConfig),
|
||||
#[cfg(feature = "sendmail")]
|
||||
Sendmail(SendmailConfig),
|
||||
}
|
||||
|
||||
+287
-153
@@ -5,76 +5,128 @@ use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use std::ops::Deref;
|
||||
|
||||
use email::account::config::AccountConfig;
|
||||
#[cfg(all(feature = "envelope-get", feature = "imap"))]
|
||||
use email::envelope::get::imap::GetEnvelopeImap;
|
||||
#[cfg(all(feature = "envelope-get", feature = "maildir"))]
|
||||
use email::envelope::get::maildir::GetEnvelopeMaildir;
|
||||
#[cfg(all(feature = "envelope-list", feature = "imap"))]
|
||||
use email::envelope::list::imap::ListEnvelopesImap;
|
||||
#[cfg(all(feature = "envelope-list", feature = "maildir"))]
|
||||
use email::envelope::list::maildir::ListEnvelopesMaildir;
|
||||
#[cfg(all(feature = "envelope-watch", feature = "imap"))]
|
||||
use email::envelope::watch::imap::WatchImapEnvelopes;
|
||||
#[cfg(all(feature = "envelope-watch", feature = "maildir"))]
|
||||
use email::envelope::watch::maildir::WatchMaildirEnvelopes;
|
||||
#[cfg(feature = "message-add")]
|
||||
use email::envelope::SingleId;
|
||||
#[cfg(all(feature = "flag-add", feature = "imap"))]
|
||||
use email::flag::add::imap::AddFlagsImap;
|
||||
#[cfg(all(feature = "flag-add", feature = "maildir"))]
|
||||
use email::flag::add::maildir::AddFlagsMaildir;
|
||||
#[cfg(all(feature = "flag-remove", feature = "imap"))]
|
||||
use email::flag::remove::imap::RemoveFlagsImap;
|
||||
#[cfg(all(feature = "flag-remove", feature = "maildir"))]
|
||||
use email::flag::remove::maildir::RemoveFlagsMaildir;
|
||||
#[cfg(all(feature = "flag-set", feature = "imap"))]
|
||||
use email::flag::set::imap::SetFlagsImap;
|
||||
#[cfg(all(feature = "flag-set", feature = "maildir"))]
|
||||
use email::flag::set::maildir::SetFlagsMaildir;
|
||||
#[cfg(all(feature = "folder-add", feature = "imap"))]
|
||||
use email::folder::add::imap::AddFolderImap;
|
||||
#[cfg(all(feature = "folder-add", feature = "maildir"))]
|
||||
use email::folder::add::maildir::AddFolderMaildir;
|
||||
#[cfg(all(feature = "folder-delete", feature = "imap"))]
|
||||
use email::folder::delete::imap::DeleteFolderImap;
|
||||
#[cfg(all(feature = "folder-delete", feature = "maildir"))]
|
||||
use email::folder::delete::maildir::DeleteFolderMaildir;
|
||||
#[cfg(all(feature = "folder-expunge", feature = "imap"))]
|
||||
use email::folder::expunge::imap::ExpungeFolderImap;
|
||||
#[cfg(all(feature = "folder-expunge", feature = "maildir"))]
|
||||
use email::folder::expunge::maildir::ExpungeFolderMaildir;
|
||||
#[cfg(all(feature = "folder-list", feature = "imap"))]
|
||||
use email::folder::list::imap::ListFoldersImap;
|
||||
#[cfg(all(feature = "folder-list", feature = "maildir"))]
|
||||
use email::folder::list::maildir::ListFoldersMaildir;
|
||||
#[cfg(all(feature = "folder-purge", feature = "imap"))]
|
||||
use email::folder::purge::imap::PurgeFolderImap;
|
||||
#[cfg(feature = "imap")]
|
||||
use email::imap::{ImapSessionBuilder, ImapSessionSync};
|
||||
#[cfg(feature = "sync")]
|
||||
use email::maildir::config::MaildirConfig;
|
||||
#[cfg(feature = "maildir")]
|
||||
use email::maildir::{MaildirSessionBuilder, MaildirSessionSync};
|
||||
#[cfg(all(feature = "message-add", feature = "maildir"))]
|
||||
use email::message::add_with_flags::maildir::AddMessageWithFlagsMaildir;
|
||||
#[cfg(all(feature = "message-copy", feature = "imap"))]
|
||||
use email::message::copy::imap::CopyMessagesImap;
|
||||
#[cfg(all(feature = "message-copy", feature = "maildir"))]
|
||||
use email::message::copy::maildir::CopyMessagesMaildir;
|
||||
#[cfg(all(feature = "message-get", feature = "imap"))]
|
||||
use email::message::get::imap::GetMessagesImap;
|
||||
#[cfg(all(feature = "message-move", feature = "imap"))]
|
||||
use email::message::move_::imap::MoveMessagesImap;
|
||||
#[cfg(all(feature = "message-move", feature = "maildir"))]
|
||||
use email::message::move_::maildir::MoveMessagesMaildir;
|
||||
#[cfg(all(feature = "message-peek", feature = "imap"))]
|
||||
use email::message::peek::imap::PeekMessagesImap;
|
||||
#[cfg(all(feature = "message-peek", feature = "maildir"))]
|
||||
use email::message::peek::maildir::PeekMessagesMaildir;
|
||||
#[cfg(any(feature = "message-peek", feature = "message-get"))]
|
||||
use email::message::Messages;
|
||||
#[cfg(all(feature = "message-add", feature = "imap"))]
|
||||
use email::message::{add::imap::AddMessageImap, add_with_flags::imap::AddMessageWithFlagsImap};
|
||||
#[cfg(feature = "sendmail")]
|
||||
use email::sendmail::SendmailContext;
|
||||
#[cfg(feature = "smtp")]
|
||||
use email::smtp::{SmtpClientBuilder, SmtpClientSync};
|
||||
|
||||
#[cfg(any(feature = "flag-command"))]
|
||||
use email::{
|
||||
account::config::AccountConfig,
|
||||
envelope::{
|
||||
get::{imap::GetEnvelopeImap, maildir::GetEnvelopeMaildir},
|
||||
list::{imap::ListEnvelopesImap, maildir::ListEnvelopesMaildir},
|
||||
watch::{imap::WatchImapEnvelopes, maildir::WatchMaildirEnvelopes},
|
||||
Id, SingleId,
|
||||
},
|
||||
flag::{
|
||||
add::{imap::AddFlagsImap, maildir::AddFlagsMaildir},
|
||||
remove::{imap::RemoveFlagsImap, maildir::RemoveFlagsMaildir},
|
||||
set::{imap::SetFlagsImap, maildir::SetFlagsMaildir},
|
||||
Flag, Flags,
|
||||
},
|
||||
folder::{
|
||||
add::{imap::AddFolderImap, maildir::AddFolderMaildir},
|
||||
delete::{imap::DeleteFolderImap, maildir::DeleteFolderMaildir},
|
||||
expunge::{imap::ExpungeFolderImap, maildir::ExpungeFolderMaildir},
|
||||
list::{imap::ListFoldersImap, maildir::ListFoldersMaildir},
|
||||
purge::imap::PurgeFolderImap,
|
||||
},
|
||||
maildir::{config::MaildirConfig, MaildirSessionBuilder, MaildirSessionSync},
|
||||
message::{
|
||||
add_raw::imap::AddRawMessageImap,
|
||||
add_raw_with_flags::{
|
||||
imap::AddRawMessageWithFlagsImap, maildir::AddRawMessageWithFlagsMaildir,
|
||||
},
|
||||
copy::{imap::CopyMessagesImap, maildir::CopyMessagesMaildir},
|
||||
get::imap::GetMessagesImap,
|
||||
move_::{imap::MoveMessagesImap, maildir::MoveMessagesMaildir},
|
||||
peek::{imap::PeekMessagesImap, maildir::PeekMessagesMaildir},
|
||||
Messages,
|
||||
},
|
||||
sendmail::SendmailContext,
|
||||
envelope::Id,
|
||||
flag::{Flag, Flags},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{account::config::TomlAccountConfig, cache::IdMapper, envelope::Envelopes};
|
||||
#[cfg(feature = "envelope-list")]
|
||||
use crate::envelope::Envelopes;
|
||||
use crate::{account::config::TomlAccountConfig, cache::IdMapper};
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum BackendKind {
|
||||
Maildir,
|
||||
#[serde(skip_deserializing)]
|
||||
MaildirForSync,
|
||||
#[cfg(feature = "imap")]
|
||||
Imap,
|
||||
#[cfg(feature = "maildir")]
|
||||
Maildir,
|
||||
#[cfg(feature = "sync")]
|
||||
#[serde(skip_deserializing)]
|
||||
MaildirForSync,
|
||||
#[cfg(feature = "notmuch")]
|
||||
Notmuch,
|
||||
#[cfg(feature = "smtp")]
|
||||
Smtp,
|
||||
#[cfg(feature = "sendmail")]
|
||||
Sendmail,
|
||||
None,
|
||||
}
|
||||
|
||||
impl ToString for BackendKind {
|
||||
fn to_string(&self) -> String {
|
||||
let kind = match self {
|
||||
Self::Maildir => "Maildir",
|
||||
Self::MaildirForSync => "Maildir",
|
||||
#[cfg(feature = "imap")]
|
||||
Self::Imap => "IMAP",
|
||||
#[cfg(feature = "maildir")]
|
||||
Self::Maildir => "Maildir",
|
||||
#[cfg(feature = "sync")]
|
||||
Self::MaildirForSync => "Maildir",
|
||||
#[cfg(feature = "notmuch")]
|
||||
Self::Notmuch => "Notmuch",
|
||||
#[cfg(feature = "smtp")]
|
||||
Self::Smtp => "SMTP",
|
||||
#[cfg(feature = "sendmail")]
|
||||
Self::Sendmail => "Sendmail",
|
||||
Self::None => "None",
|
||||
};
|
||||
|
||||
kind.to_string()
|
||||
@@ -83,34 +135,26 @@ impl ToString for BackendKind {
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct BackendContextBuilder {
|
||||
pub maildir: Option<MaildirSessionBuilder>,
|
||||
pub maildir_for_sync: Option<MaildirSessionBuilder>,
|
||||
#[cfg(feature = "imap")]
|
||||
pub imap: Option<ImapSessionBuilder>,
|
||||
#[cfg(feature = "maildir")]
|
||||
pub maildir: Option<MaildirSessionBuilder>,
|
||||
#[cfg(feature = "sync")]
|
||||
pub maildir_for_sync: Option<MaildirSessionBuilder>,
|
||||
#[cfg(feature = "smtp")]
|
||||
pub smtp: Option<SmtpClientBuilder>,
|
||||
#[cfg(feature = "sendmail")]
|
||||
pub sendmail: Option<SendmailContext>,
|
||||
}
|
||||
|
||||
impl BackendContextBuilder {
|
||||
#[allow(unused)]
|
||||
pub async fn new(
|
||||
toml_account_config: &TomlAccountConfig,
|
||||
account_config: &AccountConfig,
|
||||
kinds: Vec<&BackendKind>,
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
maildir: toml_account_config
|
||||
.maildir
|
||||
.as_ref()
|
||||
.filter(|_| kinds.contains(&&BackendKind::Maildir))
|
||||
.map(|mdir_config| {
|
||||
MaildirSessionBuilder::new(account_config.clone(), mdir_config.clone())
|
||||
}),
|
||||
maildir_for_sync: Some(MaildirConfig {
|
||||
root_dir: account_config.get_sync_dir()?,
|
||||
})
|
||||
.filter(|_| kinds.contains(&&BackendKind::MaildirForSync))
|
||||
.map(|mdir_config| MaildirSessionBuilder::new(account_config.clone(), mdir_config)),
|
||||
#[cfg(feature = "imap")]
|
||||
imap: {
|
||||
let ctx_builder = toml_account_config
|
||||
@@ -126,6 +170,20 @@ impl BackendContextBuilder {
|
||||
None => None,
|
||||
}
|
||||
},
|
||||
#[cfg(feature = "maildir")]
|
||||
maildir: toml_account_config
|
||||
.maildir
|
||||
.as_ref()
|
||||
.filter(|_| kinds.contains(&&BackendKind::Maildir))
|
||||
.map(|mdir_config| {
|
||||
MaildirSessionBuilder::new(account_config.clone(), mdir_config.clone())
|
||||
}),
|
||||
#[cfg(feature = "sync")]
|
||||
maildir_for_sync: Some(MaildirConfig {
|
||||
root_dir: account_config.get_sync_dir()?,
|
||||
})
|
||||
.filter(|_| kinds.contains(&&BackendKind::MaildirForSync))
|
||||
.map(|mdir_config| MaildirSessionBuilder::new(account_config.clone(), mdir_config)),
|
||||
#[cfg(feature = "notmuch")]
|
||||
notmuch: toml_account_config
|
||||
.notmuch
|
||||
@@ -159,21 +217,24 @@ impl email::backend::BackendContextBuilder for BackendContextBuilder {
|
||||
type Context = BackendContext;
|
||||
|
||||
async fn build(self) -> Result<Self::Context> {
|
||||
#[allow(unused_mut)]
|
||||
let mut ctx = BackendContext::default();
|
||||
|
||||
if let Some(maildir) = self.maildir {
|
||||
ctx.maildir = Some(maildir.build().await?);
|
||||
}
|
||||
|
||||
if let Some(maildir) = self.maildir_for_sync {
|
||||
ctx.maildir_for_sync = Some(maildir.build().await?);
|
||||
}
|
||||
|
||||
#[cfg(feature = "imap")]
|
||||
if let Some(imap) = self.imap {
|
||||
ctx.imap = Some(imap.build().await?);
|
||||
}
|
||||
|
||||
#[cfg(feature = "maildir")]
|
||||
if let Some(maildir) = self.maildir {
|
||||
ctx.maildir = Some(maildir.build().await?);
|
||||
}
|
||||
|
||||
#[cfg(feature = "sync")]
|
||||
if let Some(maildir) = self.maildir_for_sync {
|
||||
ctx.maildir_for_sync = Some(maildir.build().await?);
|
||||
}
|
||||
|
||||
#[cfg(feature = "notmuch")]
|
||||
if let Some(notmuch) = self.notmuch {
|
||||
ctx.notmuch = Some(notmuch.build().await?);
|
||||
@@ -184,6 +245,7 @@ impl email::backend::BackendContextBuilder for BackendContextBuilder {
|
||||
ctx.smtp = Some(smtp.build().await?);
|
||||
}
|
||||
|
||||
#[cfg(feature = "sendmail")]
|
||||
if let Some(sendmail) = self.sendmail {
|
||||
ctx.sendmail = Some(sendmail.build().await?);
|
||||
}
|
||||
@@ -194,12 +256,15 @@ impl email::backend::BackendContextBuilder for BackendContextBuilder {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct BackendContext {
|
||||
pub maildir: Option<MaildirSessionSync>,
|
||||
pub maildir_for_sync: Option<MaildirSessionSync>,
|
||||
#[cfg(feature = "imap")]
|
||||
pub imap: Option<ImapSessionSync>,
|
||||
#[cfg(feature = "maildir")]
|
||||
pub maildir: Option<MaildirSessionSync>,
|
||||
#[cfg(feature = "sync")]
|
||||
pub maildir_for_sync: Option<MaildirSessionSync>,
|
||||
#[cfg(feature = "smtp")]
|
||||
pub smtp: Option<SmtpClientSync>,
|
||||
#[cfg(feature = "sendmail")]
|
||||
pub sendmail: Option<SendmailContext>,
|
||||
}
|
||||
|
||||
@@ -213,27 +278,17 @@ impl BackendBuilder {
|
||||
toml_account_config: TomlAccountConfig,
|
||||
account_config: AccountConfig,
|
||||
) -> Result<Self> {
|
||||
#[allow(unused)]
|
||||
let used_backends = toml_account_config.get_used_backends();
|
||||
|
||||
let is_maildir_used = used_backends.contains(&BackendKind::Maildir);
|
||||
let is_maildir_for_sync_used = used_backends.contains(&BackendKind::MaildirForSync);
|
||||
#[cfg(feature = "imap")]
|
||||
let is_imap_used = used_backends.contains(&BackendKind::Imap);
|
||||
#[cfg(feature = "maildir")]
|
||||
let is_maildir_used = used_backends.contains(&BackendKind::Maildir);
|
||||
#[cfg(feature = "sync")]
|
||||
let is_maildir_for_sync_used = used_backends.contains(&BackendKind::MaildirForSync);
|
||||
|
||||
let backend_ctx_builder = BackendContextBuilder {
|
||||
maildir: toml_account_config
|
||||
.maildir
|
||||
.as_ref()
|
||||
.filter(|_| is_maildir_used)
|
||||
.map(|mdir_config| {
|
||||
MaildirSessionBuilder::new(account_config.clone(), mdir_config.clone())
|
||||
}),
|
||||
maildir_for_sync: Some(MaildirConfig {
|
||||
root_dir: account_config.get_sync_dir()?,
|
||||
})
|
||||
.filter(|_| is_maildir_for_sync_used)
|
||||
.map(|mdir_config| MaildirSessionBuilder::new(account_config.clone(), mdir_config)),
|
||||
|
||||
#[cfg(feature = "imap")]
|
||||
imap: {
|
||||
let ctx_builder = toml_account_config
|
||||
@@ -250,17 +305,35 @@ impl BackendBuilder {
|
||||
None => None,
|
||||
}
|
||||
},
|
||||
#[cfg(feature = "maildir")]
|
||||
maildir: toml_account_config
|
||||
.maildir
|
||||
.as_ref()
|
||||
.filter(|_| is_maildir_used)
|
||||
.map(|mdir_config| {
|
||||
MaildirSessionBuilder::new(account_config.clone(), mdir_config.clone())
|
||||
}),
|
||||
#[cfg(feature = "sync")]
|
||||
maildir_for_sync: Some(MaildirConfig {
|
||||
root_dir: account_config.get_sync_dir()?,
|
||||
})
|
||||
.filter(|_| is_maildir_for_sync_used)
|
||||
.map(|mdir_config| MaildirSessionBuilder::new(account_config.clone(), mdir_config)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
#[allow(unused_mut)]
|
||||
let mut backend_builder =
|
||||
email::backend::BackendBuilder::new(account_config.clone(), backend_ctx_builder);
|
||||
|
||||
#[cfg(feature = "folder-add")]
|
||||
match toml_account_config.add_folder_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder
|
||||
.with_add_folder(|ctx| ctx.maildir.as_ref().and_then(AddFolderMaildir::new));
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_add_folder(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -281,12 +354,15 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "folder-list")]
|
||||
match toml_account_config.list_folders_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_list_folders(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(ListFoldersMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_list_folders(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -308,12 +384,15 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "folder-expunge")]
|
||||
match toml_account_config.expunge_folder_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_expunge_folder(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(ExpungeFolderMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_expunge_folder(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -335,13 +414,16 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "folder-purge")]
|
||||
match toml_account_config.purge_folder_kind() {
|
||||
// TODO
|
||||
// #[cfg(feature = "maildir")]
|
||||
// Some(BackendKind::Maildir) => {
|
||||
// backend_builder = backend_builder
|
||||
// .with_purge_folder(|ctx| ctx.maildir.as_ref().and_then(PurgeFolderMaildir::new));
|
||||
// }
|
||||
// TODO
|
||||
// #[cfg(feature = "sync")]
|
||||
// Some(BackendKind::MaildirForSync) => {
|
||||
// backend_builder = backend_builder
|
||||
// .with_purge_folder(|ctx| ctx.maildir_for_sync.as_ref().and_then(PurgeFolderMaildir::new));
|
||||
@@ -360,12 +442,15 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "folder-delete")]
|
||||
match toml_account_config.delete_folder_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_delete_folder(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(DeleteFolderMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_delete_folder(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -387,12 +472,45 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match toml_account_config.backend {
|
||||
#[cfg(feature = "envelope-list")]
|
||||
match toml_account_config.list_envelopes_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_list_envelopes(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(ListEnvelopesMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_list_envelopes(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
.as_ref()
|
||||
.and_then(ListEnvelopesMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "imap")]
|
||||
Some(BackendKind::Imap) => {
|
||||
backend_builder = backend_builder
|
||||
.with_list_envelopes(|ctx| ctx.imap.as_ref().and_then(ListEnvelopesImap::new));
|
||||
}
|
||||
#[cfg(feature = "notmuch")]
|
||||
Some(BackendKind::Notmuch) => {
|
||||
backend_builder = backend_builder.with_list_envelopes(|ctx| {
|
||||
ctx.notmuch.as_ref().and_then(ListEnvelopesNotmuch::new)
|
||||
});
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "envelope-watch")]
|
||||
match toml_account_config.watch_envelopes_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_watch_envelopes(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(WatchMaildirEnvelopes::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_watch_envelopes(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -415,12 +533,15 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "envelope-get")]
|
||||
match toml_account_config.get_envelope_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_get_envelope(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(GetEnvelopeMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_get_envelope(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -442,38 +563,14 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match toml_account_config.list_envelopes_kind() {
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_list_envelopes(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(ListEnvelopesMaildir::new)
|
||||
});
|
||||
}
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_list_envelopes(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
.as_ref()
|
||||
.and_then(ListEnvelopesMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "imap")]
|
||||
Some(BackendKind::Imap) => {
|
||||
backend_builder = backend_builder
|
||||
.with_list_envelopes(|ctx| ctx.imap.as_ref().and_then(ListEnvelopesImap::new));
|
||||
}
|
||||
#[cfg(feature = "notmuch")]
|
||||
Some(BackendKind::Notmuch) => {
|
||||
backend_builder = backend_builder.with_list_envelopes(|ctx| {
|
||||
ctx.notmuch.as_ref().and_then(ListEnvelopesNotmuch::new)
|
||||
});
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-add")]
|
||||
match toml_account_config.add_flags_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder
|
||||
.with_add_flags(|ctx| ctx.maildir.as_ref().and_then(AddFlagsMaildir::new));
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_add_flags(|ctx| {
|
||||
ctx.maildir_for_sync.as_ref().and_then(AddFlagsMaildir::new)
|
||||
@@ -492,11 +589,14 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-set")]
|
||||
match toml_account_config.set_flags_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder
|
||||
.with_set_flags(|ctx| ctx.maildir.as_ref().and_then(SetFlagsMaildir::new));
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_set_flags(|ctx| {
|
||||
ctx.maildir_for_sync.as_ref().and_then(SetFlagsMaildir::new)
|
||||
@@ -515,12 +615,15 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-remove")]
|
||||
match toml_account_config.remove_flags_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_remove_flags(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(RemoveFlagsMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_remove_flags(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -542,29 +645,32 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match toml_account_config.add_raw_message_kind() {
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_add_raw_message_with_flags(|ctx| {
|
||||
ctx.maildir
|
||||
.as_ref()
|
||||
.and_then(AddRawMessageWithFlagsMaildir::new)
|
||||
});
|
||||
}
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_add_raw_message_with_flags(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
.as_ref()
|
||||
.and_then(AddRawMessageWithFlagsMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "message-add")]
|
||||
match toml_account_config.add_message_kind() {
|
||||
#[cfg(feature = "imap")]
|
||||
Some(BackendKind::Imap) => {
|
||||
backend_builder = backend_builder
|
||||
.with_add_raw_message(|ctx| ctx.imap.as_ref().and_then(AddRawMessageImap::new))
|
||||
.with_add_raw_message_with_flags(|ctx| {
|
||||
ctx.imap.as_ref().and_then(AddRawMessageWithFlagsImap::new)
|
||||
.with_add_message(|ctx| ctx.imap.as_ref().and_then(AddMessageImap::new))
|
||||
.with_add_message_with_flags(|ctx| {
|
||||
ctx.imap.as_ref().and_then(AddMessageWithFlagsImap::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_add_message_with_flags(|ctx| {
|
||||
ctx.maildir
|
||||
.as_ref()
|
||||
.and_then(AddMessageWithFlagsMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_add_message_with_flags(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
.as_ref()
|
||||
.and_then(AddMessageWithFlagsMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "notmuch")]
|
||||
Some(BackendKind::Notmuch) => {
|
||||
backend_builder = backend_builder.with_add_raw_message(|ctx| {
|
||||
@@ -574,12 +680,15 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-peek")]
|
||||
match toml_account_config.peek_messages_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_peek_messages(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(PeekMessagesMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_peek_messages(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -601,6 +710,7 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-get")]
|
||||
match toml_account_config.get_messages_kind() {
|
||||
#[cfg(feature = "imap")]
|
||||
Some(BackendKind::Imap) => {
|
||||
@@ -616,12 +726,15 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-copy")]
|
||||
match toml_account_config.copy_messages_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_copy_messages(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(CopyMessagesMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_copy_messages(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -643,12 +756,15 @@ impl BackendBuilder {
|
||||
_ => (),
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-move")]
|
||||
match toml_account_config.move_messages_kind() {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
backend_builder = backend_builder.with_move_messages(|ctx| {
|
||||
ctx.maildir.as_ref().and_then(MoveMessagesMaildir::new)
|
||||
});
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
backend_builder = backend_builder.with_move_messages(|ctx| {
|
||||
ctx.maildir_for_sync
|
||||
@@ -699,6 +815,7 @@ impl Into<email::backend::BackendBuilder<BackendContextBuilder>> for BackendBuil
|
||||
}
|
||||
|
||||
pub struct Backend {
|
||||
#[allow(unused)]
|
||||
toml_account_config: TomlAccountConfig,
|
||||
backend: email::backend::Backend<BackendContext>,
|
||||
}
|
||||
@@ -724,14 +841,17 @@ impl Backend {
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn build_id_mapper(
|
||||
&self,
|
||||
folder: &str,
|
||||
backend_kind: Option<&BackendKind>,
|
||||
) -> Result<IdMapper> {
|
||||
#[allow(unused_mut)]
|
||||
let mut id_mapper = IdMapper::Dummy;
|
||||
|
||||
match backend_kind {
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(BackendKind::Maildir) => {
|
||||
if let Some(mdir_config) = &self.toml_account_config.maildir {
|
||||
id_mapper = IdMapper::new(
|
||||
@@ -741,6 +861,7 @@ impl Backend {
|
||||
)?;
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "sync")]
|
||||
Some(BackendKind::MaildirForSync) => {
|
||||
id_mapper = IdMapper::new(
|
||||
&self.backend.account_config,
|
||||
@@ -764,6 +885,7 @@ impl Backend {
|
||||
Ok(id_mapper)
|
||||
}
|
||||
|
||||
#[cfg(feature = "envelope-list")]
|
||||
pub async fn list_envelopes(
|
||||
&self,
|
||||
folder: &str,
|
||||
@@ -777,6 +899,7 @@ impl Backend {
|
||||
Ok(envelopes)
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-add")]
|
||||
pub async fn add_flags(&self, folder: &str, ids: &[usize], flags: &Flags) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.add_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
@@ -784,6 +907,15 @@ impl Backend {
|
||||
self.backend.add_flags(folder, &ids, flags).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-add")]
|
||||
pub async fn add_flag(&self, folder: &str, ids: &[usize], flag: Flag) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.add_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let ids = Id::multiple(id_mapper.get_ids(ids)?);
|
||||
self.backend.add_flag(folder, &ids, flag).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-set")]
|
||||
pub async fn set_flags(&self, folder: &str, ids: &[usize], flags: &Flags) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.set_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
@@ -791,6 +923,15 @@ impl Backend {
|
||||
self.backend.set_flags(folder, &ids, flags).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-set")]
|
||||
pub async fn set_flag(&self, folder: &str, ids: &[usize], flag: Flag) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.set_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let ids = Id::multiple(id_mapper.get_ids(ids)?);
|
||||
self.backend.set_flag(folder, &ids, flag).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-remove")]
|
||||
pub async fn remove_flags(&self, folder: &str, ids: &[usize], flags: &Flags) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.remove_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
@@ -798,6 +939,24 @@ impl Backend {
|
||||
self.backend.remove_flags(folder, &ids, flags).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "flag-remove")]
|
||||
pub async fn remove_flag(&self, folder: &str, ids: &[usize], flag: Flag) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.remove_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let ids = Id::multiple(id_mapper.get_ids(ids)?);
|
||||
self.backend.remove_flag(folder, &ids, flag).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-add")]
|
||||
pub async fn add_message(&self, folder: &str, email: &[u8]) -> Result<SingleId> {
|
||||
let backend_kind = self.toml_account_config.add_message_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let id = self.backend.add_message(folder, email).await?;
|
||||
id_mapper.create_alias(&*id)?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-peek")]
|
||||
pub async fn peek_messages(&self, folder: &str, ids: &[usize]) -> Result<Messages> {
|
||||
let backend_kind = self.toml_account_config.get_messages_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
@@ -805,6 +964,7 @@ impl Backend {
|
||||
self.backend.peek_messages(folder, &ids).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-get")]
|
||||
pub async fn get_messages(&self, folder: &str, ids: &[usize]) -> Result<Messages> {
|
||||
let backend_kind = self.toml_account_config.get_messages_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
@@ -812,6 +972,7 @@ impl Backend {
|
||||
self.backend.get_messages(folder, &ids).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-copy")]
|
||||
pub async fn copy_messages(
|
||||
&self,
|
||||
from_folder: &str,
|
||||
@@ -826,6 +987,7 @@ impl Backend {
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-move")]
|
||||
pub async fn move_messages(
|
||||
&self,
|
||||
from_folder: &str,
|
||||
@@ -840,41 +1002,13 @@ impl Backend {
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(feature = "message-delete")]
|
||||
pub async fn delete_messages(&self, folder: &str, ids: &[usize]) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.delete_messages_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let ids = Id::multiple(id_mapper.get_ids(ids)?);
|
||||
self.backend.delete_messages(folder, &ids).await
|
||||
}
|
||||
|
||||
pub async fn add_raw_message(&self, folder: &str, email: &[u8]) -> Result<SingleId> {
|
||||
let backend_kind = self.toml_account_config.add_raw_message_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let id = self.backend.add_raw_message(folder, email).await?;
|
||||
id_mapper.create_alias(&*id)?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn add_flag(&self, folder: &str, ids: &[usize], flag: Flag) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.add_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let ids = Id::multiple(id_mapper.get_ids(ids)?);
|
||||
self.backend.add_flag(folder, &ids, flag).await
|
||||
}
|
||||
|
||||
pub async fn set_flag(&self, folder: &str, ids: &[usize], flag: Flag) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.set_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let ids = Id::multiple(id_mapper.get_ids(ids)?);
|
||||
self.backend.set_flag(folder, &ids, flag).await
|
||||
}
|
||||
|
||||
pub async fn remove_flag(&self, folder: &str, ids: &[usize], flag: Flag) -> Result<()> {
|
||||
let backend_kind = self.toml_account_config.remove_flags_kind();
|
||||
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
|
||||
let ids = Id::multiple(id_mapper.get_ids(ids)?);
|
||||
self.backend.remove_flag(folder, &ids, flag).await
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Backend {
|
||||
|
||||
+17
-6
@@ -1,19 +1,24 @@
|
||||
use anyhow::Result;
|
||||
use dialoguer::Select;
|
||||
|
||||
use crate::config::wizard::THEME;
|
||||
#[cfg(feature = "imap")]
|
||||
use crate::imap;
|
||||
#[cfg(feature = "maildir")]
|
||||
use crate::maildir;
|
||||
#[cfg(feature = "notmuch")]
|
||||
use crate::notmuch;
|
||||
#[cfg(feature = "sendmail")]
|
||||
use crate::sendmail;
|
||||
#[cfg(feature = "smtp")]
|
||||
use crate::smtp;
|
||||
use crate::{config::wizard::THEME, maildir, sendmail};
|
||||
|
||||
use super::{config::BackendConfig, BackendKind};
|
||||
|
||||
const DEFAULT_BACKEND_KINDS: &[BackendKind] = &[
|
||||
#[cfg(feature = "imap")]
|
||||
BackendKind::Imap,
|
||||
#[cfg(feature = "maildir")]
|
||||
BackendKind::Maildir,
|
||||
#[cfg(feature = "notmuch")]
|
||||
BackendKind::Notmuch,
|
||||
@@ -22,10 +27,14 @@ const DEFAULT_BACKEND_KINDS: &[BackendKind] = &[
|
||||
const SEND_MESSAGE_BACKEND_KINDS: &[BackendKind] = &[
|
||||
#[cfg(feature = "smtp")]
|
||||
BackendKind::Smtp,
|
||||
#[cfg(feature = "sendmail")]
|
||||
BackendKind::Sendmail,
|
||||
];
|
||||
|
||||
pub(crate) async fn configure(account_name: &str, email: &str) -> Result<Option<BackendConfig>> {
|
||||
pub(crate) async fn configure(
|
||||
#[allow(unused)] account_name: &str,
|
||||
#[allow(unused)] email: &str,
|
||||
) -> Result<Option<BackendConfig>> {
|
||||
let kind = Select::with_theme(&*THEME)
|
||||
.with_prompt("Default email backend")
|
||||
.items(DEFAULT_BACKEND_KINDS)
|
||||
@@ -34,11 +43,12 @@ pub(crate) async fn configure(account_name: &str, email: &str) -> Result<Option<
|
||||
.and_then(|idx| DEFAULT_BACKEND_KINDS.get(idx).map(Clone::clone));
|
||||
|
||||
let config = match kind {
|
||||
Some(kind) if kind == BackendKind::Maildir => Some(maildir::wizard::configure()?),
|
||||
#[cfg(feature = "imap")]
|
||||
Some(kind) if kind == BackendKind::Imap => {
|
||||
Some(imap::wizard::configure(account_name, email).await?)
|
||||
}
|
||||
#[cfg(feature = "maildir")]
|
||||
Some(kind) if kind == BackendKind::Maildir => Some(maildir::wizard::configure()?),
|
||||
#[cfg(feature = "notmuch")]
|
||||
Some(kind) if kind == BackendKind::Notmuch => Some(notmuch::wizard::configure()?),
|
||||
_ => None,
|
||||
@@ -48,8 +58,8 @@ pub(crate) async fn configure(account_name: &str, email: &str) -> Result<Option<
|
||||
}
|
||||
|
||||
pub(crate) async fn configure_sender(
|
||||
account_name: &str,
|
||||
email: &str,
|
||||
#[allow(unused)] account_name: &str,
|
||||
#[allow(unused)] email: &str,
|
||||
) -> Result<Option<BackendConfig>> {
|
||||
let kind = Select::with_theme(&*THEME)
|
||||
.with_prompt("Backend for sending messages")
|
||||
@@ -59,11 +69,12 @@ pub(crate) async fn configure_sender(
|
||||
.and_then(|idx| SEND_MESSAGE_BACKEND_KINDS.get(idx).map(Clone::clone));
|
||||
|
||||
let config = match kind {
|
||||
Some(kind) if kind == BackendKind::Sendmail => Some(sendmail::wizard::configure()?),
|
||||
#[cfg(feature = "smtp")]
|
||||
Some(kind) if kind == BackendKind::Smtp => {
|
||||
Some(smtp::wizard::configure(account_name, email).await?)
|
||||
}
|
||||
#[cfg(feature = "sendmail")]
|
||||
Some(kind) if kind == BackendKind::Sendmail => Some(sendmail::wizard::configure()?),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user