diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-02-23 14:38:37 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-02-23 14:38:45 +0100 |
| commit | affb3534538385056cfb60328f6dba6bd852229a (patch) | |
| tree | a0a1b4844d459a66c3d32e0ad0dacf3f47c3966b /library/src/compute | |
| parent | 090831c9cb08c9b3c14ee30b423bb61275d70eb4 (diff) | |
| parent | 457ce954366f3a81989fee788c85a5b20a96ce96 (diff) | |
Merge main back
Diffstat (limited to 'library/src/compute')
| -rw-r--r-- | library/src/compute/construct.rs | 9 | ||||
| -rw-r--r-- | library/src/compute/data.rs | 8 | ||||
| -rw-r--r-- | library/src/compute/foundations.rs | 35 |
3 files changed, 39 insertions, 13 deletions
diff --git a/library/src/compute/construct.rs b/library/src/compute/construct.rs index 3b42ef24..b6f2de8f 100644 --- a/library/src/compute/construct.rs +++ b/library/src/compute/construct.rs @@ -1,5 +1,6 @@ use std::str::FromStr; +use ecow::EcoVec; use typst::model::Regex; use crate::prelude::*; @@ -275,7 +276,7 @@ castable! { /// construct #[func] pub fn symbol(args: &mut Args) -> SourceResult<Value> { - let mut list: Vec<(EcoString, char)> = vec![]; + let mut list = EcoVec::new(); for Spanned { v, span } in args.all::<Spanned<Variant>>()? { if list.iter().any(|(prev, _)| &v.0 == prev) { bail!(span, "duplicate variant"); @@ -458,12 +459,12 @@ pub fn range(args: &mut Args) -> SourceResult<Value> { }; let mut x = start; - let mut seq = vec![]; + let mut array = Array::new(); while x.cmp(&end) == 0.cmp(&step) { - seq.push(Value::Int(x)); + array.push(Value::Int(x)); x += step; } - Ok(Value::Array(Array::from_vec(seq))) + Ok(Value::Array(array)) } diff --git a/library/src/compute/data.rs b/library/src/compute/data.rs index 5c0d4e64..c604be11 100644 --- a/library/src/compute/data.rs +++ b/library/src/compute/data.rs @@ -86,15 +86,15 @@ pub fn csv(vm: &Vm, args: &mut Args) -> SourceResult<Value> { } let mut reader = builder.from_reader(data.as_slice()); - let mut vec = vec![]; + let mut array = Array::new(); for result in reader.records() { let row = result.map_err(format_csv_error).at(span)?; - let array = row.iter().map(|field| Value::Str(field.into())).collect(); - vec.push(Value::Array(array)) + let sub = row.iter().map(|field| Value::Str(field.into())).collect(); + array.push(Value::Array(sub)) } - Ok(Value::Array(Array::from_vec(vec))) + Ok(Value::Array(array)) } /// The delimiter to use when parsing CSV files. diff --git a/library/src/compute/foundations.rs b/library/src/compute/foundations.rs index 82270dd3..f17af219 100644 --- a/library/src/compute/foundations.rs +++ b/library/src/compute/foundations.rs @@ -56,6 +56,29 @@ pub fn repr(args: &mut Args) -> SourceResult<Value> { Ok(args.expect::<Value>("value")?.repr().into()) } +/// # Panic +/// Fail with an error. +/// +/// ## Example +/// The code below produces the error `panicked at: "this is wrong"`. +/// ```typ +/// #panic("this is wrong") +/// ``` +/// +/// ## Parameters +/// - payload: `Value` (positional) +/// The value (or message) to panic with. +/// +/// ## Category +/// foundations +#[func] +pub fn panic(args: &mut Args) -> SourceResult<Value> { + match args.eat::<Value>()? { + Some(v) => bail!(args.span, "panicked with: {}", v.repr()), + None => bail!(args.span, "panicked"), + } +} + /// # Assert /// Ensure that a condition is fulfilled. /// @@ -64,24 +87,26 @@ pub fn repr(args: &mut Args) -> SourceResult<Value> { /// /// ## Example /// ```example -/// #assert(1 < 2) +/// #assert(1 < 2, message: "one is") /// ``` /// /// ## Parameters /// - condition: `bool` (positional, required) /// The condition that must be true for the assertion to pass. +/// - message: `EcoString` (named) +/// The error message when the assertion fails. /// /// ## Category /// foundations #[func] pub fn assert(args: &mut Args) -> SourceResult<Value> { - let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?; + let check = args.expect::<bool>("condition")?; let message = args.named::<EcoString>("message")?; - if !v { + if !check { if let Some(message) = message { - bail!(span, "assertion failed: {}", message); + bail!(args.span, "assertion failed: {}", message); } else { - bail!(span, "assertion failed"); + bail!(args.span, "assertion failed"); } } Ok(Value::None) |
