Skip to content

Commit

Permalink
Handle debug impls better
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Jun 8, 2018
1 parent 5d14da4 commit caeab28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
48 changes: 25 additions & 23 deletions src/framework/standard/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,39 @@ 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>;

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()
}
}

Expand All @@ -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(),
}
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/framework/standard/create_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pub use super::{
Command,
CommandGroup,
CommandOptions,
CommandError
CommandError,
Check,
};

use client::Context;
Expand Down Expand Up @@ -108,7 +109,7 @@ impl CreateCommand {
+ Send
+ Sync
+ 'static {
self.0.checks.push(Box::new(check));
self.0.checks.push(Check::new(check));

self
}
Expand Down
3 changes: 2 additions & 1 deletion src/framework/standard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit caeab28

Please sign in to comment.