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