feat: TODO
This commit is contained in:
@@ -10,7 +10,7 @@ use hashbrown::hash_map::Entry;
|
||||
use itertools::Itertools;
|
||||
|
||||
use nixjit_error::{Error, Result};
|
||||
use nixjit_ir::ExprId;
|
||||
use nixjit_ir::{ExprId, SymId};
|
||||
use nixjit_value::{self as p, format_symbol};
|
||||
|
||||
use crate::EvalContext;
|
||||
@@ -24,7 +24,7 @@ use super::Value;
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Constructor)]
|
||||
pub struct AttrSet {
|
||||
data: HashMap<String, Value>,
|
||||
data: HashMap<SymId, Value>,
|
||||
}
|
||||
|
||||
impl Debug for AttrSet {
|
||||
@@ -33,23 +33,23 @@ impl Debug for AttrSet {
|
||||
write!(f, "{{ ")?;
|
||||
for (k, v) in self.data.iter() {
|
||||
match v {
|
||||
List(_) => write!(f, "{} = [ ... ]; ", format_symbol(k))?,
|
||||
AttrSet(_) => write!(f, "{} = {{ ... }}; ", format_symbol(k))?,
|
||||
v => write!(f, "{} = {v:?}; ", format_symbol(k))?,
|
||||
List(_) => write!(f, "{:?} = [ ... ]; ", k)?,
|
||||
AttrSet(_) => write!(f, "{:?} = {{ ... }}; ", k)?,
|
||||
v => write!(f, "{:?} = {v:?}; ", k)?,
|
||||
}
|
||||
}
|
||||
write!(f, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HashMap<String, Value>> for AttrSet {
|
||||
fn from(data: HashMap<String, Value>) -> Self {
|
||||
impl From<HashMap<SymId, Value>> for AttrSet {
|
||||
fn from(data: HashMap<SymId, Value>) -> Self {
|
||||
Self { data }
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for AttrSet {
|
||||
type Target = HashMap<String, Value>;
|
||||
type Target = HashMap<SymId, Value>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.data
|
||||
}
|
||||
@@ -64,16 +64,16 @@ impl AttrSet {
|
||||
}
|
||||
|
||||
/// Inserts an attribute, overwriting any existing attribute with the same name.
|
||||
pub fn push_attr_force(&mut self, sym: String, val: Value) {
|
||||
pub fn push_attr_force(&mut self, sym: SymId, val: Value) {
|
||||
self.data.insert(sym, val);
|
||||
}
|
||||
|
||||
/// Inserts an attribute, returns an error if the attribute is already defined.
|
||||
pub fn push_attr(&mut self, sym: String, val: Value) -> Result<()> {
|
||||
pub fn push_attr(&mut self, sym: SymId, val: Value, ctx: &mut impl EvalContext) -> Result<()> {
|
||||
match self.data.entry(sym) {
|
||||
Entry::Occupied(occupied) => Err(Error::eval_error(format!(
|
||||
"attribute '{}' already defined",
|
||||
format_symbol(occupied.key())
|
||||
format_symbol(ctx.get_sym(*occupied.key()))
|
||||
))),
|
||||
Entry::Vacant(vacant) => {
|
||||
vacant.insert(val);
|
||||
@@ -82,29 +82,29 @@ impl AttrSet {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&self, name: &str, ctx: &mut impl EvalContext) -> Result<Value> {
|
||||
pub fn select(&self, name: SymId, ctx: &mut impl EvalContext) -> Result<Value> {
|
||||
self.data
|
||||
.get(name)
|
||||
.get(&name)
|
||||
.cloned()
|
||||
.map(|attr| match attr {
|
||||
Value::Thunk(id) => ctx.eval(id),
|
||||
Value::Thunk(id) => ctx.force(id),
|
||||
val => Ok(val),
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
Error::eval_error(format!("attribute '{}' not found", format_symbol(name)))
|
||||
Error::eval_error(format!("attribute '{}' not found", format_symbol(ctx.get_sym(name))))
|
||||
})?
|
||||
}
|
||||
|
||||
pub fn select_or(
|
||||
&self,
|
||||
name: &str,
|
||||
name: SymId,
|
||||
default: ExprId,
|
||||
ctx: &mut impl EvalContext,
|
||||
) -> Result<Value> {
|
||||
self.data
|
||||
.get(name)
|
||||
.get(&name)
|
||||
.map(|attr| match attr {
|
||||
&Value::Thunk(id) => ctx.eval(id),
|
||||
&Value::Thunk(id) => ctx.force(id),
|
||||
val => Ok(val.clone()),
|
||||
})
|
||||
.unwrap_or_else(|| ctx.eval(default))
|
||||
@@ -113,19 +113,19 @@ impl AttrSet {
|
||||
/// Checks if an attribute path exists within the set.
|
||||
pub fn has_attr(
|
||||
&self,
|
||||
mut path: impl DoubleEndedIterator<Item = Result<Value>>,
|
||||
mut path: impl DoubleEndedIterator<Item = Result<SymId>>,
|
||||
) -> Result<Value> {
|
||||
let mut data = &self.data;
|
||||
let last = path.nth_back(0).unwrap();
|
||||
for item in path {
|
||||
let Some(Value::AttrSet(attrs)) = data.get(&item.unwrap().force_string_no_ctx()?)
|
||||
let Some(Value::AttrSet(attrs)) = data.get(&item?)
|
||||
else {
|
||||
return Ok(Value::Bool(false));
|
||||
};
|
||||
data = attrs.as_inner();
|
||||
}
|
||||
Ok(Value::Bool(
|
||||
data.get(&last.unwrap().force_string_no_ctx()?).is_some(),
|
||||
data.get(&last?).is_some(),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -138,24 +138,18 @@ impl AttrSet {
|
||||
}
|
||||
|
||||
/// Returns a reference to the inner `HashMap`.
|
||||
pub fn as_inner(&self) -> &HashMap<String, Value> {
|
||||
pub fn as_inner(&self) -> &HashMap<SymId, Value> {
|
||||
&self.data
|
||||
}
|
||||
|
||||
/// Converts an `Rc<AttrSet>` to an `Rc<HashMap<String, Value>>` without allocation.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This is safe because `AttrSet` is `#[repr(transparent)]`.
|
||||
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<String, Value>> {
|
||||
// SAFETY: This is safe because `AttrSet` is `#[repr(transparent)]` over
|
||||
// `HashMap<String, Value>`, so `Rc<Self>` has the same layout as
|
||||
// `Rc<HashMap<String, Value>>`.
|
||||
unsafe { core::mem::transmute(self) }
|
||||
}
|
||||
|
||||
/// Creates an `AttrSet` from a `HashMap`.
|
||||
pub fn from_inner(data: HashMap<String, Value>) -> Self {
|
||||
Self { data }
|
||||
}
|
||||
|
||||
/// Performs a deep equality comparison between two `AttrSet`s.
|
||||
///
|
||||
/// It recursively compares the contents of both sets, ensuring that both keys
|
||||
@@ -171,11 +165,11 @@ impl AttrSet {
|
||||
}
|
||||
|
||||
/// Converts the `AttrSet` to its public-facing representation.
|
||||
pub fn to_public(self) -> p::Value {
|
||||
pub fn to_public(self, ctx: &mut impl EvalContext) -> p::Value {
|
||||
p::Value::AttrSet(p::AttrSet::new(
|
||||
self.data
|
||||
.into_iter()
|
||||
.map(|(sym, value)| (sym.into(), value.to_public()))
|
||||
.map(|(sym, value)| (ctx.get_sym(sym).into(), value.to_public(ctx)))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user