remove account config from context builder new fn

This commit is contained in:
Clément DOUIN 2024-01-21 22:09:14 +01:00
parent 3137e1e851
commit 8cebdf9e90
No known key found for this signature in database
GPG key ID: 353E4A18EE0FAB72
2 changed files with 21 additions and 33 deletions

2
Cargo.lock generated
View file

@ -1217,7 +1217,7 @@ dependencies = [
[[package]] [[package]]
name = "email-lib" name = "email-lib"
version = "0.20.1" version = "0.20.1"
source = "git+https://git.sr.ht/~soywod/pimalaya#0454027ca9bfba17931e7414e4463dacf8c028f8" source = "git+https://git.sr.ht/~soywod/pimalaya#db699eb29b71e22bc74ccbb99003c3ac06816d69"
dependencies = [ dependencies = [
"advisory-lock", "advisory-lock",
"anyhow", "anyhow",

View file

@ -112,7 +112,7 @@ use email::message::Messages;
#[cfg(feature = "notmuch")] #[cfg(feature = "notmuch")]
use email::notmuch::{NotmuchContextBuilder, NotmuchContextSync}; use email::notmuch::{NotmuchContextBuilder, NotmuchContextSync};
#[cfg(feature = "sendmail")] #[cfg(feature = "sendmail")]
use email::sendmail::SendmailContext; use email::sendmail::{SendmailContext, SendmailContextBuilder};
#[cfg(feature = "smtp")] #[cfg(feature = "smtp")]
use email::smtp::{SmtpContextBuilder, SmtpContextSync}; use email::smtp::{SmtpContextBuilder, SmtpContextSync};
@ -181,7 +181,7 @@ pub struct BackendContextBuilder {
#[cfg(feature = "smtp")] #[cfg(feature = "smtp")]
pub smtp: Option<SmtpContextBuilder>, pub smtp: Option<SmtpContextBuilder>,
#[cfg(feature = "sendmail")] #[cfg(feature = "sendmail")]
pub sendmail: Option<SendmailContext>, pub sendmail: Option<SendmailContextBuilder>,
} }
impl BackendContextBuilder { impl BackendContextBuilder {
@ -198,9 +198,8 @@ impl BackendContextBuilder {
.imap .imap
.as_ref() .as_ref()
.filter(|_| kinds.contains(&&BackendKind::Imap)) .filter(|_| kinds.contains(&&BackendKind::Imap))
.map(|imap_config| { .map(|config| {
ImapContextBuilder::new(account_config.clone(), imap_config.clone()) ImapContextBuilder::new(config.clone()).with_prebuilt_credentials()
.with_prebuilt_credentials()
}); });
match ctx_builder { match ctx_builder {
Some(ctx_builder) => Some(ctx_builder.await?), Some(ctx_builder) => Some(ctx_builder.await?),
@ -212,39 +211,31 @@ impl BackendContextBuilder {
.maildir .maildir
.as_ref() .as_ref()
.filter(|_| kinds.contains(&&BackendKind::Maildir)) .filter(|_| kinds.contains(&&BackendKind::Maildir))
.map(|mdir_config| { .map(|config| MaildirContextBuilder::new(config.clone())),
MaildirContextBuilder::new(account_config.clone(), mdir_config.clone())
}),
#[cfg(feature = "account-sync")] #[cfg(feature = "account-sync")]
maildir_for_sync: Some(MaildirConfig { maildir_for_sync: Some(MaildirConfig {
root_dir: account_config.get_sync_dir()?, root_dir: account_config.get_sync_dir()?,
}) })
.filter(|_| kinds.contains(&&BackendKind::MaildirForSync)) .filter(|_| kinds.contains(&&BackendKind::MaildirForSync))
.map(|mdir_config| MaildirContextBuilder::new(account_config.clone(), mdir_config)), .map(|config| MaildirContextBuilder::new(config)),
#[cfg(feature = "notmuch")] #[cfg(feature = "notmuch")]
notmuch: toml_account_config notmuch: toml_account_config
.notmuch .notmuch
.as_ref() .as_ref()
.filter(|_| kinds.contains(&&BackendKind::Notmuch)) .filter(|_| kinds.contains(&&BackendKind::Notmuch))
.map(|notmuch_config| { .map(|config| NotmuchContextBuilder::new(config.clone())),
NotmuchContextBuilder::new(account_config.clone(), notmuch_config.clone())
}),
#[cfg(feature = "smtp")] #[cfg(feature = "smtp")]
smtp: toml_account_config smtp: toml_account_config
.smtp .smtp
.as_ref() .as_ref()
.filter(|_| kinds.contains(&&BackendKind::Smtp)) .filter(|_| kinds.contains(&&BackendKind::Smtp))
.map(|smtp_config| { .map(|config| SmtpContextBuilder::new(config.clone())),
SmtpContextBuilder::new(account_config.clone(), smtp_config.clone())
}),
#[cfg(feature = "sendmail")] #[cfg(feature = "sendmail")]
sendmail: toml_account_config sendmail: toml_account_config
.sendmail .sendmail
.as_ref() .as_ref()
.filter(|_| kinds.contains(&&BackendKind::Sendmail)) .filter(|_| kinds.contains(&&BackendKind::Sendmail))
.map(|sendmail_config| { .map(|config| SendmailContextBuilder::new(config.clone())),
SendmailContext::new(account_config.clone(), sendmail_config.clone())
}),
}) })
} }
} }
@ -253,38 +244,38 @@ impl BackendContextBuilder {
impl email::backend::BackendContextBuilder for BackendContextBuilder { impl email::backend::BackendContextBuilder for BackendContextBuilder {
type Context = BackendContext; type Context = BackendContext;
async fn build(self) -> Result<Self::Context> { async fn build(self, config: &AccountConfig) -> Result<Self::Context> {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut ctx = BackendContext::default(); let mut ctx = BackendContext::default();
#[cfg(feature = "imap")] #[cfg(feature = "imap")]
if let Some(imap) = self.imap { if let Some(imap) = self.imap {
ctx.imap = Some(imap.build().await?); ctx.imap = Some(imap.build(config).await?);
} }
#[cfg(feature = "maildir")] #[cfg(feature = "maildir")]
if let Some(maildir) = self.maildir { if let Some(maildir) = self.maildir {
ctx.maildir = Some(maildir.build().await?); ctx.maildir = Some(maildir.build(config).await?);
} }
#[cfg(feature = "account-sync")] #[cfg(feature = "account-sync")]
if let Some(maildir) = self.maildir_for_sync { if let Some(maildir) = self.maildir_for_sync {
ctx.maildir_for_sync = Some(maildir.build().await?); ctx.maildir_for_sync = Some(maildir.build(config).await?);
} }
#[cfg(feature = "notmuch")] #[cfg(feature = "notmuch")]
if let Some(notmuch) = self.notmuch { if let Some(notmuch) = self.notmuch {
ctx.notmuch = Some(notmuch.build().await?); ctx.notmuch = Some(notmuch.build(config).await?);
} }
#[cfg(feature = "smtp")] #[cfg(feature = "smtp")]
if let Some(smtp) = self.smtp { if let Some(smtp) = self.smtp {
ctx.smtp = Some(smtp.build().await?); ctx.smtp = Some(smtp.build(config).await?);
} }
#[cfg(feature = "sendmail")] #[cfg(feature = "sendmail")]
if let Some(sendmail) = self.sendmail { if let Some(sendmail) = self.sendmail {
ctx.sendmail = Some(sendmail.build().await?); ctx.sendmail = Some(sendmail.build(config).await?);
} }
Ok(ctx) Ok(ctx)
@ -334,9 +325,8 @@ impl BackendBuilder {
.imap .imap
.as_ref() .as_ref()
.filter(|_| is_imap_used) .filter(|_| is_imap_used)
.map(|imap_config| { .map(|config| {
ImapContextBuilder::new(account_config.clone(), imap_config.clone()) ImapContextBuilder::new(config.clone()).with_prebuilt_credentials()
.with_prebuilt_credentials()
}); });
match ctx_builder { match ctx_builder {
@ -349,15 +339,13 @@ impl BackendBuilder {
.maildir .maildir
.as_ref() .as_ref()
.filter(|_| is_maildir_used) .filter(|_| is_maildir_used)
.map(|mdir_config| { .map(|config| MaildirContextBuilder::new(config.clone())),
MaildirContextBuilder::new(account_config.clone(), mdir_config.clone())
}),
#[cfg(feature = "account-sync")] #[cfg(feature = "account-sync")]
maildir_for_sync: Some(MaildirConfig { maildir_for_sync: Some(MaildirConfig {
root_dir: account_config.get_sync_dir()?, root_dir: account_config.get_sync_dir()?,
}) })
.filter(|_| is_maildir_for_sync_used) .filter(|_| is_maildir_for_sync_used)
.map(|mdir_config| MaildirContextBuilder::new(account_config.clone(), mdir_config)), .map(|config| MaildirContextBuilder::new(config)),
..Default::default() ..Default::default()
}; };