summaryrefslogtreecommitdiff
path: root/library/src/compute
diff options
context:
space:
mode:
authorPg Biel <9021226+PgBiel@users.noreply.github.com>2023-05-03 09:20:53 -0300
committerGitHub <noreply@github.com>2023-05-03 14:20:53 +0200
commitf88ef45ee6e285df59c7aa5cec935de331b4b6e0 (patch)
treeeab5481d4b50d1d57adb4d122d7fa023dee2dcec /library/src/compute
parentdb6a710638cf26ddcd09b8fba74b9d1caf6cb4b8 (diff)
Function scopes (#1032)
Diffstat (limited to 'library/src/compute')
-rw-r--r--library/src/compute/foundations.rs92
1 files changed, 92 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.