Skip to content

Commit

Permalink
add query arg to envelope list command
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Feb 28, 2024
1 parent c28b4c6 commit 1e7adc5
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 40 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Changed the `envelope list` options:
- the folder argument became a flag `--folder <name>`
- the query argument has been added at the end of the command to filter and sort results (only filter and IMAP for now)

## [1.0.0-beta.3] - 2024-02-25

### Added
Expand Down
21 changes: 19 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ tempfile = "3.3"

[dependencies]
anyhow = "1"
ariadne = "0.2"
async-trait = "0.1"
chrono = "0.4.24"
clap = { version = "4.4", features = ["derive"] }
Expand Down Expand Up @@ -88,3 +89,6 @@ uuid = { version = "0.8", features = ["v4"] }

[target.'cfg(not(windows))'.dependencies.coredump]
version = "0.1"

[patch.crates-io]
email-lib = { git = "https://git.sr.ht/~soywod/pimalaya" }
12 changes: 8 additions & 4 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ use email::{
backend::{
feature::BackendFeature, macros::BackendContext, mapper::SomeBackendContextBuilderMapper,
},
envelope::{get::GetEnvelope, list::ListEnvelopes, watch::WatchEnvelopes, Id, SingleId},
envelope::{
get::GetEnvelope,
list::{ListEnvelopes, ListEnvelopesOptions},
watch::WatchEnvelopes,
Id, SingleId,
},
flag::{add::AddFlags, remove::RemoveFlags, set::SetFlags, Flag, Flags},
folder::{
add::AddFolder, delete::DeleteFolder, expunge::ExpungeFolder, list::ListFolders,
Expand Down Expand Up @@ -644,12 +649,11 @@ impl Backend {
pub async fn list_envelopes(
&self,
folder: &str,
page_size: usize,
page: usize,
opts: ListEnvelopesOptions,
) -> Result<Envelopes> {
let backend_kind = self.toml_account_config.list_envelopes_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let envelopes = self.backend.list_envelopes(folder, page_size, page).await?;
let envelopes = self.backend.list_envelopes(folder, opts).await?;
let envelopes = Envelopes::from_backend(&self.account_config, &id_mapper, envelopes)?;
Ok(envelopes)
}
Expand Down
29 changes: 0 additions & 29 deletions src/cache/args.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod arg;
pub mod args;

use anyhow::{anyhow, Context, Result};
use dirs::data_dir;
Expand Down
47 changes: 43 additions & 4 deletions src/email/envelope/command/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use ariadne::{Color, Label, Report, ReportKind, Source};
use clap::Parser;
use email::backend::feature::BackendFeatureSource;
use email::{backend::feature::BackendFeatureSource, envelope::list::ListEnvelopesOptions};
use log::info;

#[cfg(feature = "account-sync")]
Expand All @@ -9,7 +10,7 @@ use crate::{
account::arg::name::AccountNameFlag,
backend::Backend,
config::TomlConfig,
folder::arg::name::FolderNameOptionalArg,
folder::arg::name::FolderNameOptionalFlag,
printer::{PrintTableOpts, Printer},
ui::arg::max_width::TableMaxWidthFlag,
};
Expand All @@ -21,7 +22,7 @@ use crate::{
#[derive(Debug, Parser)]
pub struct ListEnvelopesCommand {
#[command(flatten)]
pub folder: FolderNameOptionalArg,
pub folder: FolderNameOptionalFlag,

/// The page number.
///
Expand All @@ -45,6 +46,12 @@ pub struct ListEnvelopesCommand {

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

/// The list envelopes filter and sort query.
///
/// TODO
#[arg(allow_hyphen_values = true, trailing_var_arg = true)]
pub query: Option<Vec<String>>,
}

impl Default for ListEnvelopesCommand {
Expand All @@ -57,6 +64,7 @@ impl Default for ListEnvelopesCommand {
#[cfg(feature = "account-sync")]
cache: Default::default(),
account: Default::default(),
query: Default::default(),
}
}
}
Expand Down Expand Up @@ -87,7 +95,38 @@ impl ListEnvelopesCommand {
)
.await?;

let envelopes = backend.list_envelopes(folder, page_size, page).await?;
let filter = match self.query.map(|filter| filter.join(" ").parse()) {
Some(Ok(filter)) => Some(filter),
Some(Err(err)) => {
if let email::envelope::list::Error::ParseFilterError(errs, query) = &err {
errs.into_iter().for_each(|e| {
Report::build(ReportKind::Error, "query", e.span().start)
.with_message(e.to_string())
.with_label(
Label::new(("query", e.span().into_range()))
.with_message(e.reason().to_string())
.with_color(Color::Red),
)
.finish()
.eprint(("query", Source::from(&query)))
.unwrap()
});
};

Err(err)?;
None
}
None => None,
};

let opts = ListEnvelopesOptions {
page,
page_size,
filter,
sort: Default::default(),
};

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

printer.print_table(
Box::new(envelopes),
Expand Down
8 changes: 8 additions & 0 deletions src/folder/arg/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ pub struct FolderNameOptionalFlag {
pub name: String,
}

impl Default for FolderNameOptionalFlag {
fn default() -> Self {
Self {
name: INBOX.to_owned(),
}
}
}

/// The optional folder name argument parser.
#[derive(Debug, Parser)]
pub struct FolderNameOptionalArg {
Expand Down

0 comments on commit 1e7adc5

Please sign in to comment.