aboutsummaryrefslogtreecommitdiff
path: root/src/framework/standard/command.rs
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-09-23 23:16:26 +0200
committeracdenisSK <[email protected]>2017-09-23 23:16:26 +0200
commitab67c1dd60b5f49541815b2527e8a3cb7712e182 (patch)
treeb7d7f1d5c0342c80488972e4e0157dc964a9baea /src/framework/standard/command.rs
parentDowngrade sodiumoxide to v0.0.14 (diff)
downloadserenity-ab67c1dd60b5f49541815b2527e8a3cb7712e182.tar.xz
serenity-ab67c1dd60b5f49541815b2527e8a3cb7712e182.zip
Revamp errors in `Args` and commands
Diffstat (limited to 'src/framework/standard/command.rs')
-rw-r--r--src/framework/standard/command.rs63
1 files changed, 54 insertions, 9 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs
index 96c2f7d..aa8f7d8 100644
--- a/src/framework/standard/command.rs
+++ b/src/framework/standard/command.rs
@@ -3,19 +3,21 @@ use super::{Args, Configuration};
use client::Context;
use model::{Message, Permissions};
use std::collections::HashMap;
+use std::error::Error as StdError;
+use std::fmt;
pub type Check = Fn(&mut Context, &Message, &mut Args, &Arc<Command>) -> bool
+ Send
+ Sync
+ 'static;
-pub type Exec = Fn(&mut Context, &Message, Args) -> Result<(), String> + Send + Sync + 'static;
+pub type Exec = Fn(&mut Context, &Message, Args) -> Result<(), Error> + Send + Sync + 'static;
pub type Help = Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args)
- -> Result<(), String>
+ -> Result<(), Error>
+ Send
+ Sync
+ 'static;
pub type BeforeHook = Fn(&mut Context, &Message, &str) -> bool + Send + Sync + 'static;
-pub type AfterHook = Fn(&mut Context, &Message, &str, Result<(), String>) + Send + Sync + 'static;
+pub type AfterHook = Fn(&mut Context, &Message, &str, Result<(), Error>) + Send + Sync + 'static;
pub(crate) type InternalCommand = Arc<Command>;
pub type PrefixCheck = Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static;
@@ -24,6 +26,40 @@ pub enum CommandOrAlias {
Command(InternalCommand),
}
+/// An error from a command.
+#[derive(Clone, Debug)]
+pub struct Error(String);
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(&self.0, f)
+ }
+}
+
+impl<E: StdError> From<E> for Error {
+ fn from(e: E) -> Self {
+ Error(format!("{}", e))
+ }
+}
+
+impl From<Custom> for Error {
+ fn from(Custom(e): Custom) -> Self {
+ Error(e)
+ }
+}
+
+/// A custom "variant" of [`Error`]
+///
+/// [`Error`]: #struct.Error.html
+#[derive(Clone, Debug)]
+pub struct Custom(String);
+
+impl From<String> for Custom {
+ fn from(e: String) -> Custom {
+ Custom(e)
+ }
+}
+
/// Command function type. Allows to access internal framework things inside
/// your commands.
pub enum CommandType {
@@ -81,22 +117,31 @@ pub struct Command {
impl Command {
pub fn new<F>(f: F) -> Self
- where F: Fn(&mut Context, &Message, Args) -> Result<(), String> + Send + Sync + 'static {
+ where F: Fn(&mut Context, &Message, Args) -> Result<(), Error> + Send + Sync + 'static {
+ Command {
+ exec: CommandType::Basic(Box::new(f)),
+ ..Command::default()
+ }
+ }
+}
+
+impl Default for Command {
+ fn default() -> Command {
Command {
aliases: Vec::new(),
checks: Vec::default(),
- exec: CommandType::Basic(Box::new(f)),
+ exec: CommandType::Basic(Box::new(|_, _, _| Ok(()))),
desc: None,
usage: None,
example: None,
- dm_only: false,
+ min_args: None,
bucket: None,
+ max_args: None,
+ required_permissions: Permissions::empty(),
+ dm_only: false,
guild_only: false,
help_available: true,
- min_args: None,
- max_args: None,
owners_only: false,
- required_permissions: Permissions::empty(),
allowed_roles: Vec::new(),
}
}