feat: Add EvalState.require_attrs_names (sorted)

(cherry picked from commit 0b1aca5a1ee7136e76a201477643cfc3acbf2676)
This commit is contained in:
Robert Hensing 2024-12-02 14:02:51 +01:00
parent 237a2281c1
commit 76b92a577d

View file

@ -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<Vec<String>> {
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<Vec<String>> {
let t = self.value_type(v)?;
if t != ValueType::AttrSet {