From cf82a545eb68b57ceb940ebd467e669ca19d7483 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 5 Nov 2022 16:51:37 +0300 Subject: feat(tvix/eval): add helper for selecting required attributes Change-Id: Idd4ae78ef55891d89b72b5c2f3afc8b697b4b26e Reviewed-on: https://cl.tvl.fyi/c/depot/+/7189 Reviewed-by: grfn Reviewed-by: Adam Joseph Tested-by: BuildkiteCI Autosubmit: tazjin --- tvix/eval/src/value/attrs.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'tvix/eval') diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs index 59e477db4acf..d8e3001f4a18 100644 --- a/tvix/eval/src/value/attrs.rs +++ b/tvix/eval/src/value/attrs.rs @@ -217,6 +217,13 @@ impl NixAttrs { self.0.select(key) } + /// Select a required value from an attribute set by key, return + /// an `AttributeNotFound` error if it is missing. + pub fn select_required(&self, key: &str) -> Result<&Value, ErrorKind> { + self.select(key) + .ok_or_else(|| ErrorKind::AttributeNotFound { name: key.into() }) + } + pub fn contains(&self, key: &str) -> bool { self.0.contains(key) } -- cgit 1.4.1