refactor: add a typealias for async errors

This commit is contained in:
Harsh Shandilya 2024-04-25 17:05:15 +05:30
parent bdc9b21bb6
commit 65c6979881
7 changed files with 37 additions and 33 deletions

View File

@ -1,6 +1,11 @@
use crate::{fixer::FixerState, message::BotExt, utils::parse_bool, FIXER_STATE};
use crate::{
fixer::FixerState,
message::BotExt,
utils::{parse_bool, AsyncError},
FIXER_STATE,
};
use once_cell::sync::Lazy;
use std::{env, error::Error, marker::Send};
use std::env;
use teloxide::{
payloads::SendMessageSetters,
prelude::Requester,
@ -48,7 +53,7 @@ pub(crate) async fn handler(
bot: Bot,
message: Message,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {
) -> Result<(), AsyncError> {
match command {
Command::Help | Command::Start => {
bot.send_chat_message(message, Command::descriptions().to_string())

View File

@ -1,9 +1,12 @@
mod model;
use crate::{message::BotExt, utils::get_urls_from_message};
use crate::{
message::BotExt,
utils::{get_urls_from_message, AsyncError},
};
use model::AMPResponse;
use reqwest::Url;
use std::{error::Error, str::FromStr};
use std::str::FromStr;
use teloxide::{prelude::Requester, types::Message, utils::html::link, Bot};
use tracing::debug;
@ -13,10 +16,7 @@ fn deserialize_amp_response(text: &str) -> Result<AMPResponse, serde_json::Error
serde_json::from_str(text)
}
pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
pub async fn handler(bot: Bot, message: Message) -> Result<(), AsyncError> {
if let Some(text) = message.text()
&& let Some(user) = message.from()
{

View File

@ -1,7 +1,9 @@
use crate::{message::BotExt, utils::scrub_urls};
use crate::{
message::BotExt,
utils::{scrub_urls, AsyncError},
};
use once_cell::sync::Lazy;
use regex::Regex;
use std::error::Error;
use teloxide::{prelude::Requester, types::Message, utils::html::link, Bot};
const HOST_MATCH_GROUP: &str = "host";
@ -10,10 +12,7 @@ pub static MATCH_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new("https://(?:www.)?(?P<host>instagram.com)/(p|reel|tv)/[A-Za-z0-9]+.*/").unwrap()
});
pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
pub async fn handler(bot: Bot, message: Message) -> Result<(), AsyncError> {
if let Some(text) = scrub_urls(&message)
&& let Some(user) = message.from()
&& let Some(caps) = MATCH_REGEX.captures(&text)

View File

@ -1,7 +1,9 @@
use crate::{message::BotExt, utils::scrub_urls};
use crate::{
message::BotExt,
utils::{scrub_urls, AsyncError},
};
use once_cell::sync::Lazy;
use regex::Regex;
use std::error::Error;
use teloxide::{prelude::Requester, types::Message, utils::html::link, Bot};
const HOST_MATCH_GROUP: &str = "host";
@ -9,10 +11,7 @@ const HOST_MATCH_GROUP: &str = "host";
pub static MATCH_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new("https://(?P<host>(?:.*)?medium.com)/.*").unwrap());
pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
pub async fn handler(bot: Bot, message: Message) -> Result<(), AsyncError> {
if let Some(text) = scrub_urls(&message)
&& let Some(user) = message.from()
&& let Some(caps) = MATCH_REGEX.captures(&text)

View File

@ -1,7 +1,9 @@
use crate::{message::BotExt, utils::scrub_urls};
use crate::{
message::BotExt,
utils::{scrub_urls, AsyncError},
};
use once_cell::sync::Lazy;
use regex::Regex;
use std::error::Error;
use teloxide::{prelude::Requester, types::Message, utils::html::link, Bot};
const HOST_MATCH_GROUP: &str = "host";
@ -12,10 +14,7 @@ pub static MATCH_REGEX: Lazy<Regex> = Lazy::new(|| {
.unwrap()
});
pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
pub async fn handler(bot: Bot, message: Message) -> Result<(), AsyncError> {
if let Some(text) = scrub_urls(&message)
&& let Some(user) = message.from()
&& let Some(caps) = MATCH_REGEX.captures(&text)

View File

@ -1,8 +1,11 @@
use once_cell::sync::Lazy;
use reqwest::Url;
use std::error::Error;
use teloxide::types::{Message, MessageEntityKind};
use tracing::{error, info};
pub(crate) type AsyncError = Box<dyn Error + Send + Sync + 'static>;
pub(crate) fn get_urls_from_message(msg: &Message) -> Vec<String> {
if let Some(entities) = msg.entities()
&& !entities.is_empty()

View File

@ -1,17 +1,16 @@
use crate::{message::BotExt, utils::scrub_urls};
use crate::{
message::BotExt,
utils::{scrub_urls, AsyncError},
};
use once_cell::sync::Lazy;
use regex::Regex;
use std::error::Error;
use teloxide::{prelude::Requester, types::Message, utils::html::link, Bot};
pub static MATCH_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new("https://(?:www.)?youtube.com/(?P<shorts>shorts/)[A-Za-z0-9-_]{11}.*").unwrap()
});
pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
pub async fn handler(bot: Bot, message: Message) -> Result<(), AsyncError> {
if let Some(text) = scrub_urls(&message)
&& let Some(user) = message.from()
&& let Some(caps) = MATCH_REGEX.captures(&text)