aboutsummaryrefslogtreecommitdiff
path: root/src/utils/mod.rs
diff options
context:
space:
mode:
authorIllia <[email protected]>2016-12-09 23:30:30 +0200
committerzeyla <[email protected]>2016-12-09 13:30:30 -0800
commit8f24aa391f6b8a9103a9c105138c7610288acb05 (patch)
tree5af9a958502a49d64868c50ef976157c6b71adca /src/utils/mod.rs
parentImplement From<Embed> for CreateEmbed (diff)
downloadserenity-8f24aa391f6b8a9103a9c105138c7610288acb05.tar.xz
serenity-8f24aa391f6b8a9103a9c105138c7610288acb05.zip
Command builder, quoted args, and multi-prefixes
Add a command builder, which can take arguments such as multiple checks, quoted arguments, and multiple prefix support, as well as dynamic prefixes per context.
Diffstat (limited to 'src/utils/mod.rs')
-rw-r--r--src/utils/mod.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index a7a14a3..fbc642f 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -198,3 +198,49 @@ pub fn read_image<P: AsRef<Path>>(path: P) -> Result<String> {
Ok(format!("data:image/{};base64,{}", ext, b64))
}
+
+/// Turns a string into a vector of string arguments, splitting by spaces, but
+/// parsing content within quotes as one individual argument.
+pub fn parse_quotes(s: &str) -> Vec<String> {
+ let mut args = vec![];
+ let mut in_string = false;
+ let mut current_str = String::default();
+
+ for x in s.chars() {
+ if in_string {
+ if x == '"' {
+ if !current_str.is_empty() {
+ args.push(current_str);
+ }
+
+ current_str = String::default();
+ in_string = false;
+ } 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());
+ }
+
+ in_string = true;
+ current_str = String::default();
+ } else {
+ current_str.push(x);
+ }
+ }
+ }
+
+ if !current_str.is_empty() {
+ args.push(current_str);
+ }
+
+ args
+}