1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
//! This module implements Nix attribute sets, backed by Rust hash maps.
use std::borrow::Borrow;
use std::collections::hash_map;
use std::hash::Hash;
use std::iter::FromIterator;
use std::rc::Rc;
use itertools::Itertools as _;
use rustc_hash::FxHashMap;
use serde::de::{Deserializer, Error, Visitor};
use serde::Deserialize;
use super::string::NixString;
use super::thunk::ThunkSet;
use super::TotalDisplay;
use super::Value;
use crate::errors::ErrorKind;
use crate::CatchableErrorKind;
#[cfg(test)]
mod tests;
type AttrsRep = FxHashMap<NixString, Value>;
#[repr(transparent)]
#[derive(Clone, Debug, Default)]
pub struct NixAttrs(Rc<AttrsRep>);
impl From<AttrsRep> for NixAttrs {
fn from(rep: AttrsRep) -> Self {
NixAttrs(Rc::new(rep))
}
}
impl<K, V> FromIterator<(K, V)> for NixAttrs
where
NixString: From<K>,
Value: From<V>,
{
fn from_iter<T>(iter: T) -> NixAttrs
where
T: IntoIterator<Item = (K, V)>,
{
iter.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect::<AttrsRep>()
.into()
}
}
impl TotalDisplay for NixAttrs {
fn total_fmt(&self, f: &mut std::fmt::Formatter<'_>, set: &mut ThunkSet) -> std::fmt::Result {
if let Some(Value::String(s)) = self.select_str("type") {
if *s == "derivation" {
write!(f, "«derivation ")?;
if let Some(p) = self.select_str("drvPath") {
p.total_fmt(f, set)?;
} else {
write!(f, "???")?;
}
return write!(f, "»");
}
}
f.write_str("{ ")?;
for (name, value) in self.iter_sorted() {
write!(f, "{} = ", name.ident_str())?;
value.total_fmt(f, set)?;
f.write_str("; ")?;
}
f.write_str("}")
}
}
impl<'de> Deserialize<'de> for NixAttrs {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct MapVisitor;
impl<'de> Visitor<'de> for MapVisitor {
type Value = NixAttrs;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a valid Nix attribute set")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut stack_array = Vec::with_capacity(map.size_hint().unwrap_or(0) * 2);
while let Some((key, value)) = map.next_entry()? {
stack_array.push(key);
stack_array.push(value);
}
Ok(NixAttrs::construct(stack_array.len() / 2, stack_array)
.map_err(A::Error::custom)?
.expect("Catchable values are unreachable here"))
}
}
deserializer.deserialize_map(MapVisitor)
}
}
impl NixAttrs {
pub fn empty() -> Self {
AttrsRep::default().into()
}
/// Compare two attribute sets by pointer equality, but returning `false`
/// does not mean that the two attribute sets do not have equal content.
pub fn ptr_eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
/// Return an attribute set containing the merge of the two
/// provided sets. Keys from the `other` set have precedence.
pub fn update(self, other: Self) -> Self {
let mut out = Rc::unwrap_or_clone(self.0);
for (key, value) in other {
out.insert(key, value);
}
out.into()
}
/// Return the number of key-value entries in an attrset.
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Select a value from an attribute set by key.
pub fn select<Q>(&self, key: &Q) -> Option<&Value>
where
NixString: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.get(key)
}
/// Select a value from an attribute set by a key in string format. This is
/// separated out to avoid unintended copies, as the NixString
/// representation is not guaranteed to be valid UTF-8 and doesn't fit the
/// usual `Borrow` trick.
pub fn select_str(&self, key: &str) -> Option<&Value> {
self.select(key.as_bytes())
}
/// 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.0
.get(key.as_bytes())
.ok_or_else(|| ErrorKind::AttributeNotFound {
name: key.to_string(),
})
}
pub fn contains<Q>(&self, key: &Q) -> bool
where
NixString: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.contains_key(key)
}
/// Construct an iterator over all the key-value pairs in the attribute set.
#[allow(clippy::needless_lifetimes)]
pub fn iter<'a>(&'a self) -> Iter<KeyValue<'a>> {
Iter(KeyValue::Map(self.0.iter()))
}
/// Construct an iterator over all the key-value pairs in lexicographic
/// order of their keys.
pub fn iter_sorted(&self) -> Iter<KeyValue<'_>> {
let sorted = self.0.iter().sorted_by_key(|x| x.0);
Iter(KeyValue::Sorted(sorted))
}
/// Same as [IntoIterator::into_iter], but marks call sites which rely on the
/// iteration being lexicographic.
pub fn into_iter_sorted(self) -> OwnedAttrsIterator {
OwnedAttrsIterator(IntoIterRepr::Finite(
self.0
.as_ref()
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.sorted_by(|(a, _), (b, _)| a.cmp(b)),
))
}
/// Construct an iterator over all the keys of the attribute set
pub fn keys(&self) -> Keys<'_> {
Keys(KeysInner::Map(self.0.keys()))
}
/// Same as [Self::keys], but marks call sites which rely on the
/// iteration being lexicographic.
pub fn keys_sorted(&self) -> Keys<'_> {
Keys(KeysInner::Sorted(self.0.keys().sorted()))
}
/// 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>,
) -> Result<Result<Self, CatchableErrorKind>, ErrorKind> {
debug_assert!(
stack_slice.len() == count * 2,
"construct_attrs called with count == {}, but slice.len() == {}",
count,
stack_slice.len(),
);
let mut attrs_map = FxHashMap::with_capacity_and_hasher(count, rustc_hash::FxBuildHasher);
for _ in 0..count {
let value = stack_slice.pop().unwrap();
let key = stack_slice.pop().unwrap();
match key {
Value::String(ks) => set_attr(&mut attrs_map, ks, value)?,
Value::Null => {
// This is in fact valid, but leads to the value being
// ignored and nothing being set, i.e. `{ ${null} = 1; } =>
// { }`.
continue;
}
Value::Catchable(err) => return Ok(Err(*err)),
other => return Err(ErrorKind::InvalidAttributeName(other)),
}
}
Ok(Ok(attrs_map.into()))
}
/// Calculate the intersection of the attribute sets.
/// The right side value is used when the keys match.
pub(crate) fn intersect(&self, other: &Self) -> NixAttrs {
let lhs = &self.0;
let rhs = &other.0;
let mut out = FxHashMap::with_capacity_and_hasher(
std::cmp::min(lhs.len(), rhs.len()),
rustc_hash::FxBuildHasher,
);
if lhs.len() < rhs.len() {
for key in lhs.keys() {
if let Some(val) = rhs.get(key) {
out.insert(key.clone(), val.clone());
}
}
} else {
for (key, val) in rhs.iter() {
if lhs.contains_key(key) {
out.insert(key.clone(), val.clone());
}
}
};
out.into()
}
}
impl IntoIterator for NixAttrs {
type Item = (NixString, Value);
type IntoIter = OwnedAttrsIterator;
fn into_iter(self) -> Self::IntoIter {
OwnedAttrsIterator(IntoIterRepr::Map(Rc::unwrap_or_clone(self.0).into_iter()))
}
}
/// Set an attribute on an in-construction attribute set, while
/// checking against duplicate keys.
fn set_attr(map: &mut AttrsRep, key: NixString, value: Value) -> Result<(), ErrorKind> {
match map.entry(key) {
hash_map::Entry::Occupied(entry) => Err(ErrorKind::DuplicateAttrsKey {
key: entry.key().to_string(),
}),
hash_map::Entry::Vacant(entry) => {
entry.insert(value);
Ok(())
}
}
}
/// Iterator representation over the keys *and* values of an attribute
/// set.
pub enum KeyValue<'a> {
Map(hash_map::Iter<'a, NixString, Value>),
Sorted(std::vec::IntoIter<(&'a NixString, &'a Value)>),
}
/// Iterator over a Nix attribute set.
// This wrapper type exists to make the inner "raw" iterator
// inaccessible.
#[repr(transparent)]
pub struct Iter<T>(T);
impl<'a> Iterator for Iter<KeyValue<'a>> {
type Item = (&'a NixString, &'a Value);
fn next(&mut self) -> Option<Self::Item> {
match &mut self.0 {
KeyValue::Map(inner) => inner.next(),
KeyValue::Sorted(inner) => inner.next(),
}
}
}
impl ExactSizeIterator for Iter<KeyValue<'_>> {
fn len(&self) -> usize {
match &self.0 {
KeyValue::Map(inner) => inner.len(),
KeyValue::Sorted(inner) => inner.len(),
}
}
}
enum KeysInner<'a> {
Map(hash_map::Keys<'a, NixString, Value>),
Sorted(std::vec::IntoIter<&'a NixString>),
}
pub struct Keys<'a>(KeysInner<'a>);
impl<'a> Iterator for Keys<'a> {
type Item = &'a NixString;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.0 {
KeysInner::Map(m) => m.next(),
KeysInner::Sorted(v) => v.next(),
}
}
}
impl<'a> IntoIterator for &'a NixAttrs {
type Item = (&'a NixString, &'a Value);
type IntoIter = Iter<KeyValue<'a>>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl ExactSizeIterator for Keys<'_> {
fn len(&self) -> usize {
match &self.0 {
KeysInner::Map(m) => m.len(),
KeysInner::Sorted(v) => v.len(),
}
}
}
/// Internal representation of an owning attrset iterator
pub enum IntoIterRepr {
Finite(std::vec::IntoIter<(NixString, Value)>),
Map(hash_map::IntoIter<NixString, Value>),
}
/// Wrapper type which hides the internal implementation details from
/// users.
#[repr(transparent)]
pub struct OwnedAttrsIterator(IntoIterRepr);
impl Iterator for OwnedAttrsIterator {
type Item = (NixString, Value);
fn next(&mut self) -> Option<Self::Item> {
match &mut self.0 {
IntoIterRepr::Finite(inner) => inner.next(),
IntoIterRepr::Map(m) => m.next(),
}
}
}
impl ExactSizeIterator for OwnedAttrsIterator {
fn len(&self) -> usize {
match &self.0 {
IntoIterRepr::Finite(inner) => inner.len(),
IntoIterRepr::Map(inner) => inner.len(),
}
}
}
impl DoubleEndedIterator for OwnedAttrsIterator {
fn next_back(&mut self) -> Option<Self::Item> {
match &mut self.0 {
IntoIterRepr::Finite(inner) => inner.next_back(),
// hashmaps have arbitary iteration order, so reversing it would not make a difference
IntoIterRepr::Map(inner) => inner.next(),
}
}
}
|