1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-29 14:34:18 +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,9 +26,10 @@ 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 args = match captures {
Some(captures) => {
let warning = captures.name("warning") let warning = captures.name("warning")
.map(|s| s.as_str().to_owned()); .map(|s| s.as_str().to_owned());
@ -43,9 +44,19 @@ impl FromStr for DiarioArgs {
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)
} }
} }