From 3968181622694c4a15ae336049439b328649bca0 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 25 Oct 2021 13:34:49 +0200 Subject: Replace `..` syntax with `range` function --- src/eval/function.rs | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'src/eval/function.rs') diff --git a/src/eval/function.rs b/src/eval/function.rs index d9f79adf..cbbc0b36 100644 --- a/src/eval/function.rs +++ b/src/eval/function.rs @@ -77,25 +77,22 @@ pub struct Arg { } impl Args { - /// Find and consume the first castable positional argument. - pub fn eat(&mut self) -> Option + /// Consume and cast the first positional argument. + /// + /// Returns a `missing argument: {what}` error if no positional argument is + /// left. + pub fn expect(&mut self, what: &str) -> TypResult where T: Cast>, { - for (i, slot) in self.items.iter().enumerate() { - if slot.name.is_none() { - if T::is(&slot.value) { - let value = self.items.remove(i).value; - return T::cast(value).ok(); - } - } + match self.eat()? { + Some(v) => Ok(v), + None => bail!(self.span, "missing argument: {}", what), } - None } - /// Try to cast the first positional argument ir returning a `missing - /// argument: {what}` error if no positional argument is left. - pub fn expect(&mut self, what: &str) -> TypResult + /// Consume and cast the first positional argument if there is one. + pub fn eat(&mut self) -> TypResult> where T: Cast>, { @@ -103,11 +100,24 @@ impl Args { if slot.name.is_none() { let value = self.items.remove(i).value; let span = value.span; - return T::cast(value).at(span); + return T::cast(value).at(span).map(Some); } } + Ok(None) + } - bail!(self.span, "missing argument: {}", what); + /// Find and consume the first castable positional argument. + pub fn find(&mut self) -> Option + where + T: Cast>, + { + for (i, slot) in self.items.iter().enumerate() { + if slot.name.is_none() && T::is(&slot.value) { + let value = self.items.remove(i).value; + return T::cast(value).ok(); + } + } + None } /// Find and consume all castable positional arguments. @@ -115,7 +125,7 @@ impl Args { where T: Cast>, { - std::iter::from_fn(move || self.eat()) + std::iter::from_fn(move || self.find()) } /// Cast and remove the value for the given named argument, returning an -- cgit v1.2.3