about summary refs log tree commit diff
path: root/tvix/eval/src/value/attrs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tvix/eval/src/value/attrs.rs')
-rw-r--r--tvix/eval/src/value/attrs.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs
index 9204a5bb955a..ecb819fad77c 100644
--- a/tvix/eval/src/value/attrs.rs
+++ b/tvix/eval/src/value/attrs.rs
@@ -45,6 +45,26 @@ impl AttrsRep {
             }
         }
     }
+
+    fn select(&self, key: &str) -> Option<&Value> {
+        match self {
+            AttrsRep::Empty => None,
+
+            AttrsRep::KV { name, value } => {
+                if key == "name" {
+                    return Some(&name);
+                }
+
+                if key == "value" {
+                    return Some(&value);
+                }
+
+                None
+            }
+
+            AttrsRep::Map(map) => map.get(&key.to_string().into()),
+        }
+    }
 }
 
 #[derive(Clone, Debug)]
@@ -133,6 +153,11 @@ impl NixAttrs {
         }
     }
 
+    // Select a value from an attribute set by key.
+    pub fn select(&self, key: &str) -> Option<&Value> {
+        self.0.select(key)
+    }
+
     /// Implement construction logic of an attribute set, to encapsulate
     /// logic about attribute set optimisations inside of this module.
     pub fn construct(count: usize, mut stack_slice: Vec<Value>) -> EvalResult<Self> {