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:
parent
b3b6ff7a74
commit
230e500fc8
1 changed files with 27 additions and 16 deletions
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue