From 41a2f0269973561edfbb020ff32027b8d1817297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Wed, 29 Nov 2023 07:52:08 +0100 Subject: [PATCH] rename config and account config --- src/backend.rs | 12 ++++--- src/config/config.rs | 64 +++++++++++++++++----------------- src/config/wizard.rs | 6 ++-- src/domain/account/accounts.rs | 6 ++-- src/domain/account/config.rs | 4 +-- src/domain/account/handlers.rs | 14 ++++---- src/domain/account/wizard.rs | 6 ++-- src/main.rs | 13 +++---- 8 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index eef8843..9284a77 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -45,11 +45,13 @@ use email::{ }; use serde::{Deserialize, Serialize}; -use crate::{account::DeserializedAccountConfig, Envelopes, IdMapper}; +use crate::{account::TomlAccountConfig, Envelopes, IdMapper}; -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum BackendKind { + #[default] + None, Maildir, #[serde(skip_deserializing)] MaildirForSync, @@ -123,7 +125,7 @@ pub struct BackendContext { } pub struct Backend { - toml_account_config: DeserializedAccountConfig, + toml_account_config: TomlAccountConfig, backend: email::backend::Backend, } @@ -262,13 +264,13 @@ impl Deref for Backend { } pub struct BackendBuilder { - toml_account_config: DeserializedAccountConfig, + toml_account_config: TomlAccountConfig, builder: email::backend::BackendBuilder, } impl BackendBuilder { pub async fn new( - toml_account_config: DeserializedAccountConfig, + toml_account_config: TomlAccountConfig, account_config: AccountConfig, ) -> Result { let backend_ctx_builder = BackendContextBuilder { diff --git a/src/config/config.rs b/src/config/config.rs index c45f159..9c618d7 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -17,7 +17,7 @@ use std::{collections::HashMap, fs, path::PathBuf, process::exit}; use toml; use crate::{ - account::DeserializedAccountConfig, + account::TomlAccountConfig, backend::BackendKind, config::{prelude::*, wizard}, wizard_prompt, wizard_warn, @@ -26,7 +26,7 @@ use crate::{ /// Represents the user config file. #[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)] #[serde(rename_all = "kebab-case")] -pub struct DeserializedConfig { +pub struct TomlConfig { #[serde(alias = "name")] pub display_name: Option, pub signature_delim: Option, @@ -48,12 +48,12 @@ pub struct DeserializedConfig { pub email_hooks: Option, #[serde(flatten)] - pub accounts: HashMap, + pub accounts: HashMap, } -impl DeserializedConfig { +impl TomlConfig { /// Tries to create a config from an optional path. - pub async fn from_opt_path(path: Option<&str>) -> Result { + pub async fn from_maybe_path(path: Option<&str>) -> Result { debug!("path: {:?}", path); let config = if let Some(path) = path.map(PathBuf::from).or_else(Self::path) { @@ -108,7 +108,7 @@ impl DeserializedConfig { self, account_name: Option<&str>, disable_cache: bool, - ) -> Result<(DeserializedAccountConfig, AccountConfig)> { + ) -> Result<(TomlAccountConfig, AccountConfig)> { let (account_name, mut toml_account_config) = match account_name { Some("default") | Some("") | None => self .accounts @@ -229,10 +229,10 @@ mod tests { use super::*; - async fn make_config(config: &str) -> Result { + async fn make_config(config: &str) -> Result { let mut file = NamedTempFile::new().unwrap(); write!(file, "{}", config).unwrap(); - DeserializedConfig::from_opt_path(file.into_temp_path().to_str()).await + TomlConfig::from_maybe_path(file.into_temp_path().to_str()).await } #[tokio::test] @@ -515,18 +515,18 @@ mod tests { assert_eq!( config.unwrap(), - DeserializedConfig { + TomlConfig { accounts: HashMap::from_iter([( "account".into(), - DeserializedAccountConfig { + TomlAccountConfig { email: "test@localhost".into(), sender: SenderConfig::Sendmail(SendmailConfig { cmd: "/usr/sbin/sendmail".into() }), - ..DeserializedAccountConfig::default() + ..TomlAccountConfig::default() } )]), - ..DeserializedConfig::default() + ..TomlConfig::default() } ) } @@ -551,10 +551,10 @@ mod tests { assert_eq!( config.unwrap(), - DeserializedConfig { + TomlConfig { accounts: HashMap::from_iter([( "account".into(), - DeserializedAccountConfig { + TomlAccountConfig { email: "test@localhost".into(), sender: SenderConfig::Smtp(SmtpConfig { host: "localhost".into(), @@ -565,10 +565,10 @@ mod tests { }), ..SmtpConfig::default() }), - ..DeserializedAccountConfig::default() + ..TomlAccountConfig::default() } )]), - ..DeserializedConfig::default() + ..TomlConfig::default() } ) } @@ -586,18 +586,18 @@ mod tests { assert_eq!( config.unwrap(), - DeserializedConfig { + TomlConfig { accounts: HashMap::from_iter([( "account".into(), - DeserializedAccountConfig { + TomlAccountConfig { email: "test@localhost".into(), sender: SenderConfig::Sendmail(SendmailConfig { cmd: Cmd::from("echo send") }), - ..DeserializedAccountConfig::default() + ..TomlAccountConfig::default() } )]), - ..DeserializedConfig::default() + ..TomlConfig::default() } ) } @@ -619,10 +619,10 @@ mod tests { assert_eq!( config.unwrap(), - DeserializedConfig { + TomlConfig { accounts: HashMap::from_iter([( "account".into(), - DeserializedAccountConfig { + TomlAccountConfig { email: "test@localhost".into(), backend: BackendConfig::Imap(ImapConfig { host: "localhost".into(), @@ -633,10 +633,10 @@ mod tests { }), ..ImapConfig::default() }), - ..DeserializedAccountConfig::default() + ..TomlAccountConfig::default() } )]), - ..DeserializedConfig::default() + ..TomlConfig::default() } ) } @@ -654,18 +654,18 @@ mod tests { assert_eq!( config.unwrap(), - DeserializedConfig { + TomlConfig { accounts: HashMap::from_iter([( "account".into(), - DeserializedAccountConfig { + TomlAccountConfig { email: "test@localhost".into(), backend: BackendConfig::Maildir(MaildirConfig { root_dir: "/tmp/maildir".into(), }), - ..DeserializedAccountConfig::default() + ..TomlAccountConfig::default() } )]), - ..DeserializedConfig::default() + ..TomlConfig::default() } ) } @@ -684,18 +684,18 @@ mod tests { assert_eq!( config.unwrap(), - DeserializedConfig { + TomlConfig { accounts: HashMap::from_iter([( "account".into(), - DeserializedAccountConfig { + TomlAccountConfig { email: "test@localhost".into(), backend: BackendConfig::Notmuch(NotmuchConfig { db_path: "/tmp/notmuch.db".into(), }), - ..DeserializedAccountConfig::default() + ..TomlAccountConfig::default() } )]), - ..DeserializedConfig::default() + ..TomlConfig::default() } ); } diff --git a/src/config/wizard.rs b/src/config/wizard.rs index f185f53..f6add5e 100644 --- a/src/config/wizard.rs +++ b/src/config/wizard.rs @@ -1,4 +1,4 @@ -use super::DeserializedConfig; +use super::TomlConfig; use crate::account; use anyhow::Result; use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password, Select}; @@ -31,10 +31,10 @@ macro_rules! wizard_log { pub(crate) static THEME: Lazy = Lazy::new(ColorfulTheme::default); -pub(crate) async fn configure() -> Result { +pub(crate) async fn configure() -> Result { wizard_log!("Configuring your first account:"); - let mut config = DeserializedConfig::default(); + let mut config = TomlConfig::default(); while let Some((name, account_config)) = account::wizard::configure().await? { config.accounts.insert(name, account_config); diff --git a/src/domain/account/accounts.rs b/src/domain/account/accounts.rs index c1e9665..59a4579 100644 --- a/src/domain/account/accounts.rs +++ b/src/domain/account/accounts.rs @@ -13,7 +13,7 @@ use crate::{ ui::Table, }; -use super::{Account, DeserializedAccountConfig}; +use super::{Account, TomlAccountConfig}; /// Represents the list of printable accounts. #[derive(Debug, Default, Serialize)] @@ -36,8 +36,8 @@ impl PrintTable for Accounts { } } -impl From> for Accounts { - fn from(map: Iter<'_, String, DeserializedAccountConfig>) -> Self { +impl From> for Accounts { + fn from(map: Iter<'_, String, TomlAccountConfig>) -> Self { let mut accounts: Vec<_> = map .map(|(name, account)| { let mut backends = String::new(); diff --git a/src/domain/account/config.rs b/src/domain/account/config.rs index df915e7..f64396f 100644 --- a/src/domain/account/config.rs +++ b/src/domain/account/config.rs @@ -29,7 +29,7 @@ use crate::{ /// Represents all existing kind of account config. #[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)] #[serde(tag = "backend", rename_all = "kebab-case")] -pub struct DeserializedAccountConfig { +pub struct TomlAccountConfig { pub default: Option, pub email: String, @@ -87,7 +87,7 @@ pub struct DeserializedAccountConfig { pub pgp: Option, } -impl DeserializedAccountConfig { +impl TomlAccountConfig { pub fn add_folder_kind(&self) -> Option<&BackendKind> { self.folder .as_ref() diff --git a/src/domain/account/handlers.rs b/src/domain/account/handlers.rs index bec3fc3..29d5a5b 100644 --- a/src/domain/account/handlers.rs +++ b/src/domain/account/handlers.rs @@ -20,7 +20,7 @@ use crate::{ backend::BackendContextBuilder, config::{ wizard::{prompt_passwd, prompt_secret}, - DeserializedConfig, + TomlConfig, }, printer::{PrintTableOpts, Printer}, Accounts, @@ -120,7 +120,7 @@ pub async fn configure(config: &AccountConfig, reset: bool) -> Result<()> { pub fn list<'a, P: Printer>( max_width: Option, config: &AccountConfig, - deserialized_config: &DeserializedConfig, + deserialized_config: &TomlConfig, printer: &mut P, ) -> Result<()> { info!("entering the list accounts handler"); @@ -298,7 +298,7 @@ mod tests { use termcolor::ColorSpec; use crate::{ - account::DeserializedAccountConfig, + account::TomlAccountConfig, printer::{Print, PrintTable, WriteColor}, }; @@ -367,16 +367,16 @@ mod tests { let mut printer = PrinterServiceTest::default(); let config = AccountConfig::default(); - let deserialized_config = DeserializedConfig { + let deserialized_config = TomlConfig { accounts: HashMap::from_iter([( "account-1".into(), - DeserializedAccountConfig { + TomlAccountConfig { default: Some(true), backend: BackendConfig::Imap(ImapConfig::default()), - ..DeserializedAccountConfig::default() + ..TomlAccountConfig::default() }, )]), - ..DeserializedConfig::default() + ..TomlConfig::default() }; assert!(list(None, &config, &deserialized_config, &mut printer).is_ok()); diff --git a/src/domain/account/wizard.rs b/src/domain/account/wizard.rs index f64f394..84ff44e 100644 --- a/src/domain/account/wizard.rs +++ b/src/domain/account/wizard.rs @@ -4,10 +4,10 @@ use email_address::EmailAddress; use crate::config::wizard::THEME; -use super::DeserializedAccountConfig; +use super::TomlAccountConfig; -pub(crate) async fn configure() -> Result> { - let mut config = DeserializedAccountConfig::default(); +pub(crate) async fn configure() -> Result> { + let mut config = TomlAccountConfig::default(); let account_name = Input::with_theme(&*THEME) .with_prompt("Account name") diff --git a/src/main.rs b/src/main.rs index 43c6fcb..2cd9433 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use himalaya::{ account, backend::BackendBuilder, cache, compl, - config::{self, DeserializedConfig}, + config::{self, TomlConfig}, email, flag, folder, man, output, printer::StdoutPrinter, tpl, @@ -53,11 +53,11 @@ async fn main() -> Result<()> { let default_env_filter = env_logger::DEFAULT_FILTER_ENV; env_logger::init_from_env(env_logger::Env::default().filter_or(default_env_filter, "off")); - // checks mailto command before app initialization + // check mailto command before app initialization let raw_args: Vec = env::args().collect(); if raw_args.len() > 1 && raw_args[1].starts_with("mailto:") { let url = Url::parse(&raw_args[1])?; - let (toml_account_config, account_config) = DeserializedConfig::from_opt_path(None) + let (toml_account_config, account_config) = TomlConfig::from_maybe_path(None) .await? .into_account_configs(None, false)?; let backend_builder = @@ -88,11 +88,12 @@ async fn main() -> Result<()> { _ => (), } - let folder = folder::args::parse_source_arg(&m); - let disable_cache = cache::args::parse_disable_cache_flag(&m); + let maybe_config_path = config::args::parse_arg(&m); let maybe_account_name = account::args::parse_arg(&m); + let disable_cache = cache::args::parse_disable_cache_flag(&m); + let folder = folder::args::parse_source_arg(&m); - let toml_config = DeserializedConfig::from_opt_path(config::args::parse_arg(&m)).await?; + let toml_config = TomlConfig::from_maybe_path(maybe_config_path).await?; let mut printer = StdoutPrinter::try_from(&m)?;