mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Always use first positional argument in expect()
This changes `#h(100)` from "missing argument: spacing" to "expected linear, found integer".
This commit is contained in:
parent
0a23bfbc23
commit
ae05dc0876
@ -93,18 +93,23 @@ impl Args {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find and consume the first castable positional argument, returning a
|
/// Try to cast the first positional argument ir returning a `missing
|
||||||
/// `missing argument: {what}` error if no match was found.
|
/// argument: {what}` error if no positional argument is left.
|
||||||
pub fn expect<T>(&mut self, what: &str) -> TypResult<T>
|
pub fn expect<T>(&mut self, what: &str) -> TypResult<T>
|
||||||
where
|
where
|
||||||
T: Cast<Spanned<Value>>,
|
T: Cast<Spanned<Value>>,
|
||||||
{
|
{
|
||||||
match self.eat() {
|
for (i, slot) in self.items.iter().enumerate() {
|
||||||
Some(found) => Ok(found),
|
if slot.name.is_none() {
|
||||||
None => bail!(self.span, "missing argument: {}", what),
|
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.
|
/// Find and consume all castable positional arguments.
|
||||||
pub fn all<T>(&mut self) -> impl Iterator<Item = T> + '_
|
pub fn all<T>(&mut self) -> impl Iterator<Item = T> + '_
|
||||||
where
|
where
|
||||||
|
@ -6,11 +6,9 @@ use crate::color::{Color, RgbaColor};
|
|||||||
|
|
||||||
/// `assert`: Ensure that a condition is fulfilled.
|
/// `assert`: Ensure that a condition is fulfilled.
|
||||||
pub fn assert(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
|
pub fn assert(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
|
||||||
let Spanned { v, span } = args.expect("condition")?;
|
let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?;
|
||||||
match v {
|
if !v {
|
||||||
Value::Bool(true) => {}
|
bail!(span, "assertion failed");
|
||||||
Value::Bool(false) => bail!(span, "assertion failed"),
|
|
||||||
v => bail!(span, "expected boolean, found {}", v.type_name()),
|
|
||||||
}
|
}
|
||||||
Ok(Value::None)
|
Ok(Value::None)
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
#len()
|
#len()
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 6-10 expected string, array or dictionary
|
// Error: 6-10 expected string, array or dictionary, found length
|
||||||
#len(12pt)
|
#len(12pt)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
#abs(10pt + 50%)
|
#abs(10pt + 50%)
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 6-17 expected numeric value
|
// Error: 6-17 expected numeric value, found string
|
||||||
#abs("no number")
|
#abs("no number")
|
||||||
|
|
||||||
---
|
---
|
||||||
|
Loading…
x
Reference in New Issue
Block a user