diff options
| author | Maiddog <[email protected]> | 2017-08-22 06:27:10 -0500 |
|---|---|---|
| committer | alex <[email protected]> | 2017-08-22 13:27:10 +0200 |
| commit | cb072aab60ac67bc4d52aed3554300c1c5e83081 (patch) | |
| tree | fc00dd5d91b989582e10a93afb4713f1fbd6c88f /src/framework/standard/args.rs | |
| parent | Fix presence updates (diff) | |
| download | serenity-cb072aab60ac67bc4d52aed3554300c1c5e83081.tar.xz serenity-cb072aab60ac67bc4d52aed3554300c1c5e83081.zip | |
Fix tests (#145)
Diffstat (limited to 'src/framework/standard/args.rs')
| -rw-r--r-- | src/framework/standard/args.rs | 97 |
1 files changed, 56 insertions, 41 deletions
diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs index 020ce6d..f08dd48 100644 --- a/src/framework/standard/args.rs +++ b/src/framework/standard/args.rs @@ -8,7 +8,8 @@ use utils::parse_quotes; pub enum Error { /// "END-OF-STRING", more precisely, there isn't anything to parse anymore. Eos, - /// A parsing operation failed; the error in it can be of any returned from the `FromStr` trait. + /// A parsing operation failed; the error in it can be of any returned from the `FromStr` + /// trait. Parse(Box<StdError>), } @@ -27,98 +28,112 @@ impl Args { delimiter_split: message.split(delimiter).map(|s| s.to_string()).collect(), } } - - /// Removes the first element, parses it to a specific type if necessary, returns. - pub fn single<T: FromStr>(&mut self) -> Result<T> where T::Err: StdError + 'static { + + /// Removes the first element, parses it to a specific type if necessary, returns. + pub fn single<T: FromStr>(&mut self) -> Result<T> + where T::Err: StdError + 'static { if self.delimiter_split.is_empty() { - return Err(Error::Eos); + return Err(Error::Eos); } - - self.delimiter_split.shift() + + self.delimiter_split + .shift() .ok_or(Error::Eos)? .parse::<T>() .map_err(|e| Error::Parse(Box::new(e))) } - + /// Like [`single`], but doesn't remove the element. /// /// [`single`]: #method.single - pub fn single_n<T: FromStr>(&mut self) -> Result<T> where T::Err: StdError + 'static { + pub fn single_n<T: FromStr>(&mut self) -> Result<T> + where T::Err: StdError + 'static { if self.delimiter_split.is_empty() { - return Err(Error::Eos); + return Err(Error::Eos); } - - self.delimiter_split.get(0) + + self.delimiter_split + .get(0) .ok_or(Error::Eos)? .parse::<T>() .map_err(|e| Error::Parse(Box::new(e))) } - + /// Skips if there's a first element, but also returns it. - pub fn skip(&mut self) -> Option<String> { - self.delimiter_split.shift() - } - + pub fn skip(&mut self) -> Option<String> { self.delimiter_split.shift() } + /// Like [`skip`], but allows for multiple at once. /// /// [`skip`]: #method.skip pub fn skip_for(&mut self, i: u32) -> Option<Vec<String>> { let mut vec = Vec::with_capacity(i as usize); - + for _ in 0..i { vec.push(match self.delimiter_split.shift() { Some(x) => x, None => return None, }); } - + Some(vec) } - + /// Like [`single`], but takes quotes into account. /// /// [`single`]: #method.single - pub fn single_quoted<T: FromStr>(&mut self) -> Result<T> where T::Err: StdError + 'static { - parse_quotes(&self.delimiter_split.shift().ok_or(Error::Eos)?).remove(0).parse::<T>().map_err(|e| Error::Parse(Box::new(e))) + pub fn single_quoted<T: FromStr>(&mut self) -> Result<T> + where T::Err: StdError + 'static { + parse_quotes(&self.delimiter_split.shift().ok_or(Error::Eos)?) + .remove(0) + .parse::<T>() + .map_err(|e| Error::Parse(Box::new(e))) } - + /// Like [`single_quoted`], but doesn't remove the element. /// /// [`single_quoted`]: #method.single_quoted - pub fn single_quoted_n<T: FromStr>(&mut self) -> Result<T> where T::Err: StdError + 'static { - parse_quotes(&self.delimiter_split.get(0).ok_or(Error::Eos)?).remove(0).parse::<T>().map_err(|e| Error::Parse(Box::new(e))) + pub fn single_quoted_n<T: FromStr>(&mut self) -> Result<T> + where T::Err: StdError + 'static { + parse_quotes(&self.delimiter_split.get(0).ok_or(Error::Eos)?) + .remove(0) + .parse::<T>() + .map_err(|e| Error::Parse(Box::new(e))) } - + /// Like [`list`], but takes quotes into account. /// /// [`list`]: #method.list - pub fn multiple_quoted<T: FromStr>(self) -> Result<Vec<T>> where T::Err: StdError + 'static { + pub fn multiple_quoted<T: FromStr>(self) -> Result<Vec<T>> + where T::Err: StdError + 'static { if self.delimiter_split.is_empty() { return Err(Error::Eos); } - - parse_quotes(&self.delimiter_split.join(&self.delimiter)).into_iter().map(|s| s.parse::<T>().map_err(|e| Error::Parse(Box::new(e)))).collect() + + parse_quotes(&self.delimiter_split.join(&self.delimiter)) + .into_iter() + .map(|s| s.parse::<T>().map_err(|e| Error::Parse(Box::new(e)))) + .collect() } - + /// Empty outs the internal vector while parsing (if necessary) and returning them - pub fn list<T: FromStr>(self) -> Result<Vec<T>> where T::Err: StdError + 'static { + pub fn list<T: FromStr>(self) -> Result<Vec<T>> + where T::Err: StdError + 'static { if self.delimiter_split.is_empty() { return Err(Error::Eos); } - - self.delimiter_split.into_iter().map(|s| s.parse::<T>().map_err(|e| Error::Parse(Box::new(e)))).collect() + + self.delimiter_split + .into_iter() + .map(|s| s.parse::<T>().map_err(|e| Error::Parse(Box::new(e)))) + .collect() } - + /// This method is just `internal_vector.join(delimiter)` - pub fn full(&self) -> String { - self.delimiter_split.join(&self.delimiter) - } + pub fn full(&self) -> String { self.delimiter_split.join(&self.delimiter) } } impl ::std::ops::Deref for Args { type Target = [String]; - fn deref(&self) -> &Self::Target { - &self.delimiter_split - } -}
\ No newline at end of file + fn deref(&self) -> &Self::Target { &self.delimiter_split } +} |