summaryrefslogtreecommitdiff
path: root/src/eval/function.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-10-25 13:34:49 +0200
committerLaurenz <laurmaedje@gmail.com>2021-10-25 13:38:32 +0200
commit3968181622694c4a15ae336049439b328649bca0 (patch)
tree979093e93b49bfde47e54bc4475cedf24d12e3d8 /src/eval/function.rs
parentadf52a873f0cdff310c236998fc5018a886b339b (diff)
Replace `..` syntax with `range` function
Diffstat (limited to 'src/eval/function.rs')
-rw-r--r--src/eval/function.rs42
1 files changed, 26 insertions, 16 deletions
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<T>(&mut self) -> Option<T>
+ /// Consume and cast the first positional argument.
+ ///
+ /// Returns a `missing argument: {what}` error if no positional argument is
+ /// left.
+ pub fn expect<T>(&mut self, what: &str) -> TypResult<T>
where
T: Cast<Spanned<Value>>,
{
- 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<T>(&mut self, what: &str) -> TypResult<T>
+ /// Consume and cast the first positional argument if there is one.
+ pub fn eat<T>(&mut self) -> TypResult<Option<T>>
where
T: Cast<Spanned<Value>>,
{
@@ -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<T>(&mut self) -> Option<T>
+ where
+ T: Cast<Spanned<Value>>,
+ {
+ 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<Spanned<Value>>,
{
- 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