use RudeResult and take generic errors

master
rasul 5 years ago
parent f917df3ea1
commit c84f2e8c5b

@ -6,6 +6,7 @@ use strum_macros::{Display, EnumIter};
use crate::command::{CommandNew, CommandSet, Parse, ParserError}; use crate::command::{CommandNew, CommandSet, Parse, ParserError};
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
use crate::result::RudeResult;
/// Root level of command. /// Root level of command.
/// ///
@ -72,7 +73,7 @@ impl Parse for Command {
} }
} }
fn parse_subcommand(&self, s: String) -> Result<(Self, String), ParserError> { fn parse_subcommand(&self, s: String) -> RudeResult<(Self, String)> {
match self { match self {
Self::New(_) => { Self::New(_) => {
let (command, args) = CommandNew::parse(s)?; let (command, args) = CommandNew::parse(s)?;
@ -82,7 +83,7 @@ impl Parse for Command {
let (command, args) = CommandSet::parse(s)?; let (command, args) = CommandSet::parse(s)?;
Ok((Self::Set(command), args)) Ok((Self::Set(command), args))
} }
Self::Default => Err(ParserError::Default), Self::Default => Err(ParserError::Default.into()),
_ => Ok((self.clone(), s)), _ => Ok((self.clone(), s)),
} }
} }

@ -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)?)
} }

@ -6,6 +6,7 @@ use strum_macros::{Display, EnumIter};
use crate::command::{CommandSetRoomDescription, Parse, ParserError}; use crate::command::{CommandSetRoomDescription, Parse, ParserError};
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
use crate::result::RudeResult;
/// Set room properties /// Set room properties
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
@ -38,13 +39,13 @@ impl Parse for CommandSetRoom {
} }
} }
fn parse_subcommand(&self, s: String) -> Result<(Self, String), ParserError> { fn parse_subcommand(&self, s: String) -> RudeResult<(Self, String)> {
match self { match self {
Self::Description(_) => { Self::Description(_) => {
let (command, args) = CommandSetRoomDescription::parse(s)?; let (command, args) = CommandSetRoomDescription::parse(s)?;
Ok((Self::Description(command), args)) Ok((Self::Description(command), args))
} }
Self::Default => Err(ParserError::Default), Self::Default => Err(ParserError::Default.into()),
_ => Ok((self.clone(), s)), _ => Ok((self.clone(), s)),
} }
} }

@ -6,6 +6,7 @@ use strum_macros::{Display, EnumIter};
use crate::command::{CommandSetPlayer, CommandSetRoom, CommandSetZone, Parse, ParserError}; use crate::command::{CommandSetPlayer, CommandSetRoom, CommandSetZone, Parse, ParserError};
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
use crate::result::RudeResult;
/// Set various options /// Set various options
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
@ -38,7 +39,7 @@ impl Parse for CommandSet {
} }
} }
fn parse_subcommand(&self, s: String) -> Result<(Self, String), ParserError> { fn parse_subcommand(&self, s: String) -> RudeResult<(Self, String)> {
match self { match self {
Self::Player(_) => { Self::Player(_) => {
let (command, args) = CommandSetPlayer::parse(s)?; let (command, args) = CommandSetPlayer::parse(s)?;
@ -52,7 +53,7 @@ impl Parse for CommandSet {
let (command, args) = CommandSetZone::parse(s)?; let (command, args) = CommandSetZone::parse(s)?;
Ok((Self::Zone(command), args)) Ok((Self::Zone(command), args))
} }
Self::Default => Err(ParserError::Default), Self::Default => Err(ParserError::Default.into()),
} }
} }

Loading…
Cancel
Save