2020-12-05 22:20:08 +01:00
|
|
|
use anyhow::Result;
|
2020-12-06 14:57:14 +01:00
|
|
|
use log::info;
|
2020-12-05 22:20:08 +01:00
|
|
|
use reqwest::Client;
|
|
|
|
|
2020-12-19 19:43:23 +01:00
|
|
|
use crate::util::{self, api};
|
2020-12-05 22:20:08 +01:00
|
|
|
|
2020-12-21 23:06:50 +01:00
|
|
|
pub async fn run(
|
|
|
|
http: &Client,
|
|
|
|
query: String,
|
|
|
|
user: Option<String>,
|
|
|
|
cat: Option<String>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let (memes, ..) = tokio::try_join!(
|
2020-12-29 14:19:51 +01:00
|
|
|
api::memes(
|
|
|
|
http,
|
|
|
|
cat.as_ref().map(String::as_ref),
|
|
|
|
user.as_ref().map(String::as_ref),
|
|
|
|
),
|
2020-12-21 23:06:50 +01:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)?;
|
2020-12-05 22:20:08 +01:00
|
|
|
|
|
|
|
let mut matches = vec![];
|
|
|
|
|
2020-12-06 14:57:14 +01:00
|
|
|
info!("Starting search with query '{}'", query);
|
2020-12-21 23:06:50 +01:00
|
|
|
|
2020-12-05 22:20:08 +01:00
|
|
|
for meme in memes {
|
2020-12-06 14:57:14 +01:00
|
|
|
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);
|
2020-12-05 22:20:08 +01:00
|
|
|
matches.push((meme, score));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 22:31:02 +01:00
|
|
|
matches.sort_by(|a, b| b.1.cmp(&a.1));
|
2020-12-05 22:20:08 +01:00
|
|
|
|
|
|
|
let mut table = util::list_table();
|
|
|
|
|
|
|
|
for m in matches {
|
|
|
|
table.add_row(m.0.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("{}", table.render());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|