diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-12-15 22:51:55 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-12-15 23:11:20 +0100 |
| commit | b6202b646a0d5ecced301d9bac8bfcaf977d7ee4 (patch) | |
| tree | 7d42cb50f9e66153e7e8b2217009684e25f54f42 /library/src/compute | |
| parent | f3980c704544a464f9729cc8bc9f97d3a7454769 (diff) | |
Reflection for castables
Diffstat (limited to 'library/src/compute')
| -rw-r--r-- | library/src/compute/calc.rs | 12 | ||||
| -rw-r--r-- | library/src/compute/create.rs | 26 | ||||
| -rw-r--r-- | library/src/compute/data.rs | 6 | ||||
| -rw-r--r-- | library/src/compute/foundations.rs | 8 | ||||
| -rw-r--r-- | library/src/compute/utility.rs | 7 |
5 files changed, 52 insertions, 7 deletions
diff --git a/library/src/compute/calc.rs b/library/src/compute/calc.rs index 71e43e21..62d0a419 100644 --- a/library/src/compute/calc.rs +++ b/library/src/compute/calc.rs @@ -3,6 +3,8 @@ use std::cmp::Ordering; use crate::prelude::*; /// The absolute value of a numeric value. +/// +/// Tags: calculate. #[func] pub fn abs(args: &mut Args) -> SourceResult<Value> { let Spanned { v, span } = args.expect("numeric value")?; @@ -20,12 +22,16 @@ pub fn abs(args: &mut Args) -> SourceResult<Value> { } /// The minimum of a sequence of values. +/// +/// Tags: calculate. #[func] pub fn min(args: &mut Args) -> SourceResult<Value> { minmax(args, Ordering::Less) } /// The maximum of a sequence of values. +/// +/// Tags: calculate. #[func] pub fn max(args: &mut Args) -> SourceResult<Value> { minmax(args, Ordering::Greater) @@ -53,18 +59,24 @@ fn minmax(args: &mut Args, goal: Ordering) -> SourceResult<Value> { } /// Whether an integer is even. +/// +/// Tags: calculate. #[func] pub fn even(args: &mut Args) -> SourceResult<Value> { Ok(Value::Bool(args.expect::<i64>("integer")? % 2 == 0)) } /// Whether an integer is odd. +/// +/// Tags: calculate. #[func] pub fn odd(args: &mut Args) -> SourceResult<Value> { Ok(Value::Bool(args.expect::<i64>("integer")? % 2 != 0)) } /// The modulo of two numbers. +/// +/// Tags: calculate. #[func] pub fn mod_(args: &mut Args) -> SourceResult<Value> { let Spanned { v: v1, span: span1 } = args.expect("integer or float")?; diff --git a/library/src/compute/create.rs b/library/src/compute/create.rs index acd2e31f..a0eecfb8 100644 --- a/library/src/compute/create.rs +++ b/library/src/compute/create.rs @@ -5,6 +5,8 @@ use typst::model::Regex; use crate::prelude::*; /// Convert a value to an integer. +/// +/// Tags: create. #[func] pub fn int(args: &mut Args) -> SourceResult<Value> { let Spanned { v, span } = args.expect("value")?; @@ -21,6 +23,8 @@ pub fn int(args: &mut Args) -> SourceResult<Value> { } /// Convert a value to a float. +/// +/// Tags: create. #[func] pub fn float(args: &mut Args) -> SourceResult<Value> { let Spanned { v, span } = args.expect("value")?; @@ -36,6 +40,8 @@ pub fn float(args: &mut Args) -> SourceResult<Value> { } /// Create a grayscale color. +/// +/// Tags: create. #[func] pub fn luma(args: &mut Args) -> SourceResult<Value> { let Component(luma) = args.expect("gray component")?; @@ -43,6 +49,8 @@ pub fn luma(args: &mut Args) -> SourceResult<Value> { } /// Create an RGB(A) color. +/// +/// Tags: create. #[func] pub fn rgb(args: &mut Args) -> SourceResult<Value> { Ok(Value::Color(if let Some(string) = args.find::<Spanned<EcoString>>()? { @@ -60,6 +68,8 @@ pub fn rgb(args: &mut Args) -> SourceResult<Value> { } /// Create a CMYK color. +/// +/// Tags: create. #[func] pub fn cmyk(args: &mut Args) -> SourceResult<Value> { let RatioComponent(c) = args.expect("cyan component")?; @@ -74,12 +84,11 @@ struct Component(u8); castable! { Component, - Expected: "integer or ratio", - Value::Int(v) => match v { + v: i64 => match v { 0 ..= 255 => Self(v as u8), _ => Err("must be between 0 and 255")?, }, - Value::Ratio(v) => if (0.0 ..= 1.0).contains(&v.get()) { + v: Ratio => if (0.0 ..= 1.0).contains(&v.get()) { Self((v.get() * 255.0).round() as u8) } else { Err("must be between 0% and 100%")? @@ -91,8 +100,7 @@ struct RatioComponent(u8); castable! { RatioComponent, - Expected: "ratio", - Value::Ratio(v) => if (0.0 ..= 1.0).contains(&v.get()) { + v: Ratio => if (0.0 ..= 1.0).contains(&v.get()) { Self((v.get() * 255.0).round() as u8) } else { Err("must be between 0% and 100%")? @@ -100,6 +108,8 @@ castable! { } /// Convert a value to a string. +/// +/// Tags: create. #[func] pub fn str(args: &mut Args) -> SourceResult<Value> { let Spanned { v, span } = args.expect("value")?; @@ -113,12 +123,16 @@ pub fn str(args: &mut Args) -> SourceResult<Value> { } /// Create a label from a string. +/// +/// Tags: create. #[func] pub fn label(args: &mut Args) -> SourceResult<Value> { Ok(Value::Label(Label(args.expect("string")?))) } /// Create a regular expression from a string. +/// +/// Tags: create. #[func] pub fn regex(args: &mut Args) -> SourceResult<Value> { let Spanned { v, span } = args.expect::<Spanned<EcoString>>("regular expression")?; @@ -126,6 +140,8 @@ pub fn regex(args: &mut Args) -> SourceResult<Value> { } /// Create an array consisting of a sequence of numbers. +/// +/// Tags: create. #[func] pub fn range(args: &mut Args) -> SourceResult<Value> { let first = args.expect::<i64>("end")?; diff --git a/library/src/compute/data.rs b/library/src/compute/data.rs index 57dce5c1..af545304 100644 --- a/library/src/compute/data.rs +++ b/library/src/compute/data.rs @@ -5,6 +5,8 @@ use typst::diag::{format_xml_like_error, FileError}; use crate::prelude::*; /// Read structured data from a CSV file. +/// +/// Tags: data-loading. #[func] pub fn csv(vm: &Vm, args: &mut Args) -> SourceResult<Value> { let Spanned { v: path, span } = @@ -46,6 +48,8 @@ fn format_csv_error(error: csv::Error) -> String { } /// Read structured data from a JSON file. +/// +/// Tags: data-loading. #[func] pub fn json(vm: &Vm, args: &mut Args) -> SourceResult<Value> { let Spanned { v: path, span } = @@ -87,6 +91,8 @@ fn format_json_error(error: serde_json::Error) -> String { } /// Read structured data from an XML file. +/// +/// Tags: data-loading. #[func] pub fn xml(vm: &Vm, args: &mut Args) -> SourceResult<Value> { let Spanned { v: path, span } = diff --git a/library/src/compute/foundations.rs b/library/src/compute/foundations.rs index abe797dc..22d26553 100644 --- a/library/src/compute/foundations.rs +++ b/library/src/compute/foundations.rs @@ -5,18 +5,24 @@ use typst::model; use typst::syntax::Source; /// The name of a value's type. +/// +/// Tags: foundations. #[func] pub fn type_(args: &mut Args) -> SourceResult<Value> { Ok(args.expect::<Value>("value")?.type_name().into()) } /// The string representation of a value. +/// +/// Tags: foundations. #[func] pub fn repr(args: &mut Args) -> SourceResult<Value> { Ok(args.expect::<Value>("value")?.repr().into()) } /// Ensure that a condition is fulfilled. +/// +/// Tags: foundations. #[func] pub fn assert(args: &mut Args) -> SourceResult<Value> { let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?; @@ -27,6 +33,8 @@ pub fn assert(args: &mut Args) -> SourceResult<Value> { } /// Evaluate a string as Typst markup. +/// +/// Tags: foundations. #[func] pub fn eval(vm: &Vm, args: &mut Args) -> SourceResult<Value> { let Spanned { v: text, span } = args.expect::<Spanned<String>>("source")?; diff --git a/library/src/compute/utility.rs b/library/src/compute/utility.rs index d48f794e..5a6534f4 100644 --- a/library/src/compute/utility.rs +++ b/library/src/compute/utility.rs @@ -4,6 +4,8 @@ use crate::prelude::*; use crate::text::Case; /// Create a blind text string. +/// +/// Tags: utility. #[func] pub fn lorem(args: &mut Args) -> SourceResult<Value> { let words: usize = args.expect("number of words")?; @@ -11,6 +13,8 @@ pub fn lorem(args: &mut Args) -> SourceResult<Value> { } /// Apply a numbering pattern to a number. +/// +/// Tags: utility. #[func] pub fn numbering(args: &mut Args) -> SourceResult<Value> { let pattern = args.expect::<NumberingPattern>("pattern")?; @@ -93,8 +97,7 @@ impl FromStr for NumberingPattern { castable! { NumberingPattern, - Expected: "numbering pattern", - Value::Str(s) => s.parse()?, + string: EcoString => string.parse()?, } /// Different kinds of numberings. |
