mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-13 09:10:14 +00:00
refactor(commands): ToToken trait for easier conversion into tokens
This commit is contained in:
parent
e70d69e45c
commit
ff121ecc51
6 changed files with 49 additions and 23 deletions
|
|
@ -20,7 +20,10 @@ pub mod system;
|
||||||
|
|
||||||
use smol_str::SmolStr;
|
use smol_str::SmolStr;
|
||||||
|
|
||||||
use crate::{command, token::Token};
|
use crate::{
|
||||||
|
command,
|
||||||
|
token::{ToToken, Token},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Command {
|
pub struct Command {
|
||||||
|
|
@ -47,7 +50,7 @@ impl Command {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! command {
|
macro_rules! command {
|
||||||
([$($v:expr),+], $cb:expr, $help:expr) => {
|
([$($v:expr),+], $cb:expr, $help:expr) => {
|
||||||
$crate::commands::Command::new([$($v.clone()),*], $help, $cb)
|
$crate::commands::Command::new([$($v.to_token()),*], $help, $cb)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,5 +58,6 @@ pub fn all() -> Vec<Command> {
|
||||||
(help::cmds())
|
(help::cmds())
|
||||||
.chain(system::cmds())
|
.chain(system::cmds())
|
||||||
.chain(member::cmds())
|
.chain(member::cmds())
|
||||||
|
.chain(fun::cmds())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use super::*;
|
||||||
|
|
||||||
pub fn cmds() -> impl Iterator<Item = Command> {
|
pub fn cmds() -> impl Iterator<Item = Command> {
|
||||||
[command!(
|
[command!(
|
||||||
[Token::cmd("thunder")],
|
["thunder"],
|
||||||
"fun_thunder",
|
"fun_thunder",
|
||||||
"Shows the help command"
|
"Shows the help command"
|
||||||
)]
|
)]
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,18 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub fn cmds() -> impl Iterator<Item = Command> {
|
pub fn cmds() -> impl Iterator<Item = Command> {
|
||||||
[command!(
|
let help = ["help", "h"];
|
||||||
[Token::cmd("help")],
|
[
|
||||||
"help",
|
command!(
|
||||||
"Shows the help command"
|
[help],
|
||||||
)]
|
"help",
|
||||||
|
"Shows the help command"
|
||||||
|
),
|
||||||
|
command!(
|
||||||
|
[help, "commands"],
|
||||||
|
"help_commands",
|
||||||
|
"Commands"
|
||||||
|
),
|
||||||
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@ use super::*;
|
||||||
pub fn cmds() -> impl Iterator<Item = Command> {
|
pub fn cmds() -> impl Iterator<Item = Command> {
|
||||||
use Token::*;
|
use Token::*;
|
||||||
|
|
||||||
let member = Token::cmd_with_alias(["member", "m"]);
|
let member = ["member", "m"];
|
||||||
let description = Token::cmd_with_alias(["description", "desc"]);
|
let description = ["description", "desc"];
|
||||||
let privacy = Token::cmd_with_alias(["privacy", "priv"]);
|
let privacy = ["privacy", "priv"];
|
||||||
let new = Token::cmd_with_alias(["new", "n"]);
|
let new = ["new", "n"];
|
||||||
|
|
||||||
[
|
[
|
||||||
command!(
|
command!(
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@ use super::*;
|
||||||
pub fn cmds() -> impl Iterator<Item = Command> {
|
pub fn cmds() -> impl Iterator<Item = Command> {
|
||||||
use Token::*;
|
use Token::*;
|
||||||
|
|
||||||
let system = Token::cmd_with_alias(["system", "s"]);
|
let system = ["system", "s"];
|
||||||
let new = Token::cmd_with_alias(["new", "n"]);
|
let new = ["new", "n"];
|
||||||
|
|
||||||
[
|
[
|
||||||
command!(
|
command!(
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use smol_str::SmolStr;
|
use smol_str::{SmolStr, ToSmolStr};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
|
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
|
|
@ -41,14 +41,6 @@ lazy_static::lazy_static!(
|
||||||
);
|
);
|
||||||
|
|
||||||
impl Token {
|
impl Token {
|
||||||
pub fn cmd(value: impl Into<SmolStr>) -> Self {
|
|
||||||
Self::Value(vec![value.into()])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn cmd_with_alias(value: impl IntoIterator<Item = impl Into<SmolStr>>) -> Self {
|
|
||||||
Self::Value(value.into_iter().map(Into::into).collect())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn try_match(&self, input: Option<SmolStr>) -> TokenMatchResult {
|
pub fn try_match(&self, input: Option<SmolStr>) -> TokenMatchResult {
|
||||||
// short circuit on empty things
|
// short circuit on empty things
|
||||||
if matches!(self, Self::Empty) && input.is_none() {
|
if matches!(self, Self::Empty) && input.is_none() {
|
||||||
|
|
@ -94,3 +86,25 @@ impl Token {
|
||||||
return TokenMatchResult::NoMatch;
|
return TokenMatchResult::NoMatch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait ToToken {
|
||||||
|
fn to_token(&self) -> Token;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToToken for Token {
|
||||||
|
fn to_token(&self) -> Token {
|
||||||
|
self.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToToken for &str {
|
||||||
|
fn to_token(&self) -> Token {
|
||||||
|
Token::Value(vec![self.to_smolstr()])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToToken for [&str] {
|
||||||
|
fn to_token(&self) -> Token {
|
||||||
|
Token::Value(self.into_iter().map(|s| s.to_smolstr()).collect())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue