summaryrefslogtreecommitdiff
path: root/library
diff options
context:
space:
mode:
Diffstat (limited to 'library')
-rw-r--r--library/Cargo.toml1
-rw-r--r--library/src/compute/construct.rs9
-rw-r--r--library/src/compute/data.rs8
-rw-r--r--library/src/compute/foundations.rs35
-rw-r--r--library/src/layout/page.rs4
-rw-r--r--library/src/lib.rs1
-rw-r--r--library/src/prelude.rs4
-rw-r--r--library/src/text/mod.rs1
-rw-r--r--library/src/text/shift.rs1
9 files changed, 45 insertions, 19 deletions
diff --git a/library/Cargo.toml b/library/Cargo.toml
index af08d73b..d725a393 100644
--- a/library/Cargo.toml
+++ b/library/Cargo.toml
@@ -13,6 +13,7 @@ bench = false
typst = { path = ".." }
comemo = { git = "https://github.com/typst/comemo" }
csv = "1"
+ecow = { git = "https://github.com/typst/ecow" }
hypher = "0.1"
kurbo = "0.8"
lipsum = { git = "https://github.com/reknih/lipsum" }
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)
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
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::<compute::TypeFunc>("type");
global.def_func::<compute::ReprFunc>("repr");
+ global.def_func::<compute::PanicFunc>("panic");
global.def_func::<compute::AssertFunc>("assert");
global.def_func::<compute::EvalFunc>("eval");
global.def_func::<compute::IntFunc>("int");
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::*;