feat: error handling

This commit is contained in:
2025-08-08 19:35:51 +08:00
parent a9cfddbf5c
commit fd182b6233
12 changed files with 187 additions and 311 deletions

View File

@@ -15,26 +15,6 @@ use std::sync::LazyLock;
use derive_more::{Constructor, IsVariant, Unwrap};
use regex::Regex;
/// Represents errors thrown by `assert` and `throw` expressions in Nix.
/// These errors can potentially be caught and handled by `builtins.tryEval`.
#[derive(Clone, Debug, PartialEq, Constructor, Hash)]
pub struct Catchable {
/// The error message.
msg: String,
}
impl<T: Into<String>> From<T> for Catchable {
fn from(value: T) -> Self {
Catchable { msg: value.into() }
}
}
impl Display for Catchable {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "<error: {}>", self.msg)
}
}
/// Represents a constant, primitive value in Nix.
#[derive(Debug, Clone, Copy, PartialEq, IsVariant, Unwrap)]
pub enum Const {
@@ -206,8 +186,6 @@ pub enum Value {
AttrSet(AttrSet),
/// A list.
List(List),
/// A catchable error.
Catchable(Catchable),
/// A thunk, representing a delayed computation.
Thunk,
/// A function (lambda).
@@ -229,7 +207,6 @@ impl Display for Value {
String(x) => write!(f, r#""{x}""#),
AttrSet(x) => write!(f, "{x}"),
List(x) => write!(f, "{x}"),
Catchable(x) => write!(f, "{x}"),
Thunk => write!(f, "<CODE>"),
Func => write!(f, "<LAMBDA>"),
PrimOp(x) => write!(f, "<PRIMOP {x}>"),