add user and cat filters to search

This commit is contained in:
LordMZTE 2020-12-21 23:06:50 +01:00
parent 709e10111c
commit 1c90cc3a47
3 changed files with 61 additions and 30 deletions

View File

@ -9,33 +9,24 @@ pub async fn run(
user: Option<String>,
sorting: Option<MemeSorting>,
) -> 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

View File

@ -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<String>,
cat: Option<String>,
) -> 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) {

View File

@ -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<String>,
#[structopt(long, short, help = "filter by user")]
user: Option<String>,
},
#[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,