mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Fixes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use regex::Regex;
|
||||
|
||||
use crate::bots::approved_bot::modules::utils::CommandParse;
|
||||
use crate::bots::approved_bot::modules::utils::{filter_command::CommandParse, errors::CommandParseError};
|
||||
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -10,28 +10,22 @@ pub enum AnnotationCommand {
|
||||
}
|
||||
|
||||
impl CommandParse<Self> for AnnotationCommand {
|
||||
fn parse(s: &str, bot_name: &str) -> Result<Self, strum::ParseError> {
|
||||
let re = Regex::new(r"^/(?P<an_type>[ab])_an_(?P<id>\d+)$")
|
||||
.unwrap_or_else(|_| panic!("Can't create AnnotationCommand regexp!"));
|
||||
|
||||
let full_bot_name = format!("@{bot_name}");
|
||||
let after_replace = s.replace(&full_bot_name, "");
|
||||
|
||||
let caps = re.captures(&after_replace);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let annotation_type = &caps["an_type"];
|
||||
let id: u32 = caps["id"]
|
||||
.parse()
|
||||
.unwrap_or_else(|_| panic!("Can't get id from AnnotationCommand!"));
|
||||
|
||||
match annotation_type {
|
||||
"a" => Ok(AnnotationCommand::Author { id }),
|
||||
"b" => Ok(AnnotationCommand::Book { id }),
|
||||
_ => Err(strum::ParseError::VariantNotFound),
|
||||
}
|
||||
fn parse(s: &str, bot_name: &str) -> Result<Self, CommandParseError> {
|
||||
Regex::new(r"^/(?P<an_type>[ab])_an_(?P<id>\d+)$")
|
||||
.unwrap_or_else(|_| panic!("Broken AnnotationCommand regexp!"))
|
||||
.captures(
|
||||
&s.replace(&format!("@{bot_name}"), "")
|
||||
).ok_or(CommandParseError)
|
||||
.map(|caps| (
|
||||
caps["an_type"].to_string(),
|
||||
caps["id"].parse::<u32>().unwrap_or_else(|_| panic!("Can't get id from AnnotationCommand!"))
|
||||
))
|
||||
.map(|(annotation_type, id)| {
|
||||
match annotation_type.as_str() {
|
||||
"a" => Ok(AnnotationCommand::Author { id }),
|
||||
"b" => Ok(AnnotationCommand::Book { id }),
|
||||
_ => panic!("Unknown AnnotationCommand type: {}!", annotation_type),
|
||||
}
|
||||
})?
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user