aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaiddog <[email protected]>2017-08-28 06:06:17 -0500
committeralex <[email protected]>2017-08-28 13:06:17 +0200
commit25e91dabd2380bd8fd98acbb7cb220dd90d238bd (patch)
tree21e7e65c175986c135e0c72a21730258e438ead9 /src
parentRemove mut when not needed (#152) (diff)
downloadserenity-25e91dabd2380bd8fd98acbb7cb220dd90d238bd.tar.xz
serenity-25e91dabd2380bd8fd98acbb7cb220dd90d238bd.zip
Add find and find_n (#153)
Diffstat (limited to 'src')
-rw-r--r--src/framework/standard/args.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs
index f8882af..b88f345 100644
--- a/src/framework/standard/args.rs
+++ b/src/framework/standard/args.rs
@@ -136,6 +136,46 @@ impl Args {
/// This method is just `internal_vector.join(delimiter)`
pub fn full(&self) -> String { self.delimiter_split.join(&self.delimiter) }
+
+ /// Returns the first argument that can be converted and removes it from the list.
+ pub fn find<T: FromStr>(&mut self) -> Result<T>
+ where T::Err: StdError + 'static {
+ if self.delimiter_split.is_empty() {
+ return Err(Error::Eos);
+ }
+
+ match self.delimiter_split.iter().position(
+ |e| e.parse::<T>().is_ok(),
+ ) {
+ Some(index) => {
+ let value = self.delimiter_split
+ .get(index)
+ .ok_or(Error::Eos)?
+ .parse::<T>()
+ .map_err(|e| Error::Parse(Box::new(e)));
+
+ self.delimiter_split.remove(index);
+
+ value
+ },
+ _ => Err(Error::Eos),
+ }
+ }
+
+ /// Returns the first argument that can be converted and does not remove it from the list.
+ pub fn find_n<T: FromStr>(&self) -> Result<T>
+ where T::Err: StdError + 'static {
+ if self.delimiter_split.is_empty() {
+ return Err(Error::Eos);
+ }
+
+ self.delimiter_split
+ .iter()
+ .find(|e| e.parse::<T>().is_ok())
+ .ok_or(Error::Eos)?
+ .parse::<T>()
+ .map_err(|e| Error::Parse(Box::new(e)))
+ }
}
impl ::std::ops::Deref for Args {