From 1e681b35c70cbb9c117d9f9b94bb2980753f685d Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 18 Feb 2023 20:18:15 +0100 Subject: Fix typos --- library/src/layout/page.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'library/src') diff --git a/library/src/layout/page.rs b/library/src/layout/page.rs index d6862276..022619d7 100644 --- a/library/src/layout/page.rs +++ b/library/src/layout/page.rs @@ -149,7 +149,7 @@ impl PageNode { /// The page's header. /// - /// The header is placed at in the top margin of each page. + /// The header is placed in the top margin of each page. /// /// - Content: The content will be placed in the header. /// - A function: The function will be called with the page number (starting @@ -171,7 +171,7 @@ impl PageNode { /// The page's footer. /// - /// The footer is placed at in the bottom margin of each page. + /// The footer is placed in the bottom margin of each page. /// /// - Content: The content will be placed in the footer. /// - A function: The function will be called with the page number (starting -- cgit v1.2.3 From 6e65ebf23641a755b0088569751c0b02e898f1e9 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 19 Feb 2023 21:08:01 +0100 Subject: Panic function --- library/src/compute/foundations.rs | 35 ++++++++++++++++++++++++++++++----- library/src/lib.rs | 1 + 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'library/src') 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 { Ok(args.expect::("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 { + match args.eat::()? { + 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 { /// /// ## 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 { - let Spanned { v, span } = args.expect::>("condition")?; + let check = args.expect::("condition")?; let message = args.named::("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) diff --git a/library/src/lib.rs b/library/src/lib.rs index 8a231531..31da5b71 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -96,6 +96,7 @@ fn global(math: Module, calc: Module) -> Module { // Compute. global.def_func::("type"); global.def_func::("repr"); + global.def_func::("panic"); global.def_func::("assert"); global.def_func::("eval"); global.def_func::("int"); -- cgit v1.2.3 From a1d47695a2af5afa466c21ad812a1a8212780293 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 23 Feb 2023 12:15:38 +0100 Subject: Switch to ecow --- library/src/compute/construct.rs | 6 +++--- library/src/compute/data.rs | 8 ++++---- library/src/prelude.rs | 4 ++-- library/src/text/mod.rs | 1 - library/src/text/shift.rs | 1 - 5 files changed, 9 insertions(+), 11 deletions(-) (limited to 'library/src') diff --git a/library/src/compute/construct.rs b/library/src/compute/construct.rs index f540d828..a44d50c2 100644 --- a/library/src/compute/construct.rs +++ b/library/src/compute/construct.rs @@ -458,12 +458,12 @@ pub fn range(args: &mut Args) -> SourceResult { }; 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 { } 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/prelude.rs b/library/src/prelude.rs index 5bb1d08a..c8f6fe97 100644 --- a/library/src/prelude.rs +++ b/library/src/prelude.rs @@ -8,6 +8,8 @@ pub use std::num::NonZeroUsize; #[doc(no_inline)] pub use comemo::{Track, Tracked, TrackedMut}; #[doc(no_inline)] +pub use ecow::{format_eco, EcoString}; +#[doc(no_inline)] pub use typst::diag::{bail, error, At, SourceResult, StrResult}; #[doc(no_inline)] pub use typst::doc::*; @@ -23,8 +25,6 @@ pub use typst::model::{ #[doc(no_inline)] pub use typst::syntax::{Span, Spanned}; #[doc(no_inline)] -pub use typst::util::{format_eco, EcoString}; -#[doc(no_inline)] pub use typst::World; #[doc(no_inline)] diff --git a/library/src/text/mod.rs b/library/src/text/mod.rs index a87fba87..1ef32fa4 100644 --- a/library/src/text/mod.rs +++ b/library/src/text/mod.rs @@ -18,7 +18,6 @@ use std::borrow::Cow; use rustybuzz::Tag; use typst::font::{FontMetrics, FontStretch, FontStyle, FontWeight, VerticalFontMetric}; -use typst::util::EcoString; use crate::layout::ParNode; use crate::prelude::*; diff --git a/library/src/text/shift.rs b/library/src/text/shift.rs index a7967650..d6809591 100644 --- a/library/src/text/shift.rs +++ b/library/src/text/shift.rs @@ -1,5 +1,4 @@ use typst::model::SequenceNode; -use typst::util::EcoString; use super::{variant, SpaceNode, TextNode, TextSize}; use crate::prelude::*; -- cgit v1.2.3 From 457ce954366f3a81989fee788c85a5b20a96ce96 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 23 Feb 2023 14:36:40 +0100 Subject: More EcoVec usage Frame unfortunately can't use it because splice is missing. --- library/src/compute/construct.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'library/src') diff --git a/library/src/compute/construct.rs b/library/src/compute/construct.rs index a44d50c2..13ecf08d 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 { - let mut list: Vec<(EcoString, char)> = vec![]; + let mut list = EcoVec::new(); for Spanned { v, span } in args.all::>()? { if list.iter().any(|(prev, _)| &v.0 == prev) { bail!(span, "duplicate variant"); -- cgit v1.2.3