|
|
|
@ -6,6 +6,7 @@ use strum::IntoEnumIterator;
|
|
|
|
|
use crate::command::ParserError;
|
|
|
|
|
use crate::database::Db;
|
|
|
|
|
use crate::queue::SendQueue;
|
|
|
|
|
use crate::result::RudeResult;
|
|
|
|
|
|
|
|
|
|
/// Command parser.
|
|
|
|
|
///
|
|
|
|
@ -21,7 +22,7 @@ pub trait Parse: Clone + Default + IntoEnumIterator + PartialEq + ToString {
|
|
|
|
|
|
|
|
|
|
/// Check if there's a subcommand. The default implementation does not support subcommands.
|
|
|
|
|
/// If subcommands are necessary, you must implement the `subcommand()` function.
|
|
|
|
|
fn parse_subcommand(&self, s: String) -> Result<(Self, String), ParserError> {
|
|
|
|
|
fn parse_subcommand(&self, s: String) -> RudeResult<(Self, String)> {
|
|
|
|
|
Ok((self.clone(), s))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -84,7 +85,7 @@ pub trait Parse: Clone + Default + IntoEnumIterator + PartialEq + ToString {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parse a `Into<String>` into a command.
|
|
|
|
|
fn parse<S: Into<String>>(s: S) -> Result<(Self, String), ParserError> {
|
|
|
|
|
fn parse<S: Into<String>>(s: S) -> RudeResult<(Self, String)> {
|
|
|
|
|
let mut s = s.into();
|
|
|
|
|
|
|
|
|
|
// get the text command and the args
|
|
|
|
@ -100,7 +101,7 @@ pub trait Parse: Clone + Default + IntoEnumIterator + PartialEq + ToString {
|
|
|
|
|
|
|
|
|
|
// no command
|
|
|
|
|
if s.is_empty() {
|
|
|
|
|
return Err(ParserError::Empty);
|
|
|
|
|
return Err(ParserError::Empty.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut matches: Vec<Self> = Vec::new();
|
|
|
|
@ -117,7 +118,7 @@ pub trait Parse: Clone + Default + IntoEnumIterator + PartialEq + ToString {
|
|
|
|
|
|
|
|
|
|
// check if there was a match
|
|
|
|
|
if matches.is_empty() {
|
|
|
|
|
return Err(ParserError::Unknown);
|
|
|
|
|
return Err(ParserError::Unknown.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sort so the first match is the best
|
|
|
|
@ -125,7 +126,7 @@ pub trait Parse: Clone + Default + IntoEnumIterator + PartialEq + ToString {
|
|
|
|
|
|
|
|
|
|
// default is an error
|
|
|
|
|
if matches[0] == Self::default() {
|
|
|
|
|
Err(ParserError::Default)
|
|
|
|
|
Err(ParserError::Default.into())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(matches[0].parse_subcommand(args)?)
|
|
|
|
|
}
|
|
|
|
|