mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Merge pull request #25 from flibusta-apps/refactor/book-callback-data
Refactor book callback data parsers
This commit is contained in:
@@ -3,7 +3,9 @@ use std::str::FromStr;
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use strum_macros::EnumIter;
|
use strum_macros::EnumIter;
|
||||||
|
|
||||||
use crate::bots::approved_bot::modules::utils::errors::CommandParseError;
|
use crate::bots::approved_bot::modules::utils::errors::{
|
||||||
|
CallbackQueryParseError, CommandParseError,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, EnumIter)]
|
#[derive(Clone, EnumIter)]
|
||||||
pub enum DownloadQueryData {
|
pub enum DownloadQueryData {
|
||||||
@@ -60,26 +62,26 @@ impl ToString for DownloadArchiveQueryData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for DownloadArchiveQueryData {
|
impl FromStr for DownloadArchiveQueryData {
|
||||||
type Err = strum::ParseError;
|
type Err = CallbackQueryParseError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let re = Regex::new(r"^da_(?P<obj_type>[sat])_(?P<id>\d+)_(?P<file_type>\w+)$").unwrap();
|
Regex::new(r"^da_(?P<obj_type>[sat])_(?P<id>\d+)_(?P<file_type>\w+)$")
|
||||||
|
.unwrap_or_else(|_| panic!("Broken BookCallbackData regex pattern!"))
|
||||||
let caps = re.captures(s);
|
.captures(s)
|
||||||
let caps = match caps {
|
.ok_or(CallbackQueryParseError)
|
||||||
Some(v) => v,
|
.map(|caps| {
|
||||||
None => return Err(strum::ParseError::VariantNotFound),
|
(
|
||||||
};
|
caps["id"].parse().unwrap(),
|
||||||
|
caps["file_type"].to_string(),
|
||||||
let id: u32 = caps["id"].parse().unwrap();
|
caps["obj_type"].to_string(),
|
||||||
let file_type: String = caps["file_type"].to_string();
|
)
|
||||||
|
})
|
||||||
Ok(match caps["obj_type"].to_string().as_str() {
|
.map(|(id, file_type, obj_type)| match obj_type.as_str() {
|
||||||
"s" => DownloadArchiveQueryData::Sequence { id, file_type },
|
"s" => DownloadArchiveQueryData::Sequence { id, file_type },
|
||||||
"a" => DownloadArchiveQueryData::Author { id, file_type },
|
"a" => DownloadArchiveQueryData::Author { id, file_type },
|
||||||
"t" => DownloadArchiveQueryData::Translator { id, file_type },
|
"t" => DownloadArchiveQueryData::Translator { id, file_type },
|
||||||
_ => return Err(strum::ParseError::VariantNotFound),
|
_ => panic!("Unknown DownloadArchiveQueryData type: {}!", obj_type),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,19 +97,14 @@ impl ToString for CheckArchiveStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for CheckArchiveStatus {
|
impl FromStr for CheckArchiveStatus {
|
||||||
type Err = strum::ParseError;
|
type Err = CallbackQueryParseError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let re = Regex::new(r"^check_da_(?P<task_id>\w+)$").unwrap();
|
Regex::new(r"^check_da_(?P<task_id>\w+)$")
|
||||||
|
.unwrap_or_else(|_| panic!("Broken CheckArchiveStatus regex pattern!"))
|
||||||
let caps = re.captures(s);
|
.captures(s)
|
||||||
let caps = match caps {
|
.ok_or(CallbackQueryParseError)
|
||||||
Some(v) => v,
|
.map(|caps| caps["task_id"].parse().unwrap())
|
||||||
None => return Err(strum::ParseError::VariantNotFound),
|
.map(|task_id| CheckArchiveStatus { task_id })
|
||||||
};
|
|
||||||
|
|
||||||
let task_id: String = caps["task_id"].parse().unwrap();
|
|
||||||
|
|
||||||
Ok(CheckArchiveStatus { task_id })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user