jensmemesclient/cli/src/commands/search.rs
LordMZTE 34bc07c3c4
All checks were successful
continuous-integration/drone/push Build is passing
fix meme response cache to include filters
2020-12-30 16:01:22 +01:00

59 lines
1.3 KiB
Rust

use anyhow::Result;
use log::info;
use reqwest::Client;
use crate::util::{self, api};
pub async fn run(
http: &Client,
query: String,
user: Option<String>,
cat: Option<String>,
) -> Result<()> {
let (memes, ..) = tokio::try_join!(
api::memes(
http,
cat.as_ref().map(String::clone),
user.as_ref().map(String::clone),
),
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);
for meme in memes.iter() {
let file_name = meme.file_name()?;
if let Some(score) = fuzzy_matcher::clangd::fuzzy_match(file_name, &query) {
info!("Found matching meme '{}' with score {}", file_name, score);
matches.push((meme, score));
}
}
matches.sort_by(|a, b| b.1.cmp(&a.1));
let mut table = util::list_table();
for m in matches {
table.add_row(m.0.into());
}
println!("{}", table.render());
Ok(())
}