summaryrefslogtreecommitdiff
path: root/library/src/compute
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-12-14 10:09:44 +0100
committerLaurenz <laurmaedje@gmail.com>2022-12-14 10:09:44 +0100
commit9ba4d2c134479aad876a0e2ac4cd1622a353109e (patch)
treea94e0e6ae53a1ba440e869fca26cc2ea0b179057 /library/src/compute
parent4c73456fc1f5df8ebb3a89d9db657c3c54624d66 (diff)
New macro setup
Diffstat (limited to 'library/src/compute')
-rw-r--r--library/src/compute/calc.rs18
-rw-r--r--library/src/compute/create.rs27
-rw-r--r--library/src/compute/data.rs3
-rw-r--r--library/src/compute/foundations.rs10
-rw-r--r--library/src/compute/utility.rs6
5 files changed, 44 insertions, 20 deletions
diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs
index 3541e08c..71e43e21 100644
--- a/library/src/compute/calc.rs
+++ b/library/src/compute/calc.rs
@@ -3,7 +3,8 @@ use std::cmp::Ordering;
use crate::prelude::*;
/// The absolute value of a numeric value.
-pub fn abs(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn abs(args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("numeric value")?;
Ok(match v {
Value::Int(v) => Value::Int(v.abs()),
@@ -19,12 +20,14 @@ pub fn abs(_: &Vm, args: &mut Args) -> SourceResult<Value> {
}
/// The minimum of a sequence of values.
-pub fn min(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn min(args: &mut Args) -> SourceResult<Value> {
minmax(args, Ordering::Less)
}
/// The maximum of a sequence of values.
-pub fn max(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn max(args: &mut Args) -> SourceResult<Value> {
minmax(args, Ordering::Greater)
}
@@ -50,17 +53,20 @@ fn minmax(args: &mut Args, goal: Ordering) -> SourceResult<Value> {
}
/// Whether an integer is even.
-pub fn even(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn even(args: &mut Args) -> SourceResult<Value> {
Ok(Value::Bool(args.expect::<i64>("integer")? % 2 == 0))
}
/// Whether an integer is odd.
-pub fn odd(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn odd(args: &mut Args) -> SourceResult<Value> {
Ok(Value::Bool(args.expect::<i64>("integer")? % 2 != 0))
}
/// The modulo of two numbers.
-pub fn mod_(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn mod_(args: &mut Args) -> SourceResult<Value> {
let Spanned { v: v1, span: span1 } = args.expect("integer or float")?;
let Spanned { v: v2, span: span2 } = args.expect("integer or float")?;
diff --git a/library/src/compute/create.rs b/library/src/compute/create.rs
index 4fd27499..acd2e31f 100644
--- a/library/src/compute/create.rs
+++ b/library/src/compute/create.rs
@@ -5,7 +5,8 @@ use typst::model::Regex;
use crate::prelude::*;
/// Convert a value to an integer.
-pub fn int(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn int(args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("value")?;
Ok(Value::Int(match v {
Value::Bool(v) => v as i64,
@@ -20,7 +21,8 @@ pub fn int(_: &Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Convert a value to a float.
-pub fn float(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn float(args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("value")?;
Ok(Value::Float(match v {
Value::Int(v) => v as f64,
@@ -34,13 +36,15 @@ pub fn float(_: &Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Create a grayscale color.
-pub fn luma(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn luma(args: &mut Args) -> SourceResult<Value> {
let Component(luma) = args.expect("gray component")?;
Ok(Value::Color(LumaColor::new(luma).into()))
}
/// Create an RGB(A) color.
-pub fn rgb(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn rgb(args: &mut Args) -> SourceResult<Value> {
Ok(Value::Color(if let Some(string) = args.find::<Spanned<EcoString>>()? {
match RgbaColor::from_str(&string.v) {
Ok(color) => color.into(),
@@ -56,7 +60,8 @@ pub fn rgb(_: &Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Create a CMYK color.
-pub fn cmyk(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn cmyk(args: &mut Args) -> SourceResult<Value> {
let RatioComponent(c) = args.expect("cyan component")?;
let RatioComponent(m) = args.expect("magenta component")?;
let RatioComponent(y) = args.expect("yellow component")?;
@@ -95,7 +100,8 @@ castable! {
}
/// Convert a value to a string.
-pub fn str(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn str(args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect("value")?;
Ok(Value::Str(match v {
Value::Int(v) => format_str!("{}", v),
@@ -107,18 +113,21 @@ pub fn str(_: &Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Create a label from a string.
-pub fn label(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn label(args: &mut Args) -> SourceResult<Value> {
Ok(Value::Label(Label(args.expect("string")?)))
}
/// Create a regular expression from a string.
-pub fn regex(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn regex(args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect::<Spanned<EcoString>>("regular expression")?;
Ok(Regex::new(&v).at(span)?.into())
}
/// Create an array consisting of a sequence of numbers.
-pub fn range(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn range(args: &mut Args) -> SourceResult<Value> {
let first = args.expect::<i64>("end")?;
let (start, end) = match args.eat::<i64>()? {
Some(second) => (first, second),
diff --git a/library/src/compute/data.rs b/library/src/compute/data.rs
index 4f6e3b67..57dce5c1 100644
--- a/library/src/compute/data.rs
+++ b/library/src/compute/data.rs
@@ -5,6 +5,7 @@ use typst::diag::{format_xml_like_error, FileError};
use crate::prelude::*;
/// Read structured data from a CSV file.
+#[func]
pub fn csv(vm: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: path, span } =
args.expect::<Spanned<EcoString>>("path to csv file")?;
@@ -45,6 +46,7 @@ fn format_csv_error(error: csv::Error) -> String {
}
/// Read structured data from a JSON file.
+#[func]
pub fn json(vm: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: path, span } =
args.expect::<Spanned<EcoString>>("path to json file")?;
@@ -85,6 +87,7 @@ fn format_json_error(error: serde_json::Error) -> String {
}
/// Read structured data from an XML file.
+#[func]
pub fn xml(vm: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: path, span } =
args.expect::<Spanned<EcoString>>("path to xml file")?;
diff --git a/library/src/compute/foundations.rs b/library/src/compute/foundations.rs
index 5134d4ac..abe797dc 100644
--- a/library/src/compute/foundations.rs
+++ b/library/src/compute/foundations.rs
@@ -5,17 +5,20 @@ use typst::model;
use typst::syntax::Source;
/// The name of a value's type.
-pub fn type_(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn type_(args: &mut Args) -> SourceResult<Value> {
Ok(args.expect::<Value>("value")?.type_name().into())
}
/// The string representation of a value.
-pub fn repr(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn repr(args: &mut Args) -> SourceResult<Value> {
Ok(args.expect::<Value>("value")?.repr().into())
}
/// Ensure that a condition is fulfilled.
-pub fn assert(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn assert(args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?;
if !v {
bail!(span, "assertion failed");
@@ -24,6 +27,7 @@ pub fn assert(_: &Vm, args: &mut Args) -> SourceResult<Value> {
}
/// Evaluate a string as Typst markup.
+#[func]
pub fn eval(vm: &Vm, args: &mut Args) -> SourceResult<Value> {
let Spanned { v: text, span } = args.expect::<Spanned<String>>("source")?;
let source = Source::synthesized(text, span);
diff --git a/library/src/compute/utility.rs b/library/src/compute/utility.rs
index 196f8368..d48f794e 100644
--- a/library/src/compute/utility.rs
+++ b/library/src/compute/utility.rs
@@ -4,13 +4,15 @@ use crate::prelude::*;
use crate::text::Case;
/// Create a blind text string.
-pub fn lorem(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn lorem(args: &mut Args) -> SourceResult<Value> {
let words: usize = args.expect("number of words")?;
Ok(Value::Str(lipsum::lipsum(words).into()))
}
/// Apply a numbering pattern to a number.
-pub fn numbering(_: &Vm, args: &mut Args) -> SourceResult<Value> {
+#[func]
+pub fn numbering(args: &mut Args) -> SourceResult<Value> {
let pattern = args.expect::<NumberingPattern>("pattern")?;
let numbers = args.all::<NonZeroUsize>()?;
Ok(Value::Str(pattern.apply(&numbers).into()))