Skip to content

Commit

Permalink
refactor envelope with clap derive api
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Dec 6, 2023
1 parent 4a77253 commit 2c33dd2
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 642 deletions.
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
account::command::AccountSubcommand,
completion::command::CompletionGenerateCommand,
config::{self, TomlConfig},
envelope::command::EnvelopeSubcommand,
folder::command::FolderSubcommand,
manual::command::ManualGenerateCommand,
output::{ColorFmt, OutputFmt},
Expand Down Expand Up @@ -96,6 +97,10 @@ pub enum HimalayaCommand {
#[command(subcommand)]
Folder(FolderSubcommand),

/// Subcommand to manage envelopes
#[command(subcommand)]
Envelope(EnvelopeSubcommand),

/// Generate manual pages to a directory
#[command(arg_required_else_help = true)]
Manual(ManualGenerateCommand),
Expand All @@ -110,6 +115,7 @@ impl HimalayaCommand {
match self {
Self::Account(cmd) => cmd.execute(printer, config).await,
Self::Folder(cmd) => cmd.execute(printer, config).await,
Self::Envelope(cmd) => cmd.execute(printer, config).await,
Self::Manual(cmd) => cmd.execute(printer).await,
Self::Completion(cmd) => cmd.execute(printer).await,
}
Expand Down
91 changes: 0 additions & 91 deletions src/email/envelope/args.rs

This file was deleted.

68 changes: 68 additions & 0 deletions src/email/envelope/command/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use anyhow::Result;
use clap::Parser;
use log::info;

use crate::{
account::arg::name::AccountNameFlag,
backend::Backend,
cache::arg::disable::DisableCacheFlag,
config::TomlConfig,
folder::arg::name::FolderNameOptionalArg,
printer::{PrintTableOpts, Printer},
ui::arg::max_width::MaxTableWidthFlag,
};

/// List all envelopes from a folder
#[derive(Debug, Parser)]
pub struct EnvelopeListCommand {
#[command(flatten)]
pub folder: FolderNameOptionalArg,

/// The page number
#[arg(long, short, value_name = "NUMBER", default_value = "1")]
pub page: usize,

/// The page size
#[arg(long, short = 's', value_name = "NUMBER")]
pub page_size: Option<usize>,

#[command(flatten)]
pub table: MaxTableWidthFlag,

#[command(flatten)]
pub account: AccountNameFlag,

#[command(flatten)]
pub cache: DisableCacheFlag,
}

impl EnvelopeListCommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
info!("executing envelope list command");

let folder = &self.folder.name;

let some_account_name = self.account.name.as_ref().map(String::as_str);
let (toml_account_config, account_config) = config
.clone()
.into_account_configs(some_account_name, self.cache.disable)?;
let backend = Backend::new(toml_account_config, account_config.clone(), false).await?;

let page_size = self
.page_size
.unwrap_or(account_config.email_listing_page_size());
let page = 1.max(self.page) - 1;

let envelopes = backend.list_envelopes(folder, page_size, page).await?;

printer.print_table(
Box::new(envelopes),
PrintTableOpts {
format: &account_config.email_reading_format,
max_width: self.table.max_width,
},
)?;

Ok(())
}
}
24 changes: 24 additions & 0 deletions src/email/envelope/command/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pub mod list;

use anyhow::Result;
use clap::Subcommand;

use crate::{config::TomlConfig, printer::Printer};

use self::list::EnvelopeListCommand;

/// Subcommand to manage envelopes
#[derive(Debug, Subcommand)]
pub enum EnvelopeSubcommand {
/// List all envelopes from a folder
#[command(alias = "lst")]
List(EnvelopeListCommand),
}

impl EnvelopeSubcommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
match self {
Self::List(cmd) => cmd.execute(printer, config).await,
}
}
}
32 changes: 0 additions & 32 deletions src/email/envelope/handlers.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/email/envelope/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod args;
pub mod command;
pub mod config;
pub mod flag;
pub mod handlers;

use anyhow::Result;
use email::account::config::AccountConfig;
Expand Down
10 changes: 5 additions & 5 deletions src/email/message/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use anyhow::Result;
use clap::{Arg, ArgAction, ArgMatches, Command};

use crate::{folder, template};
use crate::template;

const ARG_CRITERIA: &str = "criterion";
const ARG_HEADERS: &str = "headers";
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn matches(m: &ArgMatches) -> Result<Option<Cmd>> {
Some(Cmd::Attachments(ids))
} else if let Some(m) = m.subcommand_matches(CMD_COPY) {
let ids = parse_ids_arg(m);
let folder = folder::args::parse_target_arg(m);
let folder = "INBOX";
Some(Cmd::Copy(ids, folder))
} else if let Some(m) = m.subcommand_matches(CMD_DELETE) {
let ids = parse_ids_arg(m);
Expand All @@ -83,7 +83,7 @@ pub fn matches(m: &ArgMatches) -> Result<Option<Cmd>> {
Some(Cmd::Forward(id, headers, body))
} else if let Some(m) = m.subcommand_matches(CMD_MOVE) {
let ids = parse_ids_arg(m);
let folder = folder::args::parse_target_arg(m);
let folder = "INBOX";
Some(Cmd::Move(ids, folder))
} else if let Some(m) = m.subcommand_matches(CMD_READ) {
let ids = parse_ids_arg(m);
Expand Down Expand Up @@ -158,12 +158,12 @@ pub fn subcmd() -> Command {
Command::new(CMD_COPY)
.alias("cp")
.about("Copy emails to the given folder")
.arg(folder::args::target_arg())
// .arg(folder::args::target_arg())
.arg(ids_arg()),
Command::new(CMD_MOVE)
.alias("mv")
.about("Move emails to the given folder")
.arg(folder::args::target_arg())
// .arg(folder::args::target_arg())
.arg(ids_arg()),
Command::new(CMD_DELETE)
.aliases(["remove", "rm"])
Expand Down
9 changes: 9 additions & 0 deletions src/folder/arg/name.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Parser;
use email::account::config::DEFAULT_INBOX_FOLDER;

/// The folder name argument parser
#[derive(Debug, Parser)]
Expand All @@ -7,3 +8,11 @@ pub struct FolderNameArg {
#[arg(name = "folder-name", value_name = "FOLDER")]
pub name: String,
}

/// The optional folder name argument parser
#[derive(Debug, Parser)]
pub struct FolderNameOptionalArg {
/// The name of the folder
#[arg(name = "folder-name", value_name = "FOLDER", default_value = DEFAULT_INBOX_FOLDER)]
pub name: String,
}
Loading

0 comments on commit 2c33dd2

Please sign in to comment.