From c84f2e8c5befd9eef9df85a89b52d05551ab6e35 Mon Sep 17 00:00:00 2001 From: rasul Date: Wed, 8 Apr 2020 14:53:21 -0500 Subject: [PATCH] use RudeResult and take generic errors --- src/command/command.rs | 5 +++-- src/command/parse.rs | 11 ++++++----- src/command/set/room/room.rs | 5 +++-- src/command/set/set.rs | 5 +++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/command/command.rs b/src/command/command.rs index bc565dc..509ae8f 100644 --- a/src/command/command.rs +++ b/src/command/command.rs @@ -6,6 +6,7 @@ use strum_macros::{Display, EnumIter}; use crate::command::{CommandNew, CommandSet, Parse, ParserError}; use crate::database::Db; use crate::queue::SendQueue; +use crate::result::RudeResult; /// 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 { Self::New(_) => { let (command, args) = CommandNew::parse(s)?; @@ -82,7 +83,7 @@ impl Parse for Command { let (command, args) = CommandSet::parse(s)?; Ok((Self::Set(command), args)) } - Self::Default => Err(ParserError::Default), + Self::Default => Err(ParserError::Default.into()), _ => Ok((self.clone(), s)), } } diff --git a/src/command/parse.rs b/src/command/parse.rs index 591696a..75f5bbd 100644 --- a/src/command/parse.rs +++ b/src/command/parse.rs @@ -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` into a command. - fn parse>(s: S) -> Result<(Self, String), ParserError> { + fn parse>(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 = 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)?) } diff --git a/src/command/set/room/room.rs b/src/command/set/room/room.rs index 70a29e6..63041db 100644 --- a/src/command/set/room/room.rs +++ b/src/command/set/room/room.rs @@ -6,6 +6,7 @@ use strum_macros::{Display, EnumIter}; use crate::command::{CommandSetRoomDescription, Parse, ParserError}; use crate::database::Db; use crate::queue::SendQueue; +use crate::result::RudeResult; /// Set room properties #[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 { Self::Description(_) => { let (command, args) = CommandSetRoomDescription::parse(s)?; Ok((Self::Description(command), args)) } - Self::Default => Err(ParserError::Default), + Self::Default => Err(ParserError::Default.into()), _ => Ok((self.clone(), s)), } } diff --git a/src/command/set/set.rs b/src/command/set/set.rs index 9c0a91b..725f7bf 100644 --- a/src/command/set/set.rs +++ b/src/command/set/set.rs @@ -6,6 +6,7 @@ use strum_macros::{Display, EnumIter}; use crate::command::{CommandSetPlayer, CommandSetRoom, CommandSetZone, Parse, ParserError}; use crate::database::Db; use crate::queue::SendQueue; +use crate::result::RudeResult; /// Set various options #[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 { Self::Player(_) => { let (command, args) = CommandSetPlayer::parse(s)?; @@ -52,7 +53,7 @@ impl Parse for CommandSet { let (command, args) = CommandSetZone::parse(s)?; Ok((Self::Zone(command), args)) } - Self::Default => Err(ParserError::Default), + Self::Default => Err(ParserError::Default.into()), } }