Skip to main content

tancore/
error.rs

1use tan::{
2    context::Context,
3    error::Error,
4    expr::Expr,
5    util::{
6        args::{unpack_arg, unpack_stringable_arg},
7        module_util::require_module,
8    },
9};
10
11// #todo error wrap.
12// #todo error pretty-print / format-pretty
13// #todo error variant (don't use the word `kind` reserved for type-system)
14
15fn error_new(args: &[Expr]) -> Result<Expr, Error> {
16    let reason = unpack_stringable_arg(args, 0, "reason")?;
17    Ok(Expr::error(reason))
18}
19
20fn is_error(args: &[Expr]) -> Result<Expr, Error> {
21    let arg = unpack_arg(args, 0, "value")?;
22    if let Expr::Error(_) = arg {
23        Ok(Expr::Bool(true))
24    } else {
25        Ok(Expr::Bool(false))
26    }
27}
28
29pub fn setup_lib_error(context: &mut Context) {
30    // #todo put in 'error' / 'err' or path, and import selected functionality to prelude.
31    let module = require_module("prelude", context);
32
33    // #todo Consider `Err`.
34    module.insert_invocable("Error", Expr::foreign_func(&error_new));
35    // #todo Consider just `error?`.
36    module.insert_invocable("is-error?", Expr::foreign_func(&is_error));
37}