2025-01-22 02:12:17 +09:00
|
|
|
use std::fmt::{Debug, Display};
|
2025-01-07 23:15:18 +09:00
|
|
|
|
2025-01-21 00:39:25 +09:00
|
|
|
use smol_str::SmolStr;
|
2025-01-04 02:49:04 +09:00
|
|
|
|
2025-10-07 18:22:04 +00:00
|
|
|
use crate::parameter::{Parameter, ParameterValue};
|
2025-01-07 23:15:18 +09:00
|
|
|
|
2025-01-21 12:36:54 +09:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2024-09-13 16:02:30 +09:00
|
|
|
pub enum Token {
|
2025-01-07 23:15:18 +09:00
|
|
|
/// A bot-defined command / subcommand (usually) (eg. "member" in `pk;member MyName`)
|
2025-01-22 02:12:17 +09:00
|
|
|
Value {
|
|
|
|
|
name: SmolStr,
|
|
|
|
|
aliases: Vec<SmolStr>,
|
|
|
|
|
},
|
2024-09-13 16:02:30 +09:00
|
|
|
|
2025-01-15 03:52:32 +09:00
|
|
|
/// A parameter that must be provided a value
|
2025-01-21 12:36:54 +09:00
|
|
|
Parameter(Parameter),
|
2024-09-13 16:02:30 +09:00
|
|
|
}
|
|
|
|
|
|
2025-10-13 08:16:53 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2025-01-24 04:40:07 +09:00
|
|
|
pub enum TokenMatchResult {
|
|
|
|
|
MatchedValue,
|
|
|
|
|
MatchedParameter {
|
|
|
|
|
name: SmolStr,
|
|
|
|
|
value: ParameterValue,
|
|
|
|
|
},
|
|
|
|
|
ParameterMatchError {
|
|
|
|
|
input: SmolStr,
|
|
|
|
|
msg: SmolStr,
|
|
|
|
|
},
|
|
|
|
|
MissingParameter {
|
|
|
|
|
name: SmolStr,
|
|
|
|
|
},
|
2025-01-07 23:15:18 +09:00
|
|
|
}
|
2024-09-13 16:02:30 +09:00
|
|
|
|
2025-01-24 04:40:07 +09:00
|
|
|
// q: why not have a NoMatch variant in TokenMatchResult?
|
2025-01-12 04:23:46 +09:00
|
|
|
// a: because we want to differentiate between no match and match failure (it matched with an error)
|
|
|
|
|
// "no match" has a different charecteristic because we want to continue matching other tokens...
|
|
|
|
|
// ...while "match failure" means we should stop matching and return the error
|
2025-10-13 08:16:53 +00:00
|
|
|
// Option fits this better (and it makes some code look a bit nicer)
|
2025-10-07 21:59:26 +00:00
|
|
|
pub type TryMatchResult = Option<TokenMatchResult>;
|
2024-09-13 16:02:30 +09:00
|
|
|
|
2025-01-12 04:23:46 +09:00
|
|
|
impl Token {
|
2025-10-07 21:59:26 +00:00
|
|
|
pub fn try_match(&self, input: Option<&str>) -> TryMatchResult {
|
2025-01-07 23:15:18 +09:00
|
|
|
let input = match input {
|
|
|
|
|
Some(input) => input,
|
|
|
|
|
None => {
|
|
|
|
|
// short circuit on:
|
|
|
|
|
return match self {
|
|
|
|
|
// missing paramaters
|
2025-10-03 15:50:54 +00:00
|
|
|
Self::Parameter(param) => Some(
|
|
|
|
|
param
|
|
|
|
|
.is_optional()
|
|
|
|
|
.then(|| TokenMatchResult::MatchedParameter {
|
|
|
|
|
name: param.name().into(),
|
|
|
|
|
value: ParameterValue::Null,
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_else(|| TokenMatchResult::MissingParameter {
|
|
|
|
|
name: param.name().into(),
|
|
|
|
|
}),
|
|
|
|
|
),
|
2025-01-07 23:15:18 +09:00
|
|
|
// everything else doesnt match if no input anyway
|
2025-01-22 02:12:17 +09:00
|
|
|
Self::Value { .. } => None,
|
2025-01-07 23:15:18 +09:00
|
|
|
// don't add a _ match here!
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let input = input.trim();
|
2024-09-13 16:02:30 +09:00
|
|
|
|
|
|
|
|
// try actually matching stuff
|
|
|
|
|
match self {
|
2025-01-22 02:12:17 +09:00
|
|
|
Self::Value { name, aliases } => (aliases.iter().chain(std::iter::once(name)))
|
2025-01-07 23:15:18 +09:00
|
|
|
.any(|v| v.eq(input))
|
2025-01-24 04:40:07 +09:00
|
|
|
.then(|| TokenMatchResult::MatchedValue),
|
2025-10-03 15:50:54 +00:00
|
|
|
Self::Parameter(param) => Some(match param.match_value(input) {
|
2025-01-24 04:40:07 +09:00
|
|
|
Ok(matched) => TokenMatchResult::MatchedParameter {
|
|
|
|
|
name: param.name().into(),
|
|
|
|
|
value: matched,
|
|
|
|
|
},
|
2025-04-04 05:24:09 +09:00
|
|
|
Err(err) => {
|
2025-10-03 15:50:54 +00:00
|
|
|
if param.is_skip() {
|
|
|
|
|
return None;
|
2025-04-04 05:24:09 +09:00
|
|
|
} else {
|
|
|
|
|
TokenMatchResult::ParameterMatchError {
|
|
|
|
|
input: input.into(),
|
|
|
|
|
msg: err,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-26 14:58:59 +00:00
|
|
|
}
|
2025-01-24 04:40:07 +09:00
|
|
|
}),
|
|
|
|
|
// don't add a _ match here!
|
2025-01-07 23:15:18 +09:00
|
|
|
}
|
2024-09-13 16:02:30 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-05 00:59:59 +09:00
|
|
|
|
2025-01-08 18:31:59 +09:00
|
|
|
impl Display for Token {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
match self {
|
2025-01-24 04:08:59 +09:00
|
|
|
Self::Value { name, .. } => write!(f, "{name}"),
|
2025-01-24 05:16:15 +09:00
|
|
|
Self::Parameter(param) => write!(f, "{param}"),
|
2025-01-08 18:31:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 05:16:15 +09:00
|
|
|
// (name, aliases) -> Token::Value
|
2025-01-24 04:08:59 +09:00
|
|
|
impl<const L: usize> From<(&str, [&str; L])> for Token {
|
|
|
|
|
fn from((name, aliases): (&str, [&str; L])) -> Self {
|
|
|
|
|
Self::Value {
|
|
|
|
|
name: name.into(),
|
|
|
|
|
aliases: aliases.into_iter().map(SmolStr::new).collect::<Vec<_>>(),
|
2025-01-22 02:12:17 +09:00
|
|
|
}
|
2025-01-21 00:39:25 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 05:16:15 +09:00
|
|
|
// name -> Token::Value
|
2025-01-24 04:08:59 +09:00
|
|
|
impl From<&str> for Token {
|
|
|
|
|
fn from(value: &str) -> Self {
|
|
|
|
|
Self::from((value, []))
|
2025-01-05 00:59:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 15:50:54 +00:00
|
|
|
// parameter -> Token::Parameter
|
|
|
|
|
impl<P: Into<Parameter>> From<P> for Token {
|
|
|
|
|
fn from(value: P) -> Self {
|
|
|
|
|
Self::Parameter(value.into())
|
2025-01-24 04:08:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 05:16:15 +09:00
|
|
|
/// Iterator that produces [`Token`]s.
|
|
|
|
|
///
|
|
|
|
|
/// This is more of a convenience type that the [`tokens!`] macro uses in order
|
|
|
|
|
/// to more easily combine tokens together.
|
2025-01-24 04:08:59 +09:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct TokensIterator {
|
|
|
|
|
inner: Vec<Token>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 05:16:15 +09:00
|
|
|
impl TokensIterator {
|
|
|
|
|
pub(crate) fn new(tokens: Vec<Token>) -> Self {
|
|
|
|
|
Self { inner: tokens }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 04:08:59 +09:00
|
|
|
impl Iterator for TokensIterator {
|
|
|
|
|
type Item = Token;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
(self.inner.len() > 0).then(|| self.inner.remove(0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 05:16:15 +09:00
|
|
|
impl From<Vec<Token>> for TokensIterator {
|
|
|
|
|
fn from(value: Vec<Token>) -> Self {
|
|
|
|
|
Self::new(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 04:08:59 +09:00
|
|
|
impl<T: Into<Token>> From<T> for TokensIterator {
|
|
|
|
|
fn from(value: T) -> Self {
|
2025-01-24 05:16:15 +09:00
|
|
|
Self::new(vec![value.into()])
|
2025-01-08 18:31:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-24 04:08:59 +09:00
|
|
|
|
|
|
|
|
impl<const L: usize> From<[Token; L]> for TokensIterator {
|
|
|
|
|
fn from(value: [Token; L]) -> Self {
|
2025-01-24 05:16:15 +09:00
|
|
|
Self::new(value.into_iter().collect())
|
2025-01-24 04:08:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<const L: usize> From<[Self; L]> for TokensIterator {
|
|
|
|
|
fn from(value: [Self; L]) -> Self {
|
2025-01-24 05:16:15 +09:00
|
|
|
Self::new(value.into_iter().map(|t| t.inner).flatten().collect())
|
2025-01-24 04:08:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! tokens {
|
|
|
|
|
($($v:expr),+$(,)*) => {
|
|
|
|
|
$crate::token::TokensIterator::from([$($crate::token::TokensIterator::from($v.clone())),+])
|
|
|
|
|
};
|
|
|
|
|
}
|