tvl-depot/tvix/eval/src/value/attrs.rs
Vincent Ambo e876c3a41c fix(tvix/value): KV struct needs to carry name as Value, too
Users may construct a pair that falls into the name/value optimisation
but where `name` is not actually a string, as from the language
perspective there is nothing special about this attribute set.

We also can not conditionally apply this by forcing the key at this
point, as this would change the language semantics.

Therefore, the name in the optimised representation is also carried as
`Value`.

Change-Id: I5be8a4c98ba19ebdfb7203a929f714a04492512e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6101
Reviewed-by: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
2022-08-13 20:24:12 +00:00

43 lines
1.1 KiB
Rust

/// This module implements Nix attribute sets. They have flexible
/// backing implementations, as they are used in very versatile
/// use-cases that are all exposed the same way in the language
/// surface.
use std::collections::BTreeMap;
use std::fmt::Display;
use super::string::NixString;
use super::Value;
#[derive(Debug)]
pub enum NixAttrs {
Map(BTreeMap<NixString, Value>),
KV { name: Value, value: Value },
}
impl Display for NixAttrs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("{ ")?;
match self {
NixAttrs::KV { name, value } => {
f.write_fmt(format_args!("name = \"{}\"; ", name))?;
f.write_fmt(format_args!("value = {}; ", value))?;
f.write_str("/* optimised pair! */")?;
}
NixAttrs::Map(map) => {
for (name, value) in map {
f.write_fmt(format_args!("{} = {}; ", name, value))?;
}
}
}
f.write_str("}")
}
}
impl PartialEq for NixAttrs {
fn eq(&self, _other: &Self) -> bool {
todo!("attrset equality")
}
}