aboutsummaryrefslogtreecommitdiff
path: root/src/framework/standard/create_command.rs
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-11-18 12:06:22 +0100
committeracdenisSK <[email protected]>2017-11-18 12:06:22 +0100
commite748d1ff80dbbeb82b23f8ac9fec9cf8c7e4a69e (patch)
tree10f6dfe469e5362a6936082890d5d7992e401d63 /src/framework/standard/create_command.rs
parentUse a private function to reduce repetition (diff)
downloadserenity-e748d1ff80dbbeb82b23f8ac9fec9cf8c7e4a69e.tar.xz
serenity-e748d1ff80dbbeb82b23f8ac9fec9cf8c7e4a69e.zip
Add `cmd` to `Create(Command|Group)`
Diffstat (limited to 'src/framework/standard/create_command.rs')
-rw-r--r--src/framework/standard/create_command.rs41
1 files changed, 30 insertions, 11 deletions
diff --git a/src/framework/standard/create_command.rs b/src/framework/standard/create_command.rs
index e5c9208..4137b91 100644
--- a/src/framework/standard/create_command.rs
+++ b/src/framework/standard/create_command.rs
@@ -4,7 +4,12 @@ use client::Context;
use model::{Message, Permissions};
use std::sync::Arc;
-pub struct CreateCommand(pub CommandOptions, pub fn(&mut Context, &Message, Args) -> Result<(), CommandError>);
+pub enum FnOrCommand {
+ Fn(fn(&mut Context, &Message, Args) -> Result<(), CommandError>),
+ Command(Arc<Command>),
+}
+
+pub struct CreateCommand(pub CommandOptions, pub FnOrCommand);
impl CreateCommand {
/// Adds multiple aliases.
@@ -104,10 +109,19 @@ impl CreateCommand {
/// A function that can be called when a command is received.
/// You can return `Err(string)` if there's an error.
pub fn exec(mut self, func: fn(&mut Context, &Message, Args) -> Result<(), CommandError>) -> Self {
- self.1 = func;
+ self.1 = FnOrCommand::Fn(func);
self
}
+
+ /// Like [`exec`] but accepts a `Command` directly.
+ ///
+ /// [`exec`]: #method.exec
+ pub fn cmd<C: Command + 'static>(mut self, c: C) -> Self {
+ self.1 = FnOrCommand::Command(Arc::new(c));
+
+ self
+ }
/// Whether command can be used only in guilds or not.
pub fn guild_only(mut self, guild_only: bool) -> Self {
@@ -182,19 +196,24 @@ impl CreateCommand {
}
pub(crate) fn finish(self) -> Arc<Command> {
- let CreateCommand(options, func) = self;
+ let CreateCommand(options, fc) = self;
- struct A(Arc<CommandOptions>, fn(&mut Context, &Message, Args) -> Result<(), CommandError>);
+ match fc {
+ FnOrCommand::Fn(func) => {
+ struct A(Arc<CommandOptions>, fn(&mut Context, &Message, Args) -> Result<(), CommandError>);
- impl Command for A {
- fn execute(&self, c: &mut Context, m: &Message, a: Args) -> Result<(), CommandError> {
- (self.1)(c, m, a)
- }
+ impl Command for A {
+ fn execute(&self, c: &mut Context, m: &Message, a: Args) -> Result<(), CommandError> {
+ (self.1)(c, m, a)
+ }
- fn options(&self) -> Arc<CommandOptions> { Arc::clone(&self.0) }
- }
+ fn options(&self) -> Arc<CommandOptions> { Arc::clone(&self.0) }
+ }
- Arc::new(A(Arc::new(options), func))
+ Arc::new(A(Arc::new(options), func))
+ },
+ FnOrCommand::Command(cmd) => cmd,
+ }
}
}