Panic function
This commit is contained in:
parent
1e681b35c7
commit
6e65ebf236
@ -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)
|
||||
|
@ -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");
|
||||
|
@ -10,11 +10,31 @@
|
||||
#test(repr(ltr), "ltr")
|
||||
#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
|
||||
|
Loading…
x
Reference in New Issue
Block a user