diff --git a/cli/src/commands/list.rs b/cli/src/commands/list.rs index 1304372..8aa8d32 100644 --- a/cli/src/commands/list.rs +++ b/cli/src/commands/list.rs @@ -9,33 +9,24 @@ pub async fn run( user: Option, sorting: Option, ) -> Result<()> { - let mut cat_check_fut = None; - let mut usr_check_fut = None; - if let Some(c) = cat.as_ref() { - cat_check_fut = Some(util::assert_category_exists(http, c)); - } - - if let Some(u) = user.as_ref() { - usr_check_fut = Some(util::assert_user_exists(http, u)); - } - - // This needs to be done so both users, memes and categories will be requested at once - let (_, _, memes) = tokio::try_join!( - async { - if let Some(f) = cat_check_fut { - f.await - } else { - Ok(()) - } - }, - async { - if let Some(f) = usr_check_fut { - f.await - } else { - Ok(()) - } - }, + // This needs to be done so both users, memes and categories will be requested + // at once + let (memes, ..) = tokio::try_join!( api::memes(http), + async { + if let Some(c) = cat.as_ref() { + util::assert_category_exists(http, c).await + } else { + Ok(()) + } + }, + async { + if let Some(u) = user.as_ref() { + util::assert_user_exists(http, u).await + } else { + Ok(()) + } + }, )?; let memes = memes diff --git a/cli/src/commands/search.rs b/cli/src/commands/search.rs index ab79b96..e1a4705 100644 --- a/cli/src/commands/search.rs +++ b/cli/src/commands/search.rs @@ -4,12 +4,40 @@ use reqwest::Client; use crate::util::{self, api}; -pub async fn run(http: &Client, query: String) -> Result<()> { - let memes = api::memes(http).await?; +pub async fn run( + http: &Client, + query: String, + user: Option, + cat: Option, +) -> Result<()> { + let memes = api::memes(http); + let (memes, ..) = tokio::try_join!( + memes, + async { + if let Some(u) = user.as_ref() { + util::assert_user_exists(http, u).await + } else { + Ok(()) + } + }, + async { + if let Some(c) = cat.as_ref() { + util::assert_category_exists(http, c).await + } else { + Ok(()) + } + } + )?; let mut matches = vec![]; info!("Starting search with query '{}'", query); + + let memes = memes + .iter() + .filter(|m| user.as_ref().map(|u| u == &m.user).unwrap_or(true)) + .filter(|m| cat.as_ref().map(|u| u == &m.category).unwrap_or(true)); + for meme in memes { let file_name = meme.file_name()?; if let Some(score) = fuzzy_matcher::clangd::fuzzy_match(file_name, &query) { diff --git a/cli/src/main.rs b/cli/src/main.rs index f521468..5876a55 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -44,7 +44,15 @@ enum Cmd { Cats, #[structopt(about = "searches for a meme")] - Search { query: String }, + Search { + query: String, + + #[structopt(long, short, help = "filter by category")] + category: Option, + + #[structopt(long, short, help = "filter by user")] + user: Option, + }, #[structopt(about = "lists all memes")] List { @@ -88,7 +96,11 @@ async fn main() -> Result<()> { .await? .iter() .for_each(|c| println!("{}", c)), - Cmd::Search { query } => commands::search::run(&http, query).await?, + Cmd::Search { + query, + user, + category, + } => commands::search::run(&http, query, user, category).await?, Cmd::List { category, user,