diff options
| author | acdenisSK <[email protected]> | 2018-06-08 15:21:27 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2018-06-08 15:22:52 +0200 |
| commit | caeab28059d029a92b784f3b5ae1f79c412c8404 (patch) | |
| tree | 8cd41b07fbd710befe11f05a026b184b159700c4 /src/framework | |
| parent | Bump to v0.5.4 (diff) | |
| download | serenity-caeab28059d029a92b784f3b5ae1f79c412c8404.tar.xz serenity-caeab28059d029a92b784f3b5ae1f79c412c8404.zip | |
Handle debug impls better
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/standard/command.rs | 48 | ||||
| -rw-r--r-- | src/framework/standard/create_command.rs | 5 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 3 |
3 files changed, 30 insertions, 26 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 8c35451..d8fc81e 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -12,11 +12,29 @@ use std::{ use utils::Colour; use super::{Args, Configuration, HelpBehaviour}; -pub type Check = Fn(&mut Context, &Message, &mut Args, &CommandOptions) -> bool +type CheckFunction = Fn(&mut Context, &Message, &mut Args, &CommandOptions) -> bool + Send + Sync + 'static; +pub struct Check(pub(crate) Box<CheckFunction>); + +impl Check { + pub(crate) fn new<F: Send + Sync + 'static>(f: F) -> Self + where F: Fn(&mut Context, &Message, &mut Args, &CommandOptions) -> bool + { + Check(Box::new(f)) + } +} + +impl Debug for Check { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.debug_tuple("Check") + .field(&"<fn>") + .finish() + } +} + pub type HelpFunction = fn(&mut Context, &Message, &HelpOptions, HashMap<String, Arc<CommandGroup>>, &Args) -> Result<(), Error>; @@ -24,7 +42,9 @@ pub struct Help(pub HelpFunction, pub Arc<HelpOptions>); impl Debug for Help { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "fn()") + f.debug_struct("Help") + .field("options", &self.1) + .finish() } } @@ -49,7 +69,7 @@ impl fmt::Debug for CommandOrAlias { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { CommandOrAlias::Alias(ref s) => f.debug_tuple("CommandOrAlias::Alias").field(&s).finish(), - _ => Ok(()) + CommandOrAlias::Command(ref arc) => f.debug_tuple("CommandOrAlias::Command").field(&arc.options()).finish(), } } } @@ -97,10 +117,11 @@ impl Default for CommandGroup { } } +#[derive(Debug)] pub struct CommandOptions { /// A set of checks to be called prior to executing the command. The checks /// will short-circuit on the first check that returns `false`. - pub checks: Vec<Box<Check>>, + pub checks: Vec<Check>, /// Ratelimit bucket. pub bucket: Option<String>, /// Command description, used by other commands. @@ -287,25 +308,6 @@ impl<F> Command for F where F: Fn(&mut Context, &Message, Args) -> Result<(), Er } } -impl fmt::Debug for CommandOptions { - // TODO: add CommandOptions::checks somehow? - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt.debug_struct("CommandOptions") - .field("bucket", &self.bucket) - .field("desc", &self.desc) - .field("example", &self.example) - .field("usage", &self.usage) - .field("min_args", &self.min_args) - .field("required_permissions", &self.required_permissions) - .field("allowed_roles", &self.allowed_roles) - .field("help_available", &self.help_available) - .field("dm_only", &self.dm_only) - .field("guild_only", &self.guild_only) - .field("owners_only", &self.owners_only) - .finish() - } -} - impl Default for CommandOptions { fn default() -> CommandOptions { CommandOptions { diff --git a/src/framework/standard/create_command.rs b/src/framework/standard/create_command.rs index bc85eb6..cba89c5 100644 --- a/src/framework/standard/create_command.rs +++ b/src/framework/standard/create_command.rs @@ -3,7 +3,8 @@ pub use super::{ Command, CommandGroup, CommandOptions, - CommandError + CommandError, + Check, }; use client::Context; @@ -108,7 +109,7 @@ impl CreateCommand { + Send + Sync + 'static { - self.0.checks.push(Box::new(check)); + self.0.checks.push(Check::new(check)); self } diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 321abc6..83ae765 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -17,6 +17,7 @@ pub use self::args::{ pub(crate) use self::buckets::{Bucket, Ratelimit}; pub(crate) use self::command::Help; pub use self::command::{ + Check, HelpFunction, HelpOptions, Command, @@ -625,7 +626,7 @@ impl StandardFramework { let all_passed = command .checks .iter() - .all(|check| check(&mut context, message, args, command)); + .all(|check| (check.0)(&mut context, message, args, command)); if all_passed { None |