diff --git a/src/config/model.rs b/src/config/model.rs index cc7a33a..479bbb6 100644 --- a/src/config/model.rs +++ b/src/config/model.rs @@ -50,6 +50,30 @@ 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); @@ -125,6 +149,18 @@ 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")?; @@ -165,7 +201,7 @@ impl Config { Ok(path) } - pub fn new(path: Option) -> Result { + pub fn from_path(path: Option) -> Result { let path = match path { Some(path) => path, None => Self::path_from_xdg() diff --git a/src/main.rs b/src/main.rs index 556eece..6dd1ed2 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::new(custom_config)?; + let config = Config::from_path(custom_config)?; trace!("config: {:?}", config); let account_name = arg_matches.value_of("account"); diff --git a/src/msg/tpl/cli.rs b/src/msg/tpl/cli.rs index 8e5b7f7..351a6d6 100644 --- a/src/msg/tpl/cli.rs +++ b/src/msg/tpl/cli.rs @@ -107,6 +107,17 @@ pub fn tpl_args<'a>() -> Vec> { ] } +pub fn tpl_matches(app: &App, matches: &clap::ArgMatches) -> Result { + match matches.subcommand() { + ("new", Some(matches)) => tpl_matches_new(app, matches), + ("reply", Some(matches)) => tpl_matches_reply(app, matches), + ("forward", Some(matches)) => tpl_matches_forward(app, matches), + + // TODO: find a way to show the help message for template subcommand + _ => Err("Subcommand not found".into()), + } +} + fn override_tpl_with_args(tpl: &mut Tpl, matches: &clap::ArgMatches) { if let Some(from) = matches.value_of("from") { debug!("overriden from: {:?}", from); @@ -165,17 +176,6 @@ fn override_tpl_with_args(tpl: &mut Tpl, matches: &clap::ArgMatches) { }; } -pub fn tpl_matches(app: &App, matches: &clap::ArgMatches) -> Result { - match matches.subcommand() { - ("new", Some(matches)) => tpl_matches_new(app, matches), - ("reply", Some(matches)) => tpl_matches_reply(app, matches), - ("forward", Some(matches)) => tpl_matches_forward(app, matches), - - // TODO: find a way to show the help message for template subcommand - _ => Err("Subcommand not found".into()), - } -} - fn tpl_matches_new(app: &App, matches: &clap::ArgMatches) -> Result { debug!("new command matched"); diff --git a/src/msg/tpl/model.rs b/src/msg/tpl/model.rs index e12e25d..08ae8ce 100644 --- a/src/msg/tpl/model.rs +++ b/src/msg/tpl/model.rs @@ -233,35 +233,14 @@ mod tests { fn new_tpl() { let account = Account { name: Some(String::from("Test")), - downloads_dir: None, - signature: None, - default_page_size: None, - default: Some(true), email: String::from("test@localhost"), - 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(), + ..Account::new() }; let config = Config { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), + ..Config::new() }; let output = Output::new("plain"); let mbox = String::new(); @@ -279,35 +258,15 @@ mod tests { fn new_tpl_with_signature() { let account = Account { name: Some(String::from("Test")), - downloads_dir: None, - signature: Some(String::from("-- \nCordialement,")), - default_page_size: None, - default: Some(true), email: String::from("test@localhost"), - 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(), + signature: Some(String::from("-- \nCordialement,")), + ..Account::new() }; let config = Config { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), + ..Config::new() }; let output = Output::new("plain"); let mbox = String::new(); @@ -325,35 +284,14 @@ mod tests { fn reply_tpl() { let account = Account { name: Some(String::from("Test")), - downloads_dir: None, - signature: None, - default_page_size: None, - default: Some(true), email: String::from("test@localhost"), - 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(), + ..Account::new() }; let config = Config { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), + ..Config::new() }; let output = Output::new("plain"); let mbox = String::new(); @@ -375,35 +313,15 @@ mod tests { fn reply_tpl_with_signature() { let account = Account { name: Some(String::from("Test")), - downloads_dir: None, - signature: Some(String::from("-- \nCordialement,")), - default_page_size: None, - default: Some(true), email: String::from("test@localhost"), - 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(), + signature: Some(String::from("-- \nCordialement,")), + ..Account::new() }; let config = Config { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), + ..Config::new() }; let output = Output::new("plain"); let mbox = String::new(); @@ -425,35 +343,14 @@ mod tests { fn reply_all_tpl() { let account = Account { name: Some(String::from("To")), - downloads_dir: None, - signature: None, - default_page_size: None, - default: Some(true), email: String::from("to@localhost"), - 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(), + ..Account::new() }; let config = Config { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), + ..Config::new() }; let output = Output::new("plain"); let mbox = String::new(); @@ -488,35 +385,15 @@ Subject: Re: Test fn reply_all_tpl_with_signature() { let account = Account { name: Some(String::from("Test")), - downloads_dir: None, - signature: Some(String::from("-- \nCordialement,")), - default_page_size: None, - default: Some(true), email: String::from("test@localhost"), - 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(), + signature: Some(String::from("-- \nCordialement,")), + ..Account::new() }; let config = Config { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), + ..Config::new() }; let output = Output::new("plain"); let mbox = String::new(); @@ -538,35 +415,14 @@ Subject: Re: Test fn forward_tpl() { let account = Account { name: Some(String::from("Test")), - downloads_dir: None, - signature: None, - default_page_size: None, - default: Some(true), email: String::from("test@localhost"), - 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(), + ..Account::new() }; let config = Config { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), + ..Config::new() }; let output = Output::new("plain"); let mbox = String::new(); @@ -588,35 +444,15 @@ Subject: Re: Test fn forward_tpl_with_signature() { let account = Account { name: Some(String::from("Test")), - downloads_dir: None, - signature: Some(String::from("-- \nCordialement,")), - default_page_size: None, - default: Some(true), email: String::from("test@localhost"), - 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(), + signature: Some(String::from("-- \nCordialement,")), + ..Account::new() }; let config = Config { - name: String::new(), - downloads_dir: None, - notify_cmd: None, - signature: None, - default_page_size: None, - watch_cmds: None, accounts: vec![(String::from("account"), account.clone())] .into_iter() .collect(), + ..Config::new() }; let output = Output::new("plain"); let mbox = String::new();