Skip to content

Commit

Permalink
refactor new backend api
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Jan 27, 2024
1 parent 16266db commit 4f97059
Show file tree
Hide file tree
Showing 37 changed files with 749 additions and 1,827 deletions.
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,39 @@ default = [
# "pgp-native",
]

wizard = ["email-lib/account-discovery"]

imap = ["email-lib/imap"]
maildir = ["email-lib/maildir"]
notmuch = ["email-lib/notmuch"]
smtp = ["email-lib/smtp"]
sendmail = ["email-lib/sendmail"]

wizard = ["email-lib/account-discovery"]
account = ["account-configure", "account-list", "account-sync"]
account-subcmd = []
account-configure = ["account-subcmd"]
account-list = ["account-subcmd"]
account-sync = ["account-subcmd", "email-lib/sync"]
account-sync = ["account-subcmd", "email-lib/account-sync"]

folder = ["folder-add", "folder-list", "folder-expunge", "folder-purge", "folder-delete"]
folder-subcmd = []
folder-add = ["folder-subcmd", "email-lib/folder-add"]
folder-list = ["folder-subcmd", "email-lib/folder-list"]
folder-expunge = ["folder-subcmd", "email-lib/folder-expunge"]
folder-purge = ["folder-subcmd", "email-lib/folder-purge"]
folder-delete = ["folder-subcmd", "email-lib/folder-delete"]

envelope = ["envelope-list", "envelope-watch", "envelope-get"]
envelope-subcmd = []
envelope-list = ["envelope-subcmd", "email-lib/envelope-list"]
envelope-watch = ["envelope-subcmd", "email-lib/envelope-watch"]
envelope-get = ["envelope-subcmd", "email-lib/envelope-get"]

flag = ["flag-add", "flag-set", "flag-remove"]
flag-subcmd = []
flag-add = ["flag-subcmd", "email-lib/flag-add"]
flag-set = ["flag-subcmd", "email-lib/flag-set"]
flag-remove = ["flag-subcmd", "email-lib/flag-remove"]

message = ["message-read", "message-write", "message-mailto", "message-reply", "message-forward", "message-save", "message-send", "message-copy", "message-move", "message-delete"]
message-subcmd = []
message-add = ["email-lib/message-add"]
Expand All @@ -85,9 +88,11 @@ message-reply = ["message-get", "message-add", "message-send"]
message-forward = ["message-get", "message-add", "message-send"]
message-save = ["message-add"]
message-send = ["message-subcmd", "email-lib/message-send"]

attachment = ["attachment-download"]
attachment-subcmd = []
attachment-download = ["attachment-subcmd", "message-read"]

template = ["template-write", "template-reply", "template-forward", "template-save", "template-send"]
template-subcmd = []
template-write = ["template-subcmd"]
Expand Down Expand Up @@ -115,8 +120,7 @@ clap_mangen = "0.2"
console = "0.15.2"
dialoguer = "0.10.2"
dirs = "4.0"
# email-lib = { version = "=0.20.1", default-features = false }
email-lib = { git = "https://git.sr.ht/~soywod/pimalaya", default-features = false }
email-lib = { version = "=0.21.0", default-features = false }
email_address = "0.2.4"
env_logger = "0.8"
erased-serde = "0.3"
Expand Down
136 changes: 127 additions & 9 deletions src/account/command/sync.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
use crate::{
account::{arg::name::OptionalAccountNameArg, config::TomlAccountConfig},
backend::{Backend, BackendContextBuilder, BackendKind},
config::TomlConfig,
printer::Printer,
};
use anyhow::Result;
use clap::{ArgAction, Parser};
#[cfg(feature = "imap")]
use email::imap::ImapContextBuilder;
#[cfg(feature = "account-sync")]
use email::maildir::config::MaildirConfig;
#[cfg(feature = "maildir")]
use email::maildir::MaildirContextBuilder;
#[cfg(feature = "notmuch")]
use email::notmuch::NotmuchContextBuilder;
use email::{
account::sync::{AccountSyncBuilder, AccountSyncProgressEvent},
account::{
config::AccountConfig,
sync::{AccountSyncBuilder, AccountSyncProgressEvent},
},
backend::BackendBuilder,
folder::sync::FolderSyncStrategy,
};
use indicatif::{MultiProgress, ProgressBar, ProgressFinish, ProgressStyle};
use log::info;
use once_cell::sync::Lazy;
use std::{
collections::{HashMap, HashSet},
sync::Mutex,
};

use crate::{
account::arg::name::OptionalAccountNameArg, backend::BackendBuilder, config::TomlConfig,
printer::Printer,
ops::Deref,
sync::{Arc, Mutex},
};

static MAIN_PROGRESS_STYLE: Lazy<ProgressStyle> = Lazy::new(|| {
Expand Down Expand Up @@ -101,8 +115,8 @@ impl AccountSyncCommand {
let account_name = account_config.name.as_str();

let backend_builder =
BackendBuilder::new(toml_account_config, account_config.clone()).await?;
let sync_builder = AccountSyncBuilder::new(backend_builder.into())
AccountSyncBackendBuilder::new(toml_account_config, account_config.clone()).await?;
let sync_builder = AccountSyncBuilder::new(account_config.clone(), backend_builder.into())
.await?
.with_some_folders_strategy(strategy)
.with_dry_run(self.dry_run);
Expand Down Expand Up @@ -251,3 +265,107 @@ impl AccountSyncCommand {
Ok(())
}
}

pub struct AccountSyncBackendBuilder {
toml_account_config: Arc<TomlAccountConfig>,
builder: BackendBuilder<BackendContextBuilder>,
}

impl AccountSyncBackendBuilder {
pub async fn new(
toml_account_config: Arc<TomlAccountConfig>,
account_config: Arc<AccountConfig>,
) -> Result<Self> {
#[allow(unused)]
let used_backends = toml_account_config.get_used_backends();

#[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 = "account-sync")]
let is_maildir_for_sync_used = used_backends.contains(&BackendKind::MaildirForSync);
#[cfg(feature = "notmuch")]
let is_notmuch_used = used_backends.contains(&BackendKind::Notmuch);

let backend_ctx_builder = BackendContextBuilder {
toml_account_config: toml_account_config.clone(),
account_config: account_config.clone(),

#[cfg(feature = "imap")]
imap: {
let builder = toml_account_config
.imap
.as_ref()
.filter(|_| is_imap_used)
.map(Clone::clone)
.map(Arc::new)
.map(|config| ImapContextBuilder::new(config).with_prebuilt_credentials());
match builder {
Some(builder) => Some(builder.await?),
None => None,
}
},

#[cfg(feature = "maildir")]
maildir: toml_account_config
.maildir
.as_ref()
.filter(|_| is_maildir_used)
.map(Clone::clone)
.map(Arc::new)
.map(MaildirContextBuilder::new),

#[cfg(feature = "account-sync")]
maildir_for_sync: Some(MaildirConfig {
root_dir: account_config.get_sync_dir()?,
})
.filter(|_| is_maildir_for_sync_used)
.map(Arc::new)
.map(MaildirContextBuilder::new),

#[cfg(feature = "notmuch")]
notmuch: toml_account_config
.notmuch
.as_ref()
.filter(|_| is_notmuch_used)
.map(Clone::clone)
.map(Arc::new)
.map(NotmuchContextBuilder::new),

#[cfg(feature = "smtp")]
smtp: None,

#[cfg(feature = "sendmail")]
sendmail: None,
};

let backend_builder = BackendBuilder::new(account_config.clone(), backend_ctx_builder);

Ok(Self {
toml_account_config,
builder: backend_builder,
})
}

pub async fn build(self) -> Result<Backend> {
Ok(Backend {
toml_account_config: self.toml_account_config,
backend: self.builder.build().await?,
})
}
}

impl Deref for AccountSyncBackendBuilder {
type Target = BackendBuilder<BackendContextBuilder>;

fn deref(&self) -> &Self::Target {
&self.builder
}
}

impl From<AccountSyncBackendBuilder> for BackendBuilder<BackendContextBuilder> {
fn from(backend_builder: AccountSyncBackendBuilder) -> Self {
backend_builder.builder
}
}
26 changes: 13 additions & 13 deletions src/account/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct TomlAccountConfig {
}

impl TomlAccountConfig {
#[cfg(feature = "folder-add")]
#[cfg(any(feature = "account-sync", feature = "folder-add"))]
pub fn add_folder_kind(&self) -> Option<&BackendKind> {
self.folder
.as_ref()
Expand All @@ -69,7 +69,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "folder-list")]
#[cfg(any(feature = "account-sync", feature = "folder-list"))]
pub fn list_folders_kind(&self) -> Option<&BackendKind> {
self.folder
.as_ref()
Expand All @@ -78,7 +78,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "folder-expunge")]
#[cfg(any(feature = "account-sync", feature = "folder-expunge"))]
pub fn expunge_folder_kind(&self) -> Option<&BackendKind> {
self.folder
.as_ref()
Expand All @@ -96,7 +96,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "folder-delete")]
#[cfg(any(feature = "account-sync", feature = "folder-delete"))]
pub fn delete_folder_kind(&self) -> Option<&BackendKind> {
self.folder
.as_ref()
Expand All @@ -105,7 +105,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "envelope-get")]
#[cfg(any(feature = "account-sync", feature = "envelope-get"))]
pub fn get_envelope_kind(&self) -> Option<&BackendKind> {
self.envelope
.as_ref()
Expand All @@ -114,7 +114,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "envelope-list")]
#[cfg(any(feature = "account-sync", feature = "envelope-list"))]
pub fn list_envelopes_kind(&self) -> Option<&BackendKind> {
self.envelope
.as_ref()
Expand All @@ -132,7 +132,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "flag-add")]
#[cfg(any(feature = "account-sync", feature = "flag-add"))]
pub fn add_flags_kind(&self) -> Option<&BackendKind> {
self.flag
.as_ref()
Expand All @@ -141,7 +141,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "flag-set")]
#[cfg(any(feature = "account-sync", feature = "flag-set"))]
pub fn set_flags_kind(&self) -> Option<&BackendKind> {
self.flag
.as_ref()
Expand All @@ -159,7 +159,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "message-add")]
#[cfg(any(feature = "account-sync", feature = "message-add"))]
pub fn add_message_kind(&self) -> Option<&BackendKind> {
self.message
.as_ref()
Expand All @@ -168,7 +168,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "message-peek")]
#[cfg(any(feature = "account-sync", feature = "message-peek"))]
pub fn peek_messages_kind(&self) -> Option<&BackendKind> {
self.message
.as_ref()
Expand All @@ -177,7 +177,7 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "message-get")]
#[cfg(any(feature = "account-sync", feature = "message-get"))]
pub fn get_messages_kind(&self) -> Option<&BackendKind> {
self.message
.as_ref()
Expand All @@ -195,11 +195,11 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

#[cfg(feature = "message-move")]
#[cfg(any(feature = "account-sync", feature = "message-move"))]
pub fn move_messages_kind(&self) -> Option<&BackendKind> {
self.message
.as_ref()
.and_then(|message| message.move_.as_ref())
.and_then(|message| message.r#move.as_ref())
.and_then(|move_| move_.backend.as_ref())
.or(self.backend.as_ref())
}
Expand Down
Loading

0 comments on commit 4f97059

Please sign in to comment.