about summary refs log tree commit diff
path: root/tvix/eval
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-08T14·09+0300
committertazjin <tazjin@tvl.su>2022-08-13T11·50+0000
commitba03226e514b9bc55e5da35830d5fe6cadcf988c (patch)
treef5edb2313022923967de556811a67f7593feff13 /tvix/eval
parentd26c097d1b5f99ace10e60243280f4687cfc3cb9 (diff)
feat(tvix/eval): add module for attribute set implementations r/4422
Change-Id: I6002bd5e5596b93325dea6c862370ba5235c0f08
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6086
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'tvix/eval')
-rw-r--r--tvix/eval/src/value/attrs.rs36
-rw-r--r--tvix/eval/src/value/mod.rs1
2 files changed, 37 insertions, 0 deletions
diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs
new file mode 100644
index 000000000000..69688d3c5122
--- /dev/null
+++ b/tvix/eval/src/value/attrs.rs
@@ -0,0 +1,36 @@
+/// 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: NixString, 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))?;
+            }
+
+            NixAttrs::Map(map) => {
+                for (name, value) in map {
+                    f.write_fmt(format_args!("{} = {}; ", name, value))?;
+                }
+            }
+        }
+
+        f.write_str("}")
+    }
+}
diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs
index 7052df3d4e66..628eefb8fdd8 100644
--- a/tvix/eval/src/value/mod.rs
+++ b/tvix/eval/src/value/mod.rs
@@ -4,6 +4,7 @@ use std::fmt::Display;
 
 use crate::errors::{Error, EvalResult};
 
+mod attrs;
 mod string;
 
 #[derive(Clone, Copy, Debug, PartialEq)]