diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ext/framework/command.rs | 25 | ||||
| -rw-r--r-- | src/ext/framework/configuration.rs | 3 | ||||
| -rw-r--r-- | src/ext/framework/mod.rs | 6 | ||||
| -rw-r--r-- | src/utils/mod.rs | 28 |
4 files changed, 33 insertions, 29 deletions
diff --git a/src/ext/framework/command.rs b/src/ext/framework/command.rs index 225f50a..b7d5bb8 100644 --- a/src/ext/framework/command.rs +++ b/src/ext/framework/command.rs @@ -4,19 +4,27 @@ use ::client::Context; use ::model::Message; use std::collections::HashMap; +pub type Check = Fn(&Context, &Message) -> bool + Send + Sync + 'static; +pub type Exec = Fn(&Context, &Message, Vec<String>) + Send + Sync + 'static; +pub type Help = Fn(&Context, &Message, HashMap<String, Arc<Command>>, Vec<String>) + Send + Sync + 'static; +pub type Hook = Fn(&Context, &Message, &String) + Send + Sync + 'static; +#[doc(hidden)] +pub type InternalCommand = Arc<Command>; +pub type PrefixCheck = Fn(&Context) -> Option<String> + Send + Sync + 'static; + /// Command function type. Allows to access internal framework things inside /// your commands. pub enum CommandType { StringResponse(String), - Basic(Box<Fn(&Context, &Message, Vec<String>) + Send + Sync + 'static>), - WithCommands(Box<Fn(&Context, &Message, HashMap<String, Arc<Command>>, Vec<String>) + Send + Sync + 'static>) + Basic(Box<Exec>), + WithCommands(Box<Help>), } /// Command struct used to store commands internally. pub struct Command { /// 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<Fn(&Context, &Message) -> bool + Send + Sync + 'static>>, + pub checks: Vec<Box<Check>>, /// Function called when the command is called. pub exec: CommandType, /// Command description, used by other commands. @@ -27,19 +35,16 @@ pub struct Command { pub use_quotes: bool, } -#[doc(hidden)] -pub type InternalCommand = Arc<Command>; - pub fn positions(ctx: &Context, content: &str, conf: &Configuration) -> Option<Vec<usize>> { - if conf.prefixes.len() > 0 || conf.dynamic_prefix.is_some() { + if !conf.prefixes.is_empty() || conf.dynamic_prefix.is_some() { // Find out if they were mentioned. If not, determine if the prefix // was used. If not, return None. let mut positions: Vec<usize> = vec![]; - if let Some(mention_end) = find_mention_end(&content, conf) { + if let Some(mention_end) = find_mention_end(content, conf) { positions.push(mention_end); } else if let Some(ref func) = conf.dynamic_prefix { - if let Some(x) = func(&ctx) { + if let Some(x) = func(ctx) { positions.push(x.len()); } else { for n in conf.prefixes.clone() { @@ -56,7 +61,7 @@ pub fn positions(ctx: &Context, content: &str, conf: &Configuration) -> Option<V } }; - if positions.len() == 0 { + if positions.is_empty() { return None; } diff --git a/src/ext/framework/configuration.rs b/src/ext/framework/configuration.rs index 7af38ed..9caa48b 100644 --- a/src/ext/framework/configuration.rs +++ b/src/ext/framework/configuration.rs @@ -1,4 +1,5 @@ use std::default::Default; +use super::command::PrefixCheck; use ::client::rest; use ::client::Context; @@ -34,7 +35,7 @@ pub struct Configuration { #[doc(hidden)] pub prefixes: Vec<String>, #[doc(hidden)] - pub dynamic_prefix: Option<Box<Fn(&Context) -> Option<String> + Send + Sync + 'static>> + pub dynamic_prefix: Option<Box<PrefixCheck>>, } impl Configuration { diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs index 7458223..077f767 100644 --- a/src/ext/framework/mod.rs +++ b/src/ext/framework/mod.rs @@ -61,7 +61,7 @@ pub use self::command::{Command, CommandType}; pub use self::configuration::Configuration; pub use self::create_command::CreateCommand; -use self::command::InternalCommand; +use self::command::{Hook, InternalCommand}; use std::collections::HashMap; use std::sync::Arc; use std::thread; @@ -138,8 +138,8 @@ macro_rules! command { pub struct Framework { configuration: Configuration, commands: HashMap<String, InternalCommand>, - before: Option<Arc<Fn(&Context, &Message, &String) + Send + Sync + 'static>>, - after: Option<Arc<Fn(&Context, &Message, &String) + Send + Sync + 'static>>, + before: Option<Arc<Hook>>, + after: Option<Arc<Hook>>, /// Whether the framework has been "initialized". /// /// The framework is initialized once one of the following occurs: diff --git a/src/utils/mod.rs b/src/utils/mod.rs index fbc642f..f0e4d3a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -218,23 +218,21 @@ pub fn parse_quotes(s: &str) -> Vec<String> { } else { current_str.push(x); } - } else { - if x == ' ' { - if !current_str.is_empty() { - args.push(current_str.clone()); - } - - current_str = String::default(); - } else if x == '"' { - if !current_str.is_empty() { - args.push(current_str.clone()); - } + } else if x == ' ' { + if !current_str.is_empty() { + args.push(current_str.clone()); + } - in_string = true; - current_str = String::default(); - } else { - current_str.push(x); + current_str = String::default(); + } else if x == '"' { + if !current_str.is_empty() { + args.push(current_str.clone()); } + + in_string = true; + current_str = String::default(); + } else { + current_str.push(x); } } |