refactor msg matches (#151)

* Refactor msg_matches() impl into several functions

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>

* Simplify impl of msg_matches()

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>

* Refactor to use msg_matches_list() impl for default case as well

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>

* Remove unidomatic return keyword

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2021-05-23 12:04:37 +02:00 committed by GitHub
parent c9e1609ea9
commit 62ac9aef2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -172,18 +172,42 @@ pub fn msg_subcmds<'a>() -> Vec<clap::App<'a, 'a>> {
}
pub fn msg_matches(app: &App) -> Result<bool> {
if let Some(matches) = app.arg_matches.subcommand_matches("list") {
match app.arg_matches.subcommand() {
("attachments", Some(matches)) => msg_matches_attachments(app, matches),
("copy", Some(matches)) => msg_matches_copy(app, matches),
("delete", Some(matches)) => msg_matches_delete(app, matches),
("forward", Some(matches)) => msg_matches_forward(app, matches),
("move", Some(matches)) => msg_matches_move(app, matches),
("read", Some(matches)) => msg_matches_read(app, matches),
("reply", Some(matches)) => msg_matches_reply(app, matches),
("save", Some(matches)) => msg_matches_save(app, matches),
("search", Some(matches)) => msg_matches_search(app, matches),
("send", Some(matches)) => msg_matches_send(app, matches),
("template", Some(matches)) => msg_matches_template(app, matches),
("write", Some(matches)) => msg_matches_write(app, matches),
("list", opt_matches) => msg_matches_list(app, opt_matches),
(_other, opt_matches) => msg_matches_list(app, opt_matches),
}
}
fn msg_matches_list(app: &App, opt_matches: Option<&clap::ArgMatches>) -> Result<bool> {
debug!("list command matched");
let page_size: usize = matches
.value_of("page-size")
let page_size: usize = opt_matches
.and_then(|matches| {
matches.value_of("page-size")
.and_then(|s| s.parse().ok())
.unwrap_or(app.config.default_page_size(&app.account));
debug!("page size: {}", &page_size);
let page: usize = matches
.value_of("page")
})
.unwrap_or_else(|| app.config.default_page_size(&app.account));
debug!("page size: {:?}", page_size);
let page: usize = opt_matches
.and_then(|matches| {
matches.value_of("page")
.unwrap()
.parse()
.ok()
})
.unwrap_or_default();
debug!("page: {}", &page);
@ -199,10 +223,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
app.output.print(msgs);
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("search") {
fn msg_matches_search(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("search command matched");
let page_size: usize = matches
@ -254,10 +278,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
app.output.print(msgs);
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("read") {
fn msg_matches_read(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("read command matched");
let uid = matches.value_of("uid").unwrap();
@ -280,10 +304,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
}
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("attachments") {
fn msg_matches_attachments(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("attachments command matched");
let uid = matches.value_of("uid").unwrap();
@ -316,10 +340,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
));
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("write") {
fn msg_matches_write(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("write command matched");
let mut imap_conn = ImapConnector::new(&app.account)?;
@ -366,10 +390,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
}
}
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("reply") {
fn msg_matches_reply(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("reply command matched");
let uid = matches.value_of("uid").unwrap();
@ -429,10 +453,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
}
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("forward") {
fn msg_matches_forward(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("forward command matched");
let uid = matches.value_of("uid").unwrap();
@ -486,10 +510,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
}
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("template") {
fn msg_matches_template(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("template command matched");
if let Some(_) = matches.subcommand_matches("new") {
@ -533,10 +557,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
imap_conn.logout();
}
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("copy") {
fn msg_matches_copy(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("copy command matched");
let uid = matches.value_of("uid").unwrap();
@ -556,10 +580,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
));
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("move") {
fn msg_matches_move(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("move command matched");
let uid = matches.value_of("uid").unwrap();
@ -581,10 +605,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
imap_conn.expunge(&app.mbox)?;
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("delete") {
fn msg_matches_delete(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("delete command matched");
let uid = matches.value_of("uid").unwrap();
@ -598,10 +622,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
imap_conn.expunge(&app.mbox)?;
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("send") {
fn msg_matches_send(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("send command matched");
let mut imap_conn = ImapConnector::new(&app.account)?;
@ -627,10 +651,10 @@ pub fn msg_matches(app: &App) -> Result<bool> {
imap_conn.append_msg("Sent", &msg.formatted(), vec![Flag::Seen])?;
imap_conn.logout();
return Ok(true);
Ok(true)
}
if let Some(matches) = app.arg_matches.subcommand_matches("save") {
fn msg_matches_save(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
debug!("save command matched");
let mut imap_conn = ImapConnector::new(&app.account)?;
@ -638,24 +662,7 @@ pub fn msg_matches(app: &App) -> Result<bool> {
let msg = Msg::from(msg.to_string());
imap_conn.append_msg(&app.mbox, &msg.to_vec()?, vec![Flag::Seen])?;
imap_conn.logout();
return Ok(true);
}
{
debug!("default list command matched");
let mut imap_conn = ImapConnector::new(&app.account)?;
let msgs =
imap_conn.list_msgs(&app.mbox, &app.config.default_page_size(&app.account), &0)?;
let msgs = if let Some(ref fetches) = msgs {
Msgs::from(fetches)
} else {
Msgs::new()
};
app.output.print(msgs);
imap_conn.logout();
Ok(true)
}
}