Skip to content

Commit

Permalink
refactor message with clap derive api (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Dec 7, 2023
1 parent 5e1a03e commit a47902a
Show file tree
Hide file tree
Showing 26 changed files with 539 additions and 365 deletions.
21 changes: 14 additions & 7 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,28 +723,35 @@ impl Backend {
Ok(envelopes)
}

pub async fn add_flags(&self, folder: &str, ids: &[&str], flags: &Flags) -> Result<()> {
pub async fn add_flags(&self, folder: &str, ids: &[usize], flags: &Flags) -> Result<()> {
let backend_kind = self.toml_account_config.add_flags_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let ids = Id::multiple(id_mapper.get_ids(ids)?);
self.backend.add_flags(folder, &ids, flags).await
}

pub async fn set_flags(&self, folder: &str, ids: &[&str], flags: &Flags) -> Result<()> {
pub async fn set_flags(&self, folder: &str, ids: &[usize], flags: &Flags) -> Result<()> {
let backend_kind = self.toml_account_config.set_flags_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let ids = Id::multiple(id_mapper.get_ids(ids)?);
self.backend.set_flags(folder, &ids, flags).await
}

pub async fn remove_flags(&self, folder: &str, ids: &[&str], flags: &Flags) -> Result<()> {
pub async fn remove_flags(&self, folder: &str, ids: &[usize], flags: &Flags) -> Result<()> {
let backend_kind = self.toml_account_config.remove_flags_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let ids = Id::multiple(id_mapper.get_ids(ids)?);
self.backend.remove_flags(folder, &ids, flags).await
}

pub async fn get_messages(&self, folder: &str, ids: &[&str]) -> Result<Messages> {
pub async fn peek_messages(&self, folder: &str, ids: &[usize]) -> Result<Messages> {
let backend_kind = self.toml_account_config.get_messages_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let ids = Id::multiple(id_mapper.get_ids(ids)?);
self.backend.peek_messages(folder, &ids).await
}

pub async fn get_messages(&self, folder: &str, ids: &[usize]) -> Result<Messages> {
let backend_kind = self.toml_account_config.get_messages_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let ids = Id::multiple(id_mapper.get_ids(ids)?);
Expand All @@ -755,7 +762,7 @@ impl Backend {
&self,
from_folder: &str,
to_folder: &str,
ids: &[&str],
ids: &[usize],
) -> Result<()> {
let backend_kind = self.toml_account_config.move_messages_kind();
let id_mapper = self.build_id_mapper(from_folder, backend_kind)?;
Expand All @@ -769,7 +776,7 @@ impl Backend {
&self,
from_folder: &str,
to_folder: &str,
ids: &[&str],
ids: &[usize],
) -> Result<()> {
let backend_kind = self.toml_account_config.move_messages_kind();
let id_mapper = self.build_id_mapper(from_folder, backend_kind)?;
Expand All @@ -779,7 +786,7 @@ impl Backend {
.await
}

pub async fn delete_messages(&self, folder: &str, ids: &[&str]) -> Result<()> {
pub async fn delete_messages(&self, folder: &str, ids: &[usize]) -> Result<()> {
let backend_kind = self.toml_account_config.delete_messages_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let ids = Id::multiple(id_mapper.get_ids(ids)?);
Expand Down
6 changes: 3 additions & 3 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ impl IdMapper {

pub fn get_id<A>(&self, alias: A) -> Result<String>
where
A: AsRef<str>,
A: ToString,
{
let alias = alias.as_ref();
let alias = alias.to_string();
let alias = alias
.parse::<i64>()
.context(format!("cannot parse id mapper alias {alias}"))?;
Expand Down Expand Up @@ -158,7 +158,7 @@ impl IdMapper {

pub fn get_ids<A, I>(&self, aliases: I) -> Result<Vec<String>>
where
A: AsRef<str>,
A: ToString,
I: IntoIterator<Item = A>,
{
aliases
Expand Down
44 changes: 23 additions & 21 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
flag::command::FlagSubcommand,
folder::command::FolderSubcommand,
manual::command::ManualGenerateCommand,
message::command::MessageSubcommand,
output::{ColorFmt, OutputFmt},
printer::Printer,
};
Expand All @@ -33,7 +34,8 @@ pub struct Cli {
/// applicable). If the path does not point to a valid file, the
/// wizard will propose to assist you in the creation of the
/// configuration file.
#[arg(long, short, value_name = "PATH", global = true, value_parser = config::path_parser)]
#[arg(long, short, global = true)]
#[arg(value_name = "PATH", value_parser = config::path_parser)]
pub config: Option<PathBuf>,

/// Customize the output format
Expand All @@ -47,14 +49,8 @@ pub struct Cli {
///
/// - plain: output will be in a form of either a plain text or
/// table, depending on the command
#[arg(
long,
short,
value_name = "FORMAT",
global = true,
value_enum,
default_value_t = Default::default(),
)]
#[arg(long, short, global = true)]
#[arg(value_name = "FORMAT", value_enum, default_value_t = Default::default())]
pub output: OutputFmt,

/// Control when to use colors
Expand All @@ -77,41 +73,46 @@ pub struct Cli {
/// - ansi: like 'always', but emits ANSI escapes (even in a Windows console)
///
/// - auto: himalaya tries to be smart
#[arg(
long,
short = 'C',
value_name = "MODE",
global = true,
value_enum,
default_value_t = Default::default(),
)]
#[arg(long, short = 'C', global = true)]
#[arg(value_name = "MODE", value_enum, default_value_t = Default::default())]
pub color: ColorFmt,
}

#[derive(Subcommand, Debug)]
pub enum HimalayaCommand {
/// Subcommand to manage accounts
#[command(subcommand, alias = "accounts")]
#[command(subcommand)]
#[command(alias = "accounts")]
Account(AccountSubcommand),

/// Subcommand to manage folders
#[command(subcommand, alias = "folders")]
#[command(subcommand)]
#[command(alias = "folders")]
Folder(FolderSubcommand),

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

/// Subcommand to manage flags
#[command(subcommand, alias = "flags")]
#[command(subcommand)]
#[command(alias = "flags")]
Flag(FlagSubcommand),

/// Subcommand to manage messages
#[command(subcommand)]
#[command(alias = "messages", alias = "msgs", alias = "msg")]
Message(MessageSubcommand),

/// Generate manual pages to a directory
#[command(arg_required_else_help = true)]
#[command(alias = "manuals", alias = "mans")]
Manual(ManualGenerateCommand),

/// Print completion script for a shell to stdout
#[command(arg_required_else_help = true)]
#[command(alias = "completions")]
Completion(CompletionGenerateCommand),
}

Expand All @@ -122,6 +123,7 @@ impl HimalayaCommand {
Self::Folder(cmd) => cmd.execute(printer, config).await,
Self::Envelope(cmd) => cmd.execute(printer, config).await,
Self::Flag(cmd) => cmd.execute(printer, config).await,
Self::Message(cmd) => cmd.execute(printer, config).await,
Self::Manual(cmd) => cmd.execute(printer).await,
Self::Completion(cmd) => cmd.execute(printer).await,
}
Expand Down
9 changes: 9 additions & 0 deletions src/email/envelope/arg/ids.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use clap::Parser;

/// The envelopes ids arguments parser
#[derive(Debug, Parser)]
pub struct EnvelopeIdsArgs {
/// The list of envelopes ids
#[arg(value_name = "ID", required = true)]
pub ids: Vec<usize>,
}
1 change: 1 addition & 0 deletions src/email/envelope/arg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod ids;
21 changes: 9 additions & 12 deletions src/email/envelope/flag/arg/ids_and_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,28 @@ pub struct IdsAndFlagsArgs {

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum IdOrFlag {
Id(String),
Id(usize),
Flag(Flag),
}

impl From<&str> for IdOrFlag {
fn from(value: &str) -> Self {
value
.parse::<usize>()
.map(|_| Self::Id(value.to_owned()))
.unwrap_or_else(|err| {
let flag = Flag::from(value);
debug!("cannot parse {value} as usize, parsing it as flag {flag}");
debug!("{err:?}");
Self::Flag(flag)
})
value.parse::<usize>().map(Self::Id).unwrap_or_else(|err| {
let flag = Flag::from(value);
debug!("cannot parse {value} as usize, parsing it as flag {flag}");
debug!("{err:?}");
Self::Flag(flag)
})
}
}

pub fn to_tuple<'a>(ids_and_flags: &'a [IdOrFlag]) -> (Vec<&'a str>, Flags) {
pub fn into_tuple(ids_and_flags: &[IdOrFlag]) -> (Vec<usize>, Flags) {
ids_and_flags.iter().fold(
(Vec::default(), Flags::default()),
|(mut ids, mut flags), arg| {
match arg {
IdOrFlag::Id(id) => {
ids.push(id.as_str());
ids.push(*id);
}
IdOrFlag::Flag(flag) => {
flags.insert(flag.to_owned());
Expand Down
110 changes: 0 additions & 110 deletions src/email/envelope/flag/args.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/email/envelope/flag/command/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
backend::Backend,
cache::arg::disable::DisableCacheFlag,
config::TomlConfig,
flag::arg::ids_and_flags::{to_tuple, IdsAndFlagsArgs},
flag::arg::ids_and_flags::{into_tuple, IdsAndFlagsArgs},
folder::arg::name::FolderNameArg,
printer::Printer,
};
Expand Down Expand Up @@ -40,7 +40,7 @@ impl FlagAddCommand {
config.clone().into_account_configs(account, cache)?;
let backend = Backend::new(toml_account_config, account_config.clone(), false).await?;

let (ids, flags) = to_tuple(&self.args.ids_and_flags);
let (ids, flags) = into_tuple(&self.args.ids_and_flags);
backend.add_flags(folder, &ids, &flags).await?;

printer.print(format!("Flag(s) {flags} successfully added!"))
Expand Down
4 changes: 2 additions & 2 deletions src/email/envelope/flag/command/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
backend::Backend,
cache::arg::disable::DisableCacheFlag,
config::TomlConfig,
flag::arg::ids_and_flags::{to_tuple, IdsAndFlagsArgs},
flag::arg::ids_and_flags::{into_tuple, IdsAndFlagsArgs},
folder::arg::name::FolderNameArg,
printer::Printer,
};
Expand Down Expand Up @@ -40,7 +40,7 @@ impl FlagRemoveCommand {
config.clone().into_account_configs(account, cache)?;
let backend = Backend::new(toml_account_config, account_config.clone(), false).await?;

let (ids, flags) = to_tuple(&self.args.ids_and_flags);
let (ids, flags) = into_tuple(&self.args.ids_and_flags);
backend.remove_flags(folder, &ids, &flags).await?;

printer.print(format!("Flag(s) {flags} successfully removed!"))
Expand Down
Loading

0 comments on commit a47902a

Please sign in to comment.