1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-25 20:44:21 +00:00

Allow creating /diario with a plain syntax

This commit is contained in:
Steffo 2024-08-08 19:46:34 +02:00
parent b3b6ff7a74
commit 230e500fc8
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0

View file

@ -2,7 +2,7 @@ use std::fmt::{Error, Write};
use std::str::FromStr; use std::str::FromStr;
use anyhow::Context; use anyhow::Context;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::{Captures, Regex};
use teloxide::Bot; use teloxide::Bot;
use teloxide::payloads::SendMessageSetters; use teloxide::payloads::SendMessageSetters;
use teloxide::prelude::Requester; use teloxide::prelude::Requester;
@ -26,26 +26,37 @@ impl FromStr for DiarioArgs {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#" *(?:\[(?<warning>.+)])? *"(?<quote>.+)"[, ]*(?:[-–—]+(?<quoted>\w+)(?:, *(?<context>.+))?)?"#).unwrap()); static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#" *(?:\[(?<warning>.+)])? *"(?<quote>.+)"[, ]*(?:[-–—]+(?<quoted>\w+)(?:, *(?<context>.+))?)?"#).unwrap());
let captures = REGEX.captures(s) let captures = REGEX.captures(s);
.context("Sintassi del comando incorretta.")?;
let warning = captures.name("warning") let args = match captures {
.map(|s| s.as_str().to_owned()); Some(captures) => {
let warning = captures.name("warning")
.map(|s| s.as_str().to_owned());
let quote = captures.name("quote") let quote = captures.name("quote")
.context("Citazione non specificata nel comando.")? .context("Citazione non specificata nel comando.")?
.as_str() .as_str()
.to_owned(); .to_owned();
let quoted = captures.name("quoted") let quoted = captures.name("quoted")
.map(|s| s.as_str().to_owned()); .map(|s| s.as_str().to_owned());
let context = captures.name("context") let context = captures.name("context")
.map(|s| s.as_str().to_owned()); .map(|s| s.as_str().to_owned());
Ok( DiarioArgs {warning, quote, quoted, context}
Self { warning, quote, quoted, context } },
) None => {
let warning = None;
let quote = s.to_string();
let quoted = None;
let context = None;
DiarioArgs {warning, quote, quoted, context}
},
};
Ok(args)
} }
} }