aboutsummaryrefslogtreecommitdiff
path: root/src/framework
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2018-06-17 00:09:54 +0200
committeracdenisSK <[email protected]>2018-06-17 00:23:01 +0200
commitf0f06b7d3b890d2ddcb84e00b3f62e195da80090 (patch)
tree39ed37bdcdea6e51b78620d5185e48492329752d /src/framework
parentReorganise the order of the ifs (diff)
downloadserenity-f0f06b7d3b890d2ddcb84e00b3f62e195da80090.tar.xz
serenity-f0f06b7d3b890d2ddcb84e00b3f62e195da80090.zip
Force `find(_n)` to be quote aware
Diffstat (limited to 'src/framework')
-rw-r--r--src/framework/standard/args.rs35
1 files changed, 18 insertions, 17 deletions
diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs
index 83e34fe..5965c51 100644
--- a/src/framework/standard/args.rs
+++ b/src/framework/standard/args.rs
@@ -651,13 +651,10 @@ impl Args {
return Err(Error::Eos);
}
- let cur = &self.args[self.offset];
+ let current = &self.args[self.offset];
- let lit = if cur.kind == TokenKind::QuotedArgument {
- &cur.lit[1..cur.lit.len() - 1]
- } else {
- &cur.lit
- };
+ // Discard quotations if present
+ let lit = quotes_extract(current);
let parsed = T::from_str(&lit)?;
self.offset += 1;
@@ -684,13 +681,9 @@ impl Args {
return Err(Error::Eos);
}
- let cur = &self.args[self.offset];
+ let current = &self.args[self.offset];
- let lit = if cur.kind == TokenKind::QuotedArgument {
- &cur.lit[1..cur.lit.len() - 1]
- } else {
- &cur.lit
- };
+ let lit = quotes_extract(current);
Ok(T::from_str(&lit)?)
}
@@ -774,7 +767,7 @@ impl Args {
}
/// Returns the first argument that can be parsed and removes it from the message. The suitable argument
- /// can be in an arbitrary position in the message.
+ /// can be in an arbitrary position in the message. Likewise, takes quotes into account.
///
/// **Note**:
/// Unlike how other methods on this struct work,
@@ -798,12 +791,12 @@ impl Args {
return Err(Error::Eos);
}
- let pos = match self.args.iter().position(|s| s.lit.parse::<T>().is_ok()) {
+ let pos = match self.args.iter().map(|t| quotes_extract(t)).position(|s| s.parse::<T>().is_ok()) {
Some(p) => p,
None => return Err(Error::Eos),
};
- let parsed = T::from_str(&self.args[pos].lit)?;
+ let parsed = T::from_str(quotes_extract(&self.args[pos]))?;
self.args.remove(pos);
self.rewind();
@@ -834,12 +827,12 @@ impl Args {
return Err(Error::Eos);
}
- let pos = match self.args.iter().position(|s| s.lit.parse::<T>().is_ok()) {
+ let pos = match self.args.iter().map(|t| quotes_extract(t)).position(|s| s.parse::<T>().is_ok()) {
Some(p) => p,
None => return Err(Error::Eos),
};
- Ok(T::from_str(&self.args[pos].lit)?)
+ Ok(T::from_str(quotes_extract(&self.args[pos]))?)
}
}
@@ -920,3 +913,11 @@ impl<'a, T: FromStr> Iterator for IterQuoted<'a, T> where T::Err: StdError {
}
}
}
+
+fn quotes_extract(token: &TokenOwned) -> &str {
+ if token.kind == TokenKind::QuotedArgument {
+ &token.lit[1..token.lit.len() - 1]
+ } else {
+ &token.lit
+ }
+}