orchidlang/libs/std/
panic.rs

1use std::fmt;
2use std::sync::Arc;
3
4use never::Never;
5
6use super::string::OrcString;
7use crate::foreign::error::{RTError, RTResult};
8use crate::foreign::inert::Inert;
9use crate::gen::tree::{xfn_leaf, ConstTree};
10
11/// An unrecoverable error in Orchid land. Because Orchid is lazy, this only
12/// invalidates expressions that reference the one that generated it.
13#[derive(Clone)]
14pub struct OrchidPanic(Arc<String>);
15
16impl fmt::Display for OrchidPanic {
17  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18    write!(f, "Orchid code panicked: {}", self.0)
19  }
20}
21
22impl RTError for OrchidPanic {}
23
24/// Takes a message, returns an [ExternError] unconditionally.
25pub fn orc_panic(msg: Inert<OrcString>) -> RTResult<Never> {
26  // any return value would work, but Clause is the simplest
27  Err(OrchidPanic(Arc::new(msg.0.get_string())).pack())
28}
29
30pub fn panic_lib() -> ConstTree { ConstTree::ns("std::panic", [xfn_leaf(orc_panic)]) }