add new() method for Account and Config

This commit is contained in:
Clément DOUIN 2021-06-02 22:50:22 +02:00
parent 4df39b6d01
commit fa223417a0
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
4 changed files with 69 additions and 197 deletions

View file

@ -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<PathBuf> {
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<PathBuf>) -> Result<Self> {
pub fn from_path(path: Option<PathBuf>) -> Result<Self> {
let path = match path {
Some(path) => path,
None => Self::path_from_xdg()

View file

@ -61,7 +61,7 @@ fn run() -> Result<()> {
debug!("init config");
let custom_config: Option<PathBuf> = 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");

View file

@ -107,6 +107,17 @@ pub fn tpl_args<'a>() -> Vec<clap::Arg<'a, 'a>> {
]
}
pub fn tpl_matches(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
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<bool> {
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<bool> {
debug!("new command matched");

View file

@ -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();