refactor: reduce coupling

This commit is contained in:
2025-07-28 21:37:27 +08:00
parent 78e3c5a26e
commit 7afb2a7b1c
53 changed files with 2964 additions and 3444 deletions

View File

@@ -0,0 +1,8 @@
[package]
name = "nixjit_value"
version = "0.1.0"
edition = "2024"
[dependencies]
derive_more = { version = "2.0", features = ["full"] }
regex = "1.11"

View File

@@ -0,0 +1,201 @@
use core::fmt::{Debug, Display, Formatter, Result as FmtResult};
use core::hash::Hash;
use core::ops::Deref;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::sync::LazyLock;
use derive_more::{Constructor, IsVariant, Unwrap};
use regex::Regex;
#[derive(Clone, Debug, PartialEq, Constructor, Hash)]
pub struct Catchable {
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)
}
}
#[derive(Debug, Clone, Copy, PartialEq, IsVariant, Unwrap)]
pub enum Const {
Bool(bool),
Int(i64),
Float(f64),
Null,
}
impl Display for Const {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
use Const::*;
match self {
Int(x) => write!(f, "{x}"),
Float(x) => write!(f, "{x}"),
Bool(x) => write!(f, "{x}"),
Null => write!(f, "null"),
}
}
}
impl From<bool> for Const {
fn from(value: bool) -> Self {
Const::Bool(value)
}
}
impl From<i64> for Const {
fn from(value: i64) -> Self {
Const::Int(value)
}
}
impl From<f64> for Const {
fn from(value: f64) -> Self {
Const::Float(value)
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Constructor)]
pub struct Symbol(String);
impl<T: Into<String>> From<T> for Symbol {
fn from(value: T) -> Self {
Symbol(value.into())
}
}
pub fn format_symbol<'a>(sym: &'a str) -> Cow<'a, str> {
if REGEX.is_match(sym) {
Cow::Borrowed(sym)
} else {
Cow::Owned(format!(r#""{sym}""#))
}
}
impl Display for Symbol {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
if self.normal() {
write!(f, "{}", self.0)
} else {
write!(f, r#""{}""#, self.0)
}
}
}
static REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"^[a-zA-Z\_][a-zA-Z0-9\_\'\-]*$"#).unwrap());
impl Symbol {
fn normal(&self) -> bool {
REGEX.is_match(self)
}
}
impl Deref for Symbol {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Symbol {
pub fn into_inner(self) -> String {
self.0
}
pub fn as_inner(&self) -> &String {
&self.0
}
}
#[derive(Constructor, Clone, PartialEq)]
pub struct AttrSet {
data: BTreeMap<Symbol, Value>,
}
impl Debug for AttrSet {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
use Value::*;
write!(f, "{{ ")?;
for (k, v) in self.data.iter() {
match v {
List(_) => write!(f, "{k:?} = [ ... ]; ")?,
AttrSet(_) => write!(f, "{k:?} = {{ ... }}; ")?,
v => write!(f, "{k:?} = {v:?}; ")?,
}
}
write!(f, "}}")
}
}
impl Display for AttrSet {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
use Value::*;
write!(f, "{{ ")?;
for (k, v) in self.data.iter() {
write!(f, "{k} = ")?;
match v {
AttrSet(_) => write!(f, "{{ ... }}"),
List(_) => write!(f, "[ ... ]"),
v => write!(f, "{v}"),
}?;
write!(f, "; ")?;
}
write!(f, "}}")
}
}
#[derive(Constructor, Clone, Debug, PartialEq)]
pub struct List {
data: Vec<Value>,
}
impl Display for List {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "[ ")?;
for v in self.data.iter() {
write!(f, "{v} ")?;
}
write!(f, "]")
}
}
#[derive(IsVariant, Unwrap, Clone, Debug, PartialEq)]
pub enum Value {
Const(Const),
String(String),
AttrSet(AttrSet),
List(List),
Catchable(Catchable),
Thunk,
Func,
PrimOp(&'static str),
PrimOpApp(&'static str),
Repeated,
}
impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
use Value::*;
match self {
Const(x) => write!(f, "{x}"),
String(x) => write!(f, "{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}>"),
PrimOpApp(x) => write!(f, "<PRIMOP-APP {x}>"),
Repeated => write!(f, "<REPEATED>"),
}
}
}