summaryrefslogtreecommitdiff
path: root/library/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-02-19 21:08:01 +0100
committerLaurenz <laurmaedje@gmail.com>2023-02-19 21:08:01 +0100
commit6e65ebf23641a755b0088569751c0b02e898f1e9 (patch)
tree52c19bff69db599bd86ee92250bee40d57af5622 /library/src
parent1e681b35c70cbb9c117d9f9b94bb2980753f685d (diff)
Panic function
Diffstat (limited to 'library/src')
-rw-r--r--library/src/compute/foundations.rs35
-rw-r--r--library/src/lib.rs1
2 files changed, 31 insertions, 5 deletions
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/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");