summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/eval/function.rs15
-rw-r--r--src/library/utility.rs8
-rw-r--r--tests/typ/utility/collection.typ2
-rw-r--r--tests/typ/utility/math.typ2
4 files changed, 15 insertions, 12 deletions
diff --git a/src/eval/function.rs b/src/eval/function.rs
index 205690df..d9f79adf 100644
--- a/src/eval/function.rs
+++ b/src/eval/function.rs
@@ -93,16 +93,21 @@ impl Args {
None
}
- /// Find and consume the first castable positional argument, returning a
- /// `missing argument: {what}` error if no match was found.
+ /// 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>
where
T: Cast<Spanned<Value>>,
{
- match self.eat() {
- Some(found) => Ok(found),
- None => bail!(self.span, "missing argument: {}", what),
+ for (i, slot) in self.items.iter().enumerate() {
+ if slot.name.is_none() {
+ let value = self.items.remove(i).value;
+ let span = value.span;
+ return T::cast(value).at(span);
+ }
}
+
+ bail!(self.span, "missing argument: {}", what);
}
/// Find and consume all castable positional arguments.
diff --git a/src/library/utility.rs b/src/library/utility.rs
index 5de67464..3a2f49b7 100644
--- a/src/library/utility.rs
+++ b/src/library/utility.rs
@@ -6,11 +6,9 @@ use crate::color::{Color, RgbaColor};
/// `assert`: Ensure that a condition is fulfilled.
pub fn assert(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
- let Spanned { v, span } = args.expect("condition")?;
- match v {
- Value::Bool(true) => {}
- Value::Bool(false) => bail!(span, "assertion failed"),
- v => bail!(span, "expected boolean, found {}", v.type_name()),
+ let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?;
+ if !v {
+ bail!(span, "assertion failed");
}
Ok(Value::None)
}
diff --git a/tests/typ/utility/collection.typ b/tests/typ/utility/collection.typ
index a97184d3..92ec2867 100644
--- a/tests/typ/utility/collection.typ
+++ b/tests/typ/utility/collection.typ
@@ -19,7 +19,7 @@
#len()
---
-// Error: 6-10 expected string, array or dictionary
+// Error: 6-10 expected string, array or dictionary, found length
#len(12pt)
---
diff --git a/tests/typ/utility/math.typ b/tests/typ/utility/math.typ
index aeb0d6ad..7217babe 100644
--- a/tests/typ/utility/math.typ
+++ b/tests/typ/utility/math.typ
@@ -16,7 +16,7 @@
#abs(10pt + 50%)
---
-// Error: 6-17 expected numeric value
+// Error: 6-17 expected numeric value, found string
#abs("no number")
---