use default instead of new for Config, Account and Output

This commit is contained in:
Clément DOUIN 2021-06-02 23:41:06 +02:00
parent 6bca1a289b
commit a8353b70e7
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
4 changed files with 106 additions and 94 deletions

View file

@ -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<PathBuf> {
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<PathBuf>) -> Result<Self> {
pub fn new(path: Option<PathBuf>) -> Result<Self> {
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(),
}
}
}

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::from_path(custom_config)?;
let config = Config::new(custom_config)?;
trace!("config: {:?}", config);
let account_name = arg_matches.value_of("account");

View file

@ -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 <test@localhost>\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 <test@localhost>\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 <sender@localhost>\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 <test@localhost>\nTo: Sender <sender@localhost>\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 <sender@localhost>\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 <test@localhost>\nTo: Sender <sender@localhost>\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 <to@localhost>
@ -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 <sender@localhost>\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 <test@localhost>\nTo: Sender <sender@localhost>\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 <sender@localhost>\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 <test@localhost>\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 <sender@localhost>\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 <test@localhost>\nTo: \nSubject: Fwd: Test\n\n-------- Forwarded Message --------\nHello, world!\n\n-- \nCordialement,",

View file

@ -65,3 +65,11 @@ impl Output {
}
}
}
impl Default for Output {
fn default() -> Self {
Self {
fmt: OutputFmt::Plain,
}
}
}