From a8353b70e7344c6e05c1ca189a49175057318d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Wed, 2 Jun 2021 23:41:06 +0200 Subject: [PATCH] use default instead of new for Config, Account and Output --- src/config/model.rs | 78 ++++++++++++++++-------------- src/main.rs | 2 +- src/msg/tpl/model.rs | 112 +++++++++++++++++++++---------------------- src/output/model.rs | 8 ++++ 4 files changed, 106 insertions(+), 94 deletions(-) diff --git a/src/config/model.rs b/src/config/model.rs index 479bbb67..6ea36997 100644 --- a/src/config/model.rs +++ b/src/config/model.rs @@ -50,30 +50,6 @@ pub struct Account { } impl Account { - pub fn new() -> Self { - Self { - name: None, - downloads_dir: None, - signature: None, - default_page_size: None, - default: None, - email: String::new(), - watch_cmds: None, - imap_host: String::new(), - imap_port: 0, - imap_starttls: None, - imap_insecure: None, - imap_login: String::new(), - imap_passwd_cmd: String::new(), - smtp_host: String::new(), - smtp_port: 0, - smtp_starttls: None, - smtp_insecure: None, - smtp_login: String::new(), - smtp_passwd_cmd: String::new(), - } - } - pub fn imap_addr(&self) -> (&str, u16) { debug!("host: {}", self.imap_host); debug!("port: {}", self.imap_port); @@ -133,6 +109,32 @@ impl Account { } } +impl Default for Account { + fn default() -> Self { + Self { + name: None, + downloads_dir: None, + signature: None, + default_page_size: None, + default: None, + email: String::new(), + watch_cmds: None, + imap_host: String::new(), + imap_port: 0, + imap_starttls: None, + imap_insecure: None, + imap_login: String::new(), + imap_passwd_cmd: String::new(), + smtp_host: String::new(), + smtp_port: 0, + smtp_starttls: None, + smtp_insecure: None, + smtp_login: String::new(), + smtp_passwd_cmd: String::new(), + } + } +} + // Config #[derive(Debug, Deserialize)] @@ -149,18 +151,6 @@ pub struct Config { } impl Config { - pub fn new() -> Self { - Self { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, - accounts: HashMap::new(), - } - } - fn path_from_xdg() -> Result { let path = env::var("XDG_CONFIG_HOME").chain_err(|| "Cannot find `XDG_CONFIG_HOME` env var")?; @@ -201,7 +191,7 @@ impl Config { Ok(path) } - pub fn from_path(path: Option) -> Result { + pub fn new(path: Option) -> Result { let path = match path { Some(path) => path, None => Self::path_from_xdg() @@ -300,3 +290,17 @@ impl Config { Ok(()) } } + +impl Default for Config { + fn default() -> Self { + Self { + name: String::new(), + downloads_dir: None, + notify_cmd: None, + signature: None, + default_page_size: None, + watch_cmds: None, + accounts: HashMap::new(), + } + } +} diff --git a/src/main.rs b/src/main.rs index 50a31cc9..9f0afd64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,7 @@ fn run() -> Result<()> { debug!("init config"); let custom_config: Option = arg_matches.value_of("config").map(|s| s.into()); debug!("custom config path: {:?}", custom_config); - let config = Config::from_path(custom_config)?; + let config = Config::new(custom_config)?; trace!("config: {:?}", config); let account_name = arg_matches.value_of("account"); diff --git a/src/msg/tpl/model.rs b/src/msg/tpl/model.rs index 3111e09f..4451c414 100644 --- a/src/msg/tpl/model.rs +++ b/src/msg/tpl/model.rs @@ -234,19 +234,19 @@ mod tests { let account = Account { name: Some(String::from("Test")), email: String::from("test@localhost"), - ..Account::new() + ..Account::default() }; let config = Config { accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), - ..Config::new() + ..Config::default() }; - let output = Output::new("plain"); - let mbox = String::new(); - let arg_matches = clap::ArgMatches::new(); - let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); - let tpl = Tpl::new(&app); + let output = Output::default(); + let mbox = String::default(); + let arg_matches = clap::ArgMatches::default(); + let ctx = Ctx::new(&config, &account, &output, &mbox, &arg_matches); + let tpl = Tpl::new(&ctx); assert_eq!( "From: Test \nTo: \nSubject: \n\n", @@ -260,19 +260,19 @@ mod tests { name: Some(String::from("Test")), email: String::from("test@localhost"), signature: Some(String::from("-- \nCordialement,")), - ..Account::new() + ..Account::default() }; let config = Config { accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), - ..Config::new() + ..Config::default() }; - let output = Output::new("plain"); - let mbox = String::new(); - let arg_matches = clap::ArgMatches::new(); - let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); - let tpl = Tpl::new(&app); + let output = Output::default(); + let mbox = String::default(); + let arg_matches = clap::ArgMatches::default(); + let ctx = Ctx::new(&config, &account, &output, &mbox, &arg_matches); + let tpl = Tpl::new(&ctx); assert_eq!( "From: Test \nTo: \nSubject: \n\n\n\n-- \nCordialement,", @@ -285,23 +285,23 @@ mod tests { let account = Account { name: Some(String::from("Test")), email: String::from("test@localhost"), - ..Account::new() + ..Account::default() }; let config = Config { accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), - ..Config::new() + ..Config::default() }; - let output = Output::new("plain"); - let mbox = String::new(); - let arg_matches = clap::ArgMatches::new(); - let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); + let output = Output::default(); + let mbox = String::default(); + let arg_matches = clap::ArgMatches::default(); + let ctx = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) .unwrap(); - let tpl = Tpl::reply(&app, &parsed_mail); + let tpl = Tpl::reply(&ctx, &parsed_mail); assert_eq!( "From: Test \nTo: Sender \nSubject: Re: Test\n\n>Hello, world!", @@ -315,23 +315,23 @@ mod tests { name: Some(String::from("Test")), email: String::from("test@localhost"), signature: Some(String::from("-- \nCordialement,")), - ..Account::new() + ..Account::default() }; let config = Config { accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), - ..Config::new() + ..Config::default() }; - let output = Output::new("plain"); - let mbox = String::new(); - let arg_matches = clap::ArgMatches::new(); - let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); + let output = Output::default(); + let mbox = String::default(); + let arg_matches = clap::ArgMatches::default(); + let ctx = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) .unwrap(); - let tpl = Tpl::reply(&app, &parsed_mail); + let tpl = Tpl::reply(&ctx, &parsed_mail); assert_eq!( "From: Test \nTo: Sender \nSubject: Re: Test\n\n>Hello, world!\n\n-- \nCordialement,", @@ -344,18 +344,18 @@ mod tests { let account = Account { name: Some(String::from("To")), email: String::from("to@localhost"), - ..Account::new() + ..Account::default() }; let config = Config { accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), - ..Config::new() + ..Config::default() }; - let output = Output::new("plain"); - let mbox = String::new(); - let arg_matches = clap::ArgMatches::new(); - let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); + let output = Output::default(); + let mbox = String::default(); + let arg_matches = clap::ArgMatches::default(); + let ctx = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Message-Id: 1\r Content-Type: text/plain\r @@ -367,7 +367,7 @@ Subject: Test\r Hello, world!", ) .unwrap(); - let tpl = Tpl::reply_all(&app, &parsed_mail); + let tpl = Tpl::reply_all(&ctx, &parsed_mail); assert_eq!( "From: To @@ -387,23 +387,23 @@ Subject: Re: Test name: Some(String::from("Test")), email: String::from("test@localhost"), signature: Some(String::from("-- \nCordialement,")), - ..Account::new() + ..Account::default() }; let config = Config { accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), - ..Config::new() + ..Config::default() }; - let output = Output::new("plain"); - let mbox = String::new(); - let arg_matches = clap::ArgMatches::new(); - let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); + let output = Output::default(); + let mbox = String::default(); + let arg_matches = clap::ArgMatches::default(); + let ctx = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) .unwrap(); - let tpl = Tpl::reply(&app, &parsed_mail); + let tpl = Tpl::reply(&ctx, &parsed_mail); assert_eq!( "From: Test \nTo: Sender \nSubject: Re: Test\n\n>Hello, world!\n\n-- \nCordialement,", @@ -416,23 +416,23 @@ Subject: Re: Test let account = Account { name: Some(String::from("Test")), email: String::from("test@localhost"), - ..Account::new() + ..Account::default() }; let config = Config { accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), - ..Config::new() + ..Config::default() }; - let output = Output::new("plain"); - let mbox = String::new(); - let arg_matches = clap::ArgMatches::new(); - let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); + let output = Output::default(); + let mbox = String::default(); + let arg_matches = clap::ArgMatches::default(); + let ctx = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) .unwrap(); - let tpl = Tpl::forward(&app, &parsed_mail); + let tpl = Tpl::forward(&ctx, &parsed_mail); assert_eq!( "From: Test \nTo: \nSubject: Fwd: Test\n\n-------- Forwarded Message --------\nHello, world!", @@ -446,23 +446,23 @@ Subject: Re: Test name: Some(String::from("Test")), email: String::from("test@localhost"), signature: Some(String::from("-- \nCordialement,")), - ..Account::new() + ..Account::default() }; let config = Config { accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), - ..Config::new() + ..Config::default() }; - let output = Output::new("plain"); - let mbox = String::new(); - let arg_matches = clap::ArgMatches::new(); - let app = Ctx::new(&config, &account, &output, &mbox, &arg_matches); + let output = Output::default(); + let mbox = String::default(); + let arg_matches = clap::ArgMatches::default(); + let ctx = Ctx::new(&config, &account, &output, &mbox, &arg_matches); let parsed_mail = mailparse::parse_mail( b"Content-Type: text/plain\r\nFrom: Sender \r\nSubject: Test\r\n\r\nHello, world!", ) .unwrap(); - let tpl = Tpl::forward(&app, &parsed_mail); + let tpl = Tpl::forward(&ctx, &parsed_mail); assert_eq!( "From: Test \nTo: \nSubject: Fwd: Test\n\n-------- Forwarded Message --------\nHello, world!\n\n-- \nCordialement,", diff --git a/src/output/model.rs b/src/output/model.rs index efff04fd..da6a08a1 100644 --- a/src/output/model.rs +++ b/src/output/model.rs @@ -65,3 +65,11 @@ impl Output { } } } + +impl Default for Output { + fn default() -> Self { + Self { + fmt: OutputFmt::Plain, + } + } +}