diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-02-19 21:08:01 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-02-19 21:08:01 +0100 |
| commit | 6e65ebf23641a755b0088569751c0b02e898f1e9 (patch) | |
| tree | 52c19bff69db599bd86ee92250bee40d57af5622 | |
| parent | 1e681b35c70cbb9c117d9f9b94bb2980753f685d (diff) | |
Panic function
| -rw-r--r-- | library/src/compute/foundations.rs | 35 | ||||
| -rw-r--r-- | library/src/lib.rs | 1 | ||||
| -rw-r--r-- | tests/typ/compute/foundations.typ | 22 |
3 files changed, 52 insertions, 6 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"); diff --git a/tests/typ/compute/foundations.typ b/tests/typ/compute/foundations.typ index 83cda65f..eb3e7e35 100644 --- a/tests/typ/compute/foundations.typ +++ b/tests/typ/compute/foundations.typ @@ -11,12 +11,32 @@ #test(repr((1, 2, false, )), "(1, 2, false)") --- +// Test panic. +// Error: 7-9 panicked +#panic() + +--- +// Test panic. +// Error: 7-12 panicked with: 123 +#panic(123) + +--- +// Test panic. +// Error: 7-24 panicked with: "this is wrong" +#panic("this is wrong") + +--- // Test failing assertions. -// Error: 9-15 assertion failed +// Error: 8-16 assertion failed #assert(1 == 2) --- // Test failing assertions. +// Error: 8-51 assertion failed: two is smaller than one +#assert(2 < 1, message: "two is smaller than one") + +--- +// Test failing assertions. // Error: 9-15 expected boolean, found string #assert("true") |
