about summary refs log tree commit diff
path: root/tvix/eval/src/value/attrs.rs
blob: e2ebc7cb344a7660e552381ef704bf97e9921402 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// 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")
    }
}