diff options
| author | Pg Biel <9021226+PgBiel@users.noreply.github.com> | 2023-05-03 09:20:53 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-03 14:20:53 +0200 |
| commit | f88ef45ee6e285df59c7aa5cec935de331b4b6e0 (patch) | |
| tree | eab5481d4b50d1d57adb4d122d7fa023dee2dcec /library | |
| parent | db6a710638cf26ddcd09b8fba74b9d1caf6cb4b8 (diff) | |
Function scopes (#1032)
Diffstat (limited to 'library')
| -rw-r--r-- | library/src/compute/foundations.rs | 92 | ||||
| -rw-r--r-- | library/src/layout/enum.rs | 15 |
2 files changed, 107 insertions, 0 deletions
diff --git a/library/src/compute/foundations.rs b/library/src/compute/foundations.rs index bad9f8ab..5127fca3 100644 --- a/library/src/compute/foundations.rs +++ b/library/src/compute/foundations.rs @@ -88,6 +88,9 @@ pub fn panic( /// Fails with an error if the condition is not fulfilled. Does not /// produce any output in the document. /// +/// If you wish to test equality between two values, see +/// [`assert.eq`]($func/assert.eq) and [`assert.ne`]($func/assert.ne). +/// /// ## Example /// ```typ /// #assert(1 < 2, message: "math broke") @@ -97,6 +100,11 @@ pub fn panic( /// Category: foundations /// Returns: #[func] +#[scope( + scope.define("eq", assert_eq); + scope.define("ne", assert_ne); + scope +)] pub fn assert( /// The condition that must be true for the assertion to pass. condition: bool, @@ -115,6 +123,90 @@ pub fn assert( Value::None } +/// Ensure that two values are equal. +/// +/// Fails with an error if the first value is not equal to the second. Does not +/// produce any output in the document. +/// +/// ## Example +/// ```example +/// #assert.eq(10, 10) +/// ``` +/// +/// Display: Assert Equals +/// Category: foundations +/// Returns: +#[func] +pub fn assert_eq( + /// The first value to compare. + left: Value, + + /// The second value to compare. + right: Value, + + /// An optional message to display on error instead of the representations + /// of the compared values. + #[named] + #[default] + message: Option<EcoString>, +) -> Value { + if left != right { + if let Some(message) = message { + bail!(args.span, "equality assertion failed: {}", message); + } else { + bail!( + args.span, + "equality assertion failed: value {:?} was not equal to {:?}", + left, + right + ); + } + } + Value::None +} + +/// Ensure that two values are not equal. +/// +/// Fails with an error if the first value is equal to the second. Does not +/// produce any output in the document. +/// +/// ## Example +/// ```example +/// #assert.ne(3, 4) +/// ``` +/// +/// Display: Assert Not Equals +/// Category: foundations +/// Returns: +#[func] +pub fn assert_ne( + /// The first value to compare. + left: Value, + + /// The second value to compare. + right: Value, + + /// An optional message to display on error instead of the representations + /// of the compared values. + #[named] + #[default] + message: Option<EcoString>, +) -> Value { + if left == right { + if let Some(message) = message { + bail!(args.span, "inequality assertion failed: {}", message); + } else { + bail!( + args.span, + "inequality assertion failed: value {:?} was equal to {:?}", + left, + right + ); + } + } + Value::None +} + /// Evaluate a string as Typst code. /// /// This function should only be used as a last resort. diff --git a/library/src/layout/enum.rs b/library/src/layout/enum.rs index 8814aba3..a0b23945 100644 --- a/library/src/layout/enum.rs +++ b/library/src/layout/enum.rs @@ -36,6 +36,17 @@ use super::GridLayouter; /// + Don't forget step two /// ``` /// +/// You can also use [`enum.item`]($func/enum.item) to programmatically +/// customize the number of each item in the enumeration: +/// +/// ```example +/// #enum( +/// enum.item(1)[First step], +/// enum.item(5)[Fifth step], +/// enum.item(10)[Tenth step] +/// ) +/// ``` +/// /// ## Syntax /// This functions also has dedicated syntax: /// @@ -51,6 +62,10 @@ use super::GridLayouter; /// Display: Numbered List /// Category: layout #[element(Layout)] +#[scope( + scope.define("item", EnumItem::func()); + scope +)] pub struct EnumElem { /// If this is `{false}`, the items are spaced apart with /// [enum spacing]($func/enum.spacing). If it is `{true}`, they use normal |
