From 25e91dabd2380bd8fd98acbb7cb220dd90d238bd Mon Sep 17 00:00:00 2001 From: Maiddog Date: Mon, 28 Aug 2017 06:06:17 -0500 Subject: Add find and find_n (#153) --- src/framework/standard/args.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/framework') 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(&mut self) -> Result + where T::Err: StdError + 'static { + if self.delimiter_split.is_empty() { + return Err(Error::Eos); + } + + match self.delimiter_split.iter().position( + |e| e.parse::().is_ok(), + ) { + Some(index) => { + let value = self.delimiter_split + .get(index) + .ok_or(Error::Eos)? + .parse::() + .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(&self) -> Result + where T::Err: StdError + 'static { + if self.delimiter_split.is_empty() { + return Err(Error::Eos); + } + + self.delimiter_split + .iter() + .find(|e| e.parse::().is_ok()) + .ok_or(Error::Eos)? + .parse::() + .map_err(|e| Error::Parse(Box::new(e))) + } } impl ::std::ops::Deref for Args { -- cgit v1.2.3