From fdfb1846083165629feca81b5169ceaf331289c5 Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Sat, 12 Aug 2017 19:30:35 +0200 Subject: [PATCH] Rewamp the custom delimeter functionality to support more --- src/framework/configuration.rs | 32 ++++++++++++++++++++++++++++---- src/framework/mod.rs | 7 ++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/framework/configuration.rs b/src/framework/configuration.rs index 2acf6b3f838..a6d0440598a 100644 --- a/src/framework/configuration.rs +++ b/src/framework/configuration.rs @@ -58,7 +58,7 @@ pub struct Configuration { #[doc(hidden)] pub prefixes: Vec, #[doc(hidden)] - pub delimeter: String, + pub delimeters: Vec, } impl Configuration { @@ -389,7 +389,31 @@ impl Configuration { /// .delimeter(", "))); /// ``` pub fn delimeter(mut self, delimeter: &str) -> Self { - self.delimeter = delimeter.to_string(); + self.delimeters.push(delimeter.to_string()); + + self + } + + /// Sets multiple delimeters to be used when splitting the content after a command. + /// + /// # Examples + /// + /// Have the args be seperated by a comma and a space; and a regular space: + /// + /// ```rust + /// # use serenity::prelude::*; + /// # struct Handler; + /// # + /// # impl EventHandler for Handler {} + /// # let mut client = Client::new("token", Handler); + /// # + /// use serenity::framework::BuiltinFramework; + /// + /// client.with_framework(BuiltinFramework::new().configure(|c| c + /// .delimeters(vec![", ", " "]))); + /// ``` + pub fn delimeters(mut self, delimeters: Vec<&str>) -> Self { + self.delimeters.extend(delimeters.into_iter().map(|s| s.to_string())); self } @@ -402,7 +426,7 @@ impl Default for Configuration { /// - **depth** to `5` /// - **on_mention** to `false` (basically) /// - **prefix** to `None` - /// - **delimeter** to " " + /// - **delimeters** to vec![" "] fn default() -> Configuration { Configuration { depth: 5, @@ -417,7 +441,7 @@ impl Default for Configuration { disabled_commands: HashSet::default(), allow_dm: true, ignore_webhooks: true, - delimeter: " ".to_string(), + delimeters: vec![" ".to_string()], } } } diff --git a/src/framework/mod.rs b/src/framework/mod.rs index ce484e4ba75..d55c26d7db2 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -936,8 +936,13 @@ impl ::Framework for BuiltinFramework { if command.use_quotes { utils::parse_quotes(&content[command_length..]) } else { + let delimeter = { + let delimeter = self.configuration.delimeters.iter().find(|d| content[command_length..].contains(d.as_str())); + delimeter.map(|s| s.as_str()).unwrap_or(" ") + }; + content[command_length..] - .split(&self.configuration.delimeter) + .split(delimeter) .map(|arg| arg.to_owned()) .collect::>() }