mirror of
https://github.com/msfjarvis/linkleaner.git
synced 2024-06-03 02:08:11 +05:30
Run fmt and handle cases where no results are found
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
7dda19aa09
commit
e79039f278
46
src/main.rs
46
src/main.rs
|
@ -10,8 +10,8 @@ use rand::{thread_rng, Rng};
|
|||
use std::env;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use teloxide::requests::{SendChatActionKind, SendPhoto};
|
||||
use crate::utils::{file_name_to_label, join_results_to_string, tokenized_search};
|
||||
use teloxide::requests::{SendChatActionKind, SendPhoto};
|
||||
|
||||
lazy_static! {
|
||||
static ref FILES: Vec<String> = index_pictures();
|
||||
|
@ -87,13 +87,24 @@ async fn answer(
|
|||
.await?
|
||||
} else {
|
||||
let results = get_search_results(&args.join("_"));
|
||||
let file = get_random_file(results);
|
||||
let link = format!("{}/{}", *BASE_URL, file);
|
||||
cx.bot
|
||||
.send_chat_action(cx.update.chat.id, SendChatActionKind::UploadPhoto)
|
||||
.send()
|
||||
.await?;
|
||||
send_captioned_picture(cx, link, file).send().await?
|
||||
if results.is_empty() {
|
||||
cx.bot
|
||||
.send_chat_action(cx.update.chat.id, SendChatActionKind::Typing)
|
||||
.send()
|
||||
.await?;
|
||||
cx.answer(format!("No picture found for '{}'", &args.join(" ")))
|
||||
.reply_to_message_id(cx.update.id)
|
||||
.send()
|
||||
.await?
|
||||
} else {
|
||||
let file = get_random_file(results);
|
||||
let link = format!("{}/{}", *BASE_URL, file);
|
||||
cx.bot
|
||||
.send_chat_action(cx.update.chat.id, SendChatActionKind::UploadPhoto)
|
||||
.send()
|
||||
.await?;
|
||||
send_captioned_picture(cx, link, file).send().await?
|
||||
}
|
||||
}
|
||||
}
|
||||
Command::Random => {
|
||||
|
@ -112,12 +123,19 @@ async fn answer(
|
|||
.send_chat_action(cx.update.chat.id, SendChatActionKind::Typing)
|
||||
.send()
|
||||
.await?;
|
||||
cx.answer(join_results_to_string(search_term, res, &**BASE_URL))
|
||||
.parse_mode(ParseMode::MarkdownV2)
|
||||
.disable_web_page_preview(true)
|
||||
.reply_to_message_id(cx.update.id)
|
||||
.send()
|
||||
.await?
|
||||
if res.is_empty() {
|
||||
cx.answer(format!("No results found for '{}'", &args.join(" ")))
|
||||
.reply_to_message_id(cx.update.id)
|
||||
.send()
|
||||
.await?
|
||||
} else {
|
||||
cx.answer(join_results_to_string(search_term, res, &**BASE_URL))
|
||||
.parse_mode(ParseMode::MarkdownV2)
|
||||
.disable_web_page_preview(true)
|
||||
.reply_to_message_id(cx.update.id)
|
||||
.send()
|
||||
.await?
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -29,7 +29,10 @@ pub(crate) fn join_results_to_string(
|
|||
}
|
||||
|
||||
pub(crate) fn tokenized_search(name: String, search_term: &str) -> bool {
|
||||
let tokens = file_name_to_label(name).split(" ").map(|x| x.to_lowercase()).collect::<Vec<String>>();
|
||||
let tokens = file_name_to_label(name)
|
||||
.split(" ")
|
||||
.map(|x| x.to_lowercase())
|
||||
.collect::<Vec<String>>();
|
||||
for token in tokens {
|
||||
if token == search_term {
|
||||
return true;
|
||||
|
|
Loading…
Reference in New Issue
Block a user