From 76b92a577d2e7c055a519f050827869ff4dd3f54 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 2 Dec 2024 14:02:51 +0100 Subject: [PATCH] feat: Add EvalState.require_attrs_names (sorted) (cherry picked from commit 0b1aca5a1ee7136e76a201477643cfc3acbf2676) --- rust/nix-expr/src/eval_state.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rust/nix-expr/src/eval_state.rs b/rust/nix-expr/src/eval_state.rs index efca46d..1afce0e 100644 --- a/rust/nix-expr/src/eval_state.rs +++ b/rust/nix-expr/src/eval_state.rs @@ -208,8 +208,20 @@ impl EvalState { } unsafe { check_call!(raw::get_int(&mut self.context, v.raw_ptr())) } } + /// Evaluate, and require that the value is an attrset. /// Returns a list of the keys in the attrset. + /// + /// NOTE: this currently implements its own sorting, which probably matches Nix's implementation, but is not guaranteed. + pub fn require_attrs_names(&mut self, v: &Value) -> Result> { + self.require_attrs_names_unsorted(v).map(|mut v| { + v.sort(); + v + }) + } + + /// For when [require_attrs_names] isn't fast enough. + /// Only use when it's ok that the keys are returned in an arbitrary order. pub fn require_attrs_names_unsorted(&mut self, v: &Value) -> Result> { let t = self.value_type(v)?; if t != ValueType::AttrSet {