Skip to content

Commit

Permalink
Fix string delimiters (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakelezz authored and arqunis committed Aug 13, 2017
1 parent e5889ed commit 069df4f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ version = "~0.10"
optional = true
version = "0.2.2"

[dependencies.regex]
optional = true
regex = "0.2"

[dependencies.itertools]
optional = true
itertools = "0.6.1"

[dependencies.lazy_static]
optional = true
version = "~0.2"
Expand Down Expand Up @@ -93,7 +101,7 @@ builder = []
cache = ["lazy_static"]
client = ["gateway", "lazy_static", "http", "typemap"]
extras = []
framework = ["client", "model", "utils"]
framework = ["client", "model", "utils", "regex", "itertools"]
builtin_framework = ["framework"]
gateway = ["http", "websocket", "tokio-core", "futures"]
http = ["hyper", "hyper-native-tls", "lazy_static", "multipart", "native-tls"]
Expand Down
24 changes: 13 additions & 11 deletions src/framework/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct Configuration {
#[doc(hidden)]
pub prefixes: Vec<String>,
#[doc(hidden)]
pub delimeters: Vec<String>,
pub delimiters: Vec<String>,
}

impl Configuration {
Expand Down Expand Up @@ -370,7 +370,7 @@ impl Configuration {
self
}

/// Sets a delimeter to be used when splitting the content after a command.
/// Sets a delimiter to be used when splitting the content after a command.
///
/// # Examples
///
Expand All @@ -386,15 +386,16 @@ impl Configuration {
/// use serenity::framework::BuiltinFramework;
///
/// client.with_framework(BuiltinFramework::new().configure(|c| c
/// .delimeter(", ")));
/// .delimiter(", ")));
/// ```
pub fn delimeter(mut self, delimeter: &str) -> Self {
self.delimeters.push(delimeter.to_string());
pub fn delimiter(mut self, delimiter: &str) -> Self {
self.delimiters.push(delimiter.to_string());

self
}

/// Sets multiple delimeters to be used when splitting the content after a command.
/// Sets multiple delimiters to be used when splitting the content after a command.
/// Additionally cleans the default delimiter from the vector.
///
/// # Examples
///
Expand All @@ -410,10 +411,11 @@ impl Configuration {
/// use serenity::framework::BuiltinFramework;
///
/// client.with_framework(BuiltinFramework::new().configure(|c| c
/// .delimeters(vec![", ", " "])));
/// .delimiters(vec![", ", " "])));
/// ```
pub fn delimeters(mut self, delimeters: Vec<&str>) -> Self {
self.delimeters.extend(delimeters.into_iter().map(|s| s.to_string()));
pub fn delimiters(mut self, delimiters: Vec<&str>) -> Self {
self.delimiters.clear();
self.delimiters.extend(delimiters.into_iter().map(|s| s.to_string()));

self
}
Expand All @@ -426,7 +428,7 @@ impl Default for Configuration {
/// - **depth** to `5`
/// - **on_mention** to `false` (basically)
/// - **prefix** to `None`
/// - **delimeters** to vec![" "]
/// - **delimiters** to vec![" "]
fn default() -> Configuration {
Configuration {
depth: 5,
Expand All @@ -441,7 +443,7 @@ impl Default for Configuration {
disabled_commands: HashSet::default(),
allow_dm: true,
ignore_webhooks: true,
delimeters: vec![" ".to_string()],
delimiters: vec![" ".to_string()],
}
}
}
42 changes: 22 additions & 20 deletions src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ use model::{ChannelId, GuildId, Message, UserId};
use model::permissions::Permissions;
use utils;
use tokio_core::reactor::Handle;
use itertools::Itertools;
use regex::Regex;
use regex::escape;

#[cfg(feature = "cache")]
use client::CACHE;
Expand Down Expand Up @@ -518,9 +521,9 @@ impl BuiltinFramework {
let rate_limit = bucket.take(message.author.id.0);
match bucket.check {
Some(ref check) => {
let apply = feature_cache! {{
let apply = feature_cache! {{
let guild_id = message.guild_id();
(check)(context, guild_id, message.channel_id, message.author.id)
(check)(context, guild_id, message.channel_id, message.author.id)
} else {
(check)(context, message.channel_id, message.author.id)
}};
Expand Down Expand Up @@ -567,7 +570,7 @@ impl BuiltinFramework {
}

if (!self.configuration.allow_dm && message.is_private()) ||
(command.guild_only && message.is_private()) {
(command.guild_only && message.is_private()) {
return Some(DispatchError::OnlyForGuilds);
}

Expand All @@ -579,13 +582,13 @@ impl BuiltinFramework {
if command.owners_only {
Some(DispatchError::OnlyForOwners)
} else if !command
.checks
.iter()
.all(|check| (check)(&mut context, message, command)) {
.checks
.iter()
.all(|check| (check)(&mut context, message, command)) {
Some(DispatchError::CheckFailed)
} else if self.configuration
.blocked_users
.contains(&message.author.id) {
.blocked_users
.contains(&message.author.id) {
Some(DispatchError::BlockedUser)
} else if self.configuration.disabled_commands.contains(to_check) {
Some(DispatchError::CommandDisabled(to_check.to_owned()))
Expand Down Expand Up @@ -909,7 +912,7 @@ impl ::Framework for BuiltinFramework {
let command_length = built.len();

if let Some(&CommandOrAlias::Alias(ref points_to)) =
group.commands.get(&built) {
group.commands.get(&built) {
built = points_to.to_owned();
}

Expand All @@ -924,7 +927,7 @@ impl ::Framework for BuiltinFramework {
};

if let Some(&CommandOrAlias::Command(ref command)) =
group.commands.get(&to_check) {
group.commands.get(&to_check) {
let before = self.before.clone();
let command = command.clone();
let after = self.after.clone();
Expand All @@ -937,16 +940,15 @@ impl ::Framework for BuiltinFramework {
if command.use_quotes {
utils::parse_quotes(&content[command_length..])
} else {
let delimeter = self.configuration.delimeters
.iter()
.find(|d| content.contains(d.as_str()))
.map(|s| s.as_str())
.unwrap_or(" ");

content
.split(delimeter)
.map(|arg| arg.to_owned())
.collect::<Vec<String>>()
let delimiters = &self.configuration.delimiters;
let regular_expression = delimiters.iter()
.map(|delimiter| escape(delimiter)).join("|");

let regex = Regex::new(&regular_expression).unwrap();

regex.split(content)
.filter_map(|p| if !p.is_empty() { Some(p.to_string()) } else { None })
.collect::<Vec<_>>()
}
};

Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ extern crate tokio_core;
extern crate typemap;
#[cfg(feature = "gateway")]
extern crate websocket;
#[cfg(feature = "framework")]
extern crate regex;
#[cfg(feature = "framework")]
extern crate itertools;

#[macro_use]
mod internal;
Expand Down

0 comments on commit 069df4f

Please sign in to comment.