diff --git a/src/app.rs b/src/app.rs index 8faa774..a39b2aa 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,18 +1,20 @@ -use clap::{self, Arg, SubCommand}; +use clap::{self, Arg}; use error_chain::error_chain; use std::env; use crate::{ flag::cli::{flag_matches, flag_subcmds}, + imap::cli::{imap_matches, imap_subcmds}, mbox::cli::{mbox_arg, mbox_matches, mbox_subcmds}, msg::cli::{msg_matches, msg_subcmds}, }; error_chain! { links { + FlagCli(crate::flag::cli::Error, crate::flag::cli::ErrorKind); + ImapCli(crate::imap::cli::Error, crate::imap::cli::ErrorKind); MboxCli(crate::mbox::cli::Error, crate::mbox::cli::ErrorKind); MsgCli(crate::msg::cli::Error, crate::msg::cli::ErrorKind); - FlagCli(crate::flag::cli::Error, crate::flag::cli::ErrorKind); } } @@ -41,11 +43,10 @@ impl<'a> App<'a> { .value_name("STRING"), ) .arg(mbox_arg()) - .subcommand(SubCommand::with_name("idle").about("Spawns a blocking idle daemon")); - - let app = app.subcommands(mbox_subcmds()); - let app = app.subcommands(flag_subcmds()); - let app = app.subcommands(msg_subcmds()); + .subcommands(flag_subcmds()) + .subcommands(imap_subcmds()) + .subcommands(mbox_subcmds()) + .subcommands(msg_subcmds()); Self(app) } @@ -62,18 +63,14 @@ impl<'a> App<'a> { break; } + if imap_matches(&matches)? { + break; + } + msg_matches(&matches)?; break; } - // if let Some(matches) = matches.subcommand_matches("idle") { - // let config = Config::new_from_file()?; - // let account = config.find_account_by_name(account)?; - // let mut imap_conn = ImapConnector::new(&account)?; - // let mbox = matches.value_of("mailbox").unwrap(); - // imap_conn.idle(&config, &mbox)?; - // } - Ok(()) } } diff --git a/src/flag/cli.rs b/src/flag/cli.rs index 125ded8..f2cd420 100644 --- a/src/flag/cli.rs +++ b/src/flag/cli.rs @@ -1,15 +1,12 @@ use clap::{App, Arg, ArgMatches, SubCommand}; use error_chain::error_chain; -use crate::{ - config::{self, Config}, - imap::{self, ImapConnector}, -}; +use crate::{config::Config, imap::model::ImapConnector}; error_chain! { links { - Config(config::Error, config::ErrorKind); - Imap(imap::Error, imap::ErrorKind); + Config(crate::config::Error, crate::config::ErrorKind); + Imap(crate::imap::model::Error, crate::imap::model::ErrorKind); } } @@ -58,7 +55,6 @@ pub fn flag_subcmds<'a>() -> Vec> { pub fn flag_matches(matches: &ArgMatches) -> Result { let config = Config::new_from_file()?; let account = config.find_account_by_name(matches.value_of("account"))?; - let output_fmt = matches.value_of("output").unwrap(); let mbox = matches.value_of("mailbox").unwrap(); let mut imap_conn = ImapConnector::new(&account)?; diff --git a/src/imap/cli.rs b/src/imap/cli.rs new file mode 100644 index 0000000..3d8040a --- /dev/null +++ b/src/imap/cli.rs @@ -0,0 +1,30 @@ +use clap::{self, App, ArgMatches, SubCommand}; +use error_chain::error_chain; + +use crate::{config::Config, imap::model::ImapConnector}; + +error_chain! { + links { + Config(crate::config::Error, crate::config::ErrorKind); + Imap(crate::imap::model::Error, crate::imap::model::ErrorKind); + } +} + +pub fn imap_subcmds<'a>() -> Vec> { + vec![SubCommand::with_name("idle").about("Spawns a blocking idle daemon")] +} + +pub fn imap_matches(matches: &ArgMatches) -> Result { + let config = Config::new_from_file()?; + let account = config.find_account_by_name(matches.value_of("account"))?; + let mut imap_conn = ImapConnector::new(&account)?; + let mbox = matches.value_of("mailbox").unwrap(); + + if let Some(matches) = matches.subcommand_matches("idle") { + imap_conn.idle(&config, &mbox)?; + imap_conn.logout(); + return Ok(true); + } + + Ok(false) +} diff --git a/src/imap.rs b/src/imap/model.rs similarity index 100% rename from src/imap.rs rename to src/imap/model.rs diff --git a/src/main.rs b/src/main.rs index d9802a6..8497d0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,13 @@ mod app; mod config; -mod imap; mod input; mod output; mod smtp; mod table; +mod imap { + pub(crate) mod cli; + pub(crate) mod model; +} mod flag { pub(crate) mod cli; pub(crate) mod model; diff --git a/src/mbox/cli.rs b/src/mbox/cli.rs index 418b15d..e1b6d5d 100644 --- a/src/mbox/cli.rs +++ b/src/mbox/cli.rs @@ -1,12 +1,12 @@ use clap::{self, App, Arg, ArgMatches, SubCommand}; use error_chain::error_chain; -use crate::{config::Config, imap::ImapConnector, output::print}; +use crate::{config::Config, imap::model::ImapConnector, output::print}; error_chain! { links { Config(crate::config::Error, crate::config::ErrorKind); - Imap(crate::imap::Error, crate::imap::ErrorKind); + Imap(crate::imap::model::Error, crate::imap::model::ErrorKind); MsgCli(crate::msg::cli::Error, crate::msg::cli::ErrorKind); Output(crate::output::Error, crate::output::ErrorKind); } diff --git a/src/msg/cli.rs b/src/msg/cli.rs index 58ad02f..a7557ba 100644 --- a/src/msg/cli.rs +++ b/src/msg/cli.rs @@ -4,7 +4,7 @@ use std::fs; use crate::{ config::Config, - imap::ImapConnector, + imap::model::ImapConnector, input, msg::model::{Attachments, Msg, Msgs, ReadableMsg}, output::print, @@ -14,7 +14,7 @@ use crate::{ error_chain! { links { Config(crate::config::Error, crate::config::ErrorKind); - Imap(crate::imap::Error, crate::imap::ErrorKind); + Imap(crate::imap::model::Error, crate::imap::model::ErrorKind); Input(crate::input::Error, crate::input::ErrorKind); MsgModel(crate::msg::model::Error, crate::msg::model::ErrorKind); Output(crate::output::Error, crate::output::ErrorKind);