Skip to main content

nu_protocol/value/
record.rs

1//! Our insertion ordered map-type [`Record`]
2use crate::{
3    CollectionColumns, CompareTypes, ShellError, Span, Type, TypeRelation, Value,
4    casing::{CaseInsensitive, CaseSensitive, CaseSensitivity, Casing, WrapCased},
5    value::HASH_ITEM_LIMIT,
6};
7use itertools::Itertools;
8use serde::{Deserialize, Serialize, de::Visitor, ser::SerializeMap};
9use std::{
10    fmt::Debug,
11    hash::{Hash, Hasher},
12    iter::FusedIterator,
13    marker::PhantomData,
14    ops::{Deref, DerefMut, Index, RangeBounds},
15};
16
17#[derive(Clone, Default, PartialEq)]
18pub struct Record {
19    inner: Vec<(String, Value)>,
20}
21
22impl Debug for Record {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        f.debug_map()
25            .entries(self.inner.iter().map(|(k, v)| (k, v)))
26            .finish()
27    }
28}
29
30/// Follows [`Value::strict_eq`] for records, not the derived, order-sensitive [`PartialEq`].
31///
32/// Like `Value`, `Record` must never implement [`Eq`]:
33///
34/// ```compile_fail,E0277
35/// fn requires_eq<T: Eq>() {}
36/// requires_eq::<nu_protocol::Record>();
37/// ```
38impl Hash for Record {
39    /// Hashes the length, then the `HASH_ITEM_LIMIT` smallest keys with their values.
40    ///
41    /// Selecting keys by order rather than by position makes the hash independent of insertion
42    /// order, which matches [`Value::strict_eq`]. Duplicate keys are taken in their original
43    /// order, the same tie-break as the stable sort `strict_eq` uses. The limit keeps hashing wide
44    /// records cheap; records that differ only in later keys collide, and the map's equality
45    /// check separates them.
46    fn hash<H: Hasher>(&self, state: &mut H) {
47        self.len().hash(state);
48        for (_, (key, value)) in self
49            .inner
50            .iter()
51            .enumerate()
52            .k_smallest_by_key(HASH_ITEM_LIMIT, |&(index, (key, _))| (key, index))
53        {
54            key.hash(state);
55            value.hash(state);
56        }
57    }
58}
59
60/// A wrapper around [`Record`] that handles lookups. Whether the keys are compared case sensitively
61/// or not is controlled with the `Sensitivity` parameter.
62///
63/// It is never actually constructed as a value and only used as a reference to an existing [`Record`].
64#[repr(transparent)]
65pub struct CasedRecord<Sensitivity: CaseSensitivity>(Record, PhantomData<Sensitivity>);
66
67impl<Sensitivity: CaseSensitivity> CasedRecord<Sensitivity> {
68    #[inline]
69    const fn from_record(record: &Record) -> &Self {
70        // SAFETY: `CasedRecord` has the same memory layout as `Record`.
71        unsafe { &*(record as *const Record as *const Self) }
72    }
73
74    #[inline]
75    const fn from_record_mut(record: &mut Record) -> &mut Self {
76        // SAFETY: `CasedRecord` has the same memory layout as `Record`.
77        unsafe { &mut *(record as *mut Record as *mut Self) }
78    }
79
80    pub fn index_of(&self, col: impl AsRef<str>) -> Option<usize> {
81        let col = col.as_ref();
82        self.0.columns().rposition(|k| Sensitivity::eq(k, col))
83    }
84
85    pub fn contains(&self, col: impl AsRef<str>) -> bool {
86        self.index_of(col.as_ref()).is_some()
87    }
88
89    pub fn get(&self, col: impl AsRef<str>) -> Option<&Value> {
90        let index = self.index_of(col.as_ref())?;
91        Some(self.0.get_index(index)?.1)
92    }
93
94    pub fn get_mut(&mut self, col: impl AsRef<str>) -> Option<&mut Value> {
95        let index = self.index_of(col.as_ref())?;
96        Some(self.0.get_index_mut(index)?.1)
97    }
98
99    /// Remove single value by key and return it
100    pub fn remove(&mut self, col: impl AsRef<str>) -> Option<Value> {
101        let index = self.index_of(col.as_ref())?;
102        Some(self.0.remove_index(index))
103    }
104
105    /// Insert into the record, replacing preexisting value if found.
106    ///
107    /// Returns `Some(previous_value)` if found. Else `None`
108    pub fn insert<K>(&mut self, col: K, val: Value) -> Option<Value>
109    where
110        K: AsRef<str> + Into<String>,
111    {
112        if let Some(curr_val) = self.get_mut(col.as_ref()) {
113            Some(std::mem::replace(curr_val, val))
114        } else {
115            self.0.push(col, val);
116            None
117        }
118    }
119}
120
121impl<'a> WrapCased for &'a Record {
122    type Wrapper<S: CaseSensitivity> = &'a CasedRecord<S>;
123
124    #[inline]
125    fn case_sensitive(self) -> Self::Wrapper<CaseSensitive> {
126        CasedRecord::<CaseSensitive>::from_record(self)
127    }
128
129    #[inline]
130    fn case_insensitive(self) -> Self::Wrapper<CaseInsensitive> {
131        CasedRecord::<CaseInsensitive>::from_record(self)
132    }
133}
134
135impl<'a> WrapCased for &'a mut Record {
136    type Wrapper<S: CaseSensitivity> = &'a mut CasedRecord<S>;
137
138    #[inline]
139    fn case_sensitive(self) -> Self::Wrapper<CaseSensitive> {
140        CasedRecord::<CaseSensitive>::from_record_mut(self)
141    }
142
143    #[inline]
144    fn case_insensitive(self) -> Self::Wrapper<CaseInsensitive> {
145        CasedRecord::<CaseInsensitive>::from_record_mut(self)
146    }
147}
148
149impl AsRef<Record> for Record {
150    fn as_ref(&self) -> &Record {
151        self
152    }
153}
154
155impl AsMut<Record> for Record {
156    fn as_mut(&mut self) -> &mut Record {
157        self
158    }
159}
160
161impl Deref for Record {
162    type Target = CasedRecord<CaseSensitive>;
163
164    fn deref(&self) -> &Self::Target {
165        self.case_sensitive()
166    }
167}
168
169impl DerefMut for Record {
170    fn deref_mut(&mut self) -> &mut Self::Target {
171        self.case_sensitive()
172    }
173}
174
175impl<S: AsRef<str>> Index<S> for Record {
176    type Output = Value;
177
178    #[inline]
179    #[track_caller]
180    fn index(&self, index: S) -> &Self::Output {
181        self.get(index.as_ref())
182            .expect("no entry found for key in record")
183    }
184}
185
186/// A wrapper around [`Record`] that affects whether key comparisons are case sensitive or not.
187///
188/// Implements commonly used methods of [`Record`].
189pub struct DynCasedRecord<R> {
190    record: R,
191    casing: Casing,
192}
193
194impl Clone for DynCasedRecord<&Record> {
195    fn clone(&self) -> Self {
196        *self
197    }
198}
199
200impl Copy for DynCasedRecord<&Record> {}
201
202impl<'a> DynCasedRecord<&'a Record> {
203    pub fn index_of(self, col: impl AsRef<str>) -> Option<usize> {
204        match self.casing {
205            Casing::Sensitive => self.record.case_sensitive().index_of(col.as_ref()),
206            Casing::Insensitive => self.record.case_insensitive().index_of(col.as_ref()),
207        }
208    }
209
210    pub fn contains(self, col: impl AsRef<str>) -> bool {
211        self.get(col.as_ref()).is_some()
212    }
213
214    pub fn get(self, col: impl AsRef<str>) -> Option<&'a Value> {
215        match self.casing {
216            Casing::Sensitive => self.record.case_sensitive().get(col.as_ref()),
217            Casing::Insensitive => self.record.case_insensitive().get(col.as_ref()),
218        }
219    }
220}
221
222impl<'a> DynCasedRecord<&'a mut Record> {
223    /// Explicit reborrowing. See [Self::reborrow_mut()]
224    pub fn reborrow(&self) -> DynCasedRecord<&Record> {
225        DynCasedRecord {
226            record: &*self.record,
227            casing: self.casing,
228        }
229    }
230
231    /// Explicit reborrowing. Using this before methods that receive `self` is necessary to avoid
232    /// consuming the `DynCasedRecord` instance.
233    ///
234    /// ```
235    /// use nu_protocol::{record, record::{Record, DynCasedRecord}, Value, casing::Casing};
236    ///
237    /// let mut rec = record!{
238    ///     "A" => Value::test_nothing(),
239    ///     "B" => Value::test_int(42),
240    ///     "C" => Value::test_nothing(),
241    ///     "D" => Value::test_int(42),
242    /// };
243    /// let mut cased_rec: DynCasedRecord<&mut Record> = rec.cased_mut(Casing::Insensitive);
244    /// ```
245    ///
246    /// The following will fail to compile:
247    ///
248    /// ```compile_fail
249    /// # use nu_protocol::{record, record::{Record, DynCasedRecord}, Value, casing::Casing};
250    /// # let mut rec = record!{};
251    /// # let mut cased_rec: DynCasedRecord<&mut Record> = rec.cased_mut(Casing::Insensitive);
252    /// let a = cased_rec.get_mut("a");
253    /// let b = cased_rec.get_mut("b");
254    /// ```
255    ///
256    /// This is due to the fact `.get_mut()` receives `self`[^self] _by value_, which limits its use to
257    /// just once, unless we construct a new `DynCasedRecord`.
258    ///
259    /// [^self]: Receiving `&mut self` works, but has an undesirable effect on the return value's
260    /// lifetime. With `Self == &'wrapper mut DynCasedRecord<&'source mut Record>`, return value's
261    /// lifetime will be `'wrapper` rather than `'source`.
262    ///
263    /// We can create a new `DynCasedRecord<&mut Record>` from an existing one even though `&mut T` is
264    /// not [`Copy`]. This is accomplished with [reborrowing] which happens implicitly with native
265    /// references. Reborrowing also happens to be a tragically under documented feature of rust.
266    ///
267    /// Though there isn't a trait for it yet, it's possible and simple to implement, it just has
268    /// to be called explicitly:
269    ///
270    /// ```
271    /// # use nu_protocol::{record, record::{Record, DynCasedRecord}, Value, casing::Casing};
272    /// # let mut rec = record!{};
273    /// # let mut cased_rec: DynCasedRecord<&mut Record> = rec.cased_mut(Casing::Insensitive);
274    /// let a = cased_rec.reborrow_mut().get_mut("a");
275    /// let b = cased_rec.reborrow_mut().get_mut("b");
276    /// ```
277    ///
278    /// [reborrowing]: https://quinedot.github.io/rust-learning/st-reborrow.html
279    pub fn reborrow_mut(&mut self) -> DynCasedRecord<&mut Record> {
280        DynCasedRecord {
281            record: &mut *self.record,
282            casing: self.casing,
283        }
284    }
285
286    pub fn get_mut(self, col: impl AsRef<str>) -> Option<&'a mut Value> {
287        match self.casing {
288            Casing::Sensitive => self.record.case_sensitive().get_mut(col.as_ref()),
289            Casing::Insensitive => self.record.case_insensitive().get_mut(col.as_ref()),
290        }
291    }
292
293    pub fn remove(self, col: impl AsRef<str>) -> Option<Value> {
294        match self.casing {
295            Casing::Sensitive => self.record.case_sensitive().remove(col.as_ref()),
296            Casing::Insensitive => self.record.case_insensitive().remove(col.as_ref()),
297        }
298    }
299
300    /// Insert into the record, replacing preexisting value if found.
301    ///
302    /// Returns `Some(previous_value)` if found. Else `None`
303    pub fn insert<K>(self, col: K, val: Value) -> Option<Value>
304    where
305        K: AsRef<str> + Into<String>,
306    {
307        match self.casing {
308            Casing::Sensitive => self.record.case_sensitive().insert(col.as_ref(), val),
309            Casing::Insensitive => self.record.case_insensitive().insert(col.as_ref(), val),
310        }
311    }
312}
313
314impl Record {
315    pub fn new() -> Self {
316        Self::default()
317    }
318
319    pub fn with_capacity(capacity: usize) -> Self {
320        Self {
321            inner: Vec::with_capacity(capacity),
322        }
323    }
324
325    /// Returns an estimate of the memory size used by this Record in bytes
326    pub fn memory_size(&self) -> usize {
327        std::mem::size_of::<Self>()
328            + self
329                .inner
330                .iter()
331                .map(|(k, v)| k.capacity() + v.memory_size())
332                .sum::<usize>()
333    }
334
335    pub fn cased(&self, casing: Casing) -> DynCasedRecord<&Record> {
336        DynCasedRecord {
337            record: self,
338            casing,
339        }
340    }
341
342    pub fn cased_mut(&mut self, casing: Casing) -> DynCasedRecord<&mut Record> {
343        DynCasedRecord {
344            record: self,
345            casing,
346        }
347    }
348
349    /// Create a [`Record`] from a `Vec` of columns and a `Vec` of [`Value`]s
350    ///
351    /// Returns an error if `cols` and `vals` have different lengths.
352    ///
353    /// For perf reasons, this will not validate the rest of the record assumptions:
354    /// - unique keys
355    pub fn from_raw_cols_vals(
356        cols: Vec<String>,
357        vals: Vec<Value>,
358        input_span: Span,
359        creation_site_span: Span,
360    ) -> Result<Self, ShellError> {
361        if cols.len() == vals.len() {
362            let inner = cols.into_iter().zip(vals).collect();
363            Ok(Self { inner })
364        } else {
365            Err(ShellError::RecordColsValsMismatch {
366                bad_value: input_span,
367                creation_site: creation_site_span,
368            })
369        }
370    }
371
372    pub fn iter(&self) -> Iter<'_> {
373        self.into_iter()
374    }
375
376    pub fn iter_mut(&mut self) -> IterMut<'_> {
377        self.into_iter()
378    }
379
380    pub fn is_empty(&self) -> bool {
381        self.inner.is_empty()
382    }
383
384    pub fn len(&self) -> usize {
385        self.inner.len()
386    }
387
388    /// Naive push to the end of the datastructure.
389    ///
390    /// <div class="warning">
391    /// May duplicate data!
392    ///
393    /// Consider using [`CasedRecord::insert`] or [`DynCasedRecord::insert`] instead.
394    /// </div>
395    pub fn push(&mut self, col: impl Into<String>, val: Value) {
396        self.inner.push((col.into(), val));
397    }
398
399    pub fn get_index(&self, idx: usize) -> Option<(&String, &Value)> {
400        self.inner.get(idx).map(|(col, val): &(_, _)| (col, val))
401    }
402
403    pub fn get_index_mut(&mut self, idx: usize) -> Option<(&mut String, &mut Value)> {
404        self.inner.get_mut(idx).map(|(col, val)| (col, val))
405    }
406
407    /// Remove single value by index
408    fn remove_index(&mut self, index: usize) -> Value {
409        self.inner.remove(index).1
410    }
411
412    /// Remove elements in-place that do not satisfy `keep`
413    ///
414    /// ```rust
415    /// use nu_protocol::{record, Value};
416    ///
417    /// let mut rec = record!(
418    ///     "a" => Value::test_nothing(),
419    ///     "b" => Value::test_int(42),
420    ///     "c" => Value::test_nothing(),
421    ///     "d" => Value::test_int(42),
422    /// );
423    /// rec.retain(|_k, val| !val.is_nothing());
424    /// let mut iter_rec = rec.columns();
425    /// assert_eq!(iter_rec.next().map(String::as_str), Some("b"));
426    /// assert_eq!(iter_rec.next().map(String::as_str), Some("d"));
427    /// assert_eq!(iter_rec.next(), None);
428    /// ```
429    pub fn retain<F>(&mut self, mut keep: F)
430    where
431        F: FnMut(&str, &Value) -> bool,
432    {
433        self.retain_mut(|k, v| keep(k, v));
434    }
435
436    /// Remove elements in-place that do not satisfy `keep` while allowing mutation of the value.
437    ///
438    /// This can for example be used to recursively prune nested records.
439    ///
440    /// ```rust
441    /// use nu_protocol::{record, Record, Value};
442    ///
443    /// fn remove_foo_recursively(val: &mut Value) {
444    ///     if let Value::Record {val, ..} = val {
445    ///         val.to_mut().retain_mut(keep_non_foo);
446    ///     }
447    /// }
448    ///
449    /// fn keep_non_foo(k: &str, v: &mut Value) -> bool {
450    ///     if k == "foo" {
451    ///         return false;
452    ///     }
453    ///     remove_foo_recursively(v);
454    ///     true
455    /// }
456    ///
457    /// let mut test = Value::test_record(record!(
458    ///     "foo" => Value::test_nothing(),
459    ///     "bar" => Value::test_record(record!(
460    ///         "foo" => Value::test_nothing(),
461    ///         "baz" => Value::test_nothing(),
462    ///         ))
463    ///     ));
464    ///
465    /// remove_foo_recursively(&mut test);
466    /// let expected = Value::test_record(record!(
467    ///     "bar" => Value::test_record(record!(
468    ///         "baz" => Value::test_nothing(),
469    ///         ))
470    ///     ));
471    /// assert_eq!(test, expected);
472    /// ```
473    pub fn retain_mut<F>(&mut self, mut keep: F)
474    where
475        F: FnMut(&str, &mut Value) -> bool,
476    {
477        self.inner.retain_mut(|(col, val)| keep(col, val));
478    }
479
480    /// Truncate record to the first `len` elements.
481    ///
482    /// `len > self.len()` will be ignored
483    ///
484    /// ```rust
485    /// use nu_protocol::{record, Value};
486    ///
487    /// let mut rec = record!(
488    ///     "a" => Value::test_nothing(),
489    ///     "b" => Value::test_int(42),
490    ///     "c" => Value::test_nothing(),
491    ///     "d" => Value::test_int(42),
492    /// );
493    /// rec.truncate(42); // this is fine
494    /// assert_eq!(rec.columns().map(String::as_str).collect::<String>(), "abcd");
495    /// rec.truncate(2); // truncate
496    /// assert_eq!(rec.columns().map(String::as_str).collect::<String>(), "ab");
497    /// rec.truncate(0); // clear the record
498    /// assert_eq!(rec.len(), 0);
499    /// ```
500    pub fn truncate(&mut self, len: usize) {
501        self.inner.truncate(len);
502    }
503
504    pub fn truncate_front(&mut self, len: usize) {
505        if self.len() < len {
506            return;
507        }
508        let drop = self.len() - len;
509        self.inner.drain(..drop);
510    }
511
512    pub fn columns(&self) -> Columns<'_> {
513        Columns {
514            iter: self.inner.iter(),
515        }
516    }
517
518    pub fn into_columns(self) -> IntoColumns {
519        IntoColumns {
520            iter: self.inner.into_iter(),
521        }
522    }
523
524    pub fn values(&self) -> Values<'_> {
525        Values {
526            iter: self.inner.iter(),
527        }
528    }
529
530    pub fn into_values(self) -> IntoValues {
531        IntoValues {
532            iter: self.inner.into_iter(),
533        }
534    }
535
536    /// Obtain an iterator to remove elements in `range`
537    ///
538    /// Elements not consumed from the iterator will be dropped
539    ///
540    /// ```rust
541    /// use nu_protocol::{record, Value};
542    ///
543    /// let mut rec = record!(
544    ///     "a" => Value::test_nothing(),
545    ///     "b" => Value::test_int(42),
546    ///     "c" => Value::test_string("foo"),
547    /// );
548    /// {
549    ///     let mut drainer = rec.drain(1..);
550    ///     assert_eq!(drainer.next(), Some(("b".into(), Value::test_int(42))));
551    ///     // Dropping the `Drain`
552    /// }
553    /// let mut rec_iter = rec.into_iter();
554    /// assert_eq!(rec_iter.next(), Some(("a".into(), Value::test_nothing())));
555    /// assert_eq!(rec_iter.next(), None);
556    /// ```
557    pub fn drain<R>(&mut self, range: R) -> Drain<'_>
558    where
559        R: RangeBounds<usize> + Clone,
560    {
561        Drain {
562            iter: self.inner.drain(range),
563        }
564    }
565
566    /// Sort the record by its columns.
567    ///
568    /// ```rust
569    /// use nu_protocol::{record, Value};
570    ///
571    /// let mut rec = record!(
572    ///     "c" => Value::test_string("foo"),
573    ///     "b" => Value::test_int(42),
574    ///     "a" => Value::test_nothing(),
575    /// );
576    ///
577    /// rec.sort_cols();
578    ///
579    /// assert_eq!(
580    ///     Value::test_record(rec),
581    ///     Value::test_record(record!(
582    ///         "a" => Value::test_nothing(),
583    ///         "b" => Value::test_int(42),
584    ///         "c" => Value::test_string("foo"),
585    ///     ))
586    /// );
587    /// ```
588    pub fn sort_cols(&mut self) {
589        self.inner.sort_by(|(k1, _), (k2, _)| k1.cmp(k2))
590    }
591}
592
593impl CompareTypes<CollectionColumns<Type>> for Record {
594    fn compare_types(&self, other: &CollectionColumns<Type>) -> Option<TypeRelation> {
595        match (self.is_empty(), other.is_empty()) {
596            (true, true) => return Some(TypeRelation::Equal),
597            (true, false) => return Some(TypeRelation::Supertype),
598            (false, true) => return Some(TypeRelation::Subtype),
599            (false, false) => {}
600        }
601
602        let (flipped, eq) = match self.len().cmp(&other.len()) {
603            std::cmp::Ordering::Less => (false, false),
604            std::cmp::Ordering::Equal => (false, true),
605            std::cmp::Ordering::Greater => (true, false),
606        };
607
608        let start = match eq {
609            true => TypeRelation::Equal,
610            false => TypeRelation::Supertype,
611        };
612
613        if flipped {
614            let lhs = other;
615            let rhs = self;
616            lhs.iter()
617                .map(|(lhs_key, lhs_ty)| {
618                    match rhs.get(lhs_key) {
619                        Some(rhs_val) => {
620                            if CompareTypes::<Type>::is_any(lhs_ty) || rhs_val.is_any() {
621                                // Not really" equal", just used to continue without affecting the outcome.
622                                Some(TypeRelation::Equal)
623                            } else {
624                                // `CompareTypes<Value> for Type` is not implemented
625                                // lhs_ty.compare_types(rhs_val)
626                                rhs_val.compare_types(lhs_ty).map(TypeRelation::reverse)
627                            }
628                        }
629                        None => None,
630                    }
631                })
632                .try_fold(start, |acc, e| acc.combine(e?))
633                .map(TypeRelation::reverse)
634        } else {
635            let lhs = self;
636            let rhs = other;
637            lhs.iter()
638                .map(|(lhs_key, lhs_val)| {
639                    match rhs.get(lhs_key) {
640                        Some(rhs_ty) => {
641                            if lhs_val.is_any() || CompareTypes::<Type>::is_any(rhs_ty) {
642                                // Not really" equal", just used to continue without affecting the outcome.
643                                Some(TypeRelation::Equal)
644                            } else {
645                                lhs_val.compare_types(rhs_ty)
646                            }
647                        }
648                        None => None,
649                    }
650                })
651                .try_fold(start, |acc, e| acc.combine(e?))
652        }
653    }
654}
655
656impl Serialize for Record {
657    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
658    where
659        S: serde::Serializer,
660    {
661        let mut map = serializer.serialize_map(Some(self.len()))?;
662        for (k, v) in self {
663            map.serialize_entry(k, v)?;
664        }
665        map.end()
666    }
667}
668
669impl<'de> Deserialize<'de> for Record {
670    /// Special deserialization implementation that turns a map-pattern into a [`Record`]
671    ///
672    /// Denies duplicate keys
673    ///
674    /// ```rust
675    /// use serde_json::{from_str, Result};
676    /// use nu_protocol::{Record, Value, record};
677    ///
678    /// // A `Record` in json is a Record with a packed `Value`
679    /// // The `Value` record has a single key indicating its type and the inner record describing
680    /// // its representation of value and the associated `Span`
681    /// let ok = r#"{"a": {"Int": {"val": 42, "span": {"start": 0, "end": 0}}},
682    ///              "b": {"Int": {"val": 37, "span": {"start": 0, "end": 0}}}}"#;
683    /// let ok_rec: Record = from_str(ok).unwrap();
684    /// assert_eq!(Value::test_record(ok_rec),
685    ///            Value::test_record(record!{"a" => Value::test_int(42),
686    ///                                       "b" => Value::test_int(37)}));
687    /// // A repeated key will lead to a deserialization error
688    /// let bad = r#"{"a": {"Int": {"val": 42, "span": {"start": 0, "end": 0}}},
689    ///               "a": {"Int": {"val": 37, "span": {"start": 0, "end": 0}}}}"#;
690    /// let bad_rec: Result<Record> = from_str(bad);
691    /// assert!(bad_rec.is_err());
692    /// ```
693    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
694    where
695        D: serde::Deserializer<'de>,
696    {
697        deserializer.deserialize_map(RecordVisitor)
698    }
699}
700
701struct RecordVisitor;
702
703impl<'de> Visitor<'de> for RecordVisitor {
704    type Value = Record;
705
706    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
707        formatter.write_str("a nushell `Record` mapping string keys/columns to nushell `Value`")
708    }
709
710    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
711    where
712        A: serde::de::MapAccess<'de>,
713    {
714        let mut record = Record::with_capacity(map.size_hint().unwrap_or(0));
715
716        while let Some((key, value)) = map.next_entry::<String, Value>()? {
717            if record.insert(key, value).is_some() {
718                return Err(serde::de::Error::custom(
719                    "invalid entry, duplicate keys are not allowed for `Record`",
720                ));
721            }
722        }
723
724        Ok(record)
725    }
726}
727
728impl FromIterator<(String, Value)> for Record {
729    fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
730        // TODO: should this check for duplicate keys/columns?
731        Self {
732            inner: iter.into_iter().collect(),
733        }
734    }
735}
736
737impl Extend<(String, Value)> for Record {
738    fn extend<T: IntoIterator<Item = (String, Value)>>(&mut self, iter: T) {
739        for (k, v) in iter {
740            // TODO: should this .insert with a check?
741            self.push(k, v)
742        }
743    }
744}
745
746pub struct IntoIter {
747    iter: std::vec::IntoIter<(String, Value)>,
748}
749
750impl Iterator for IntoIter {
751    type Item = (String, Value);
752
753    fn next(&mut self) -> Option<Self::Item> {
754        self.iter.next()
755    }
756
757    fn size_hint(&self) -> (usize, Option<usize>) {
758        self.iter.size_hint()
759    }
760}
761
762impl DoubleEndedIterator for IntoIter {
763    fn next_back(&mut self) -> Option<Self::Item> {
764        self.iter.next_back()
765    }
766}
767
768impl ExactSizeIterator for IntoIter {
769    fn len(&self) -> usize {
770        self.iter.len()
771    }
772}
773
774impl FusedIterator for IntoIter {}
775
776impl IntoIterator for Record {
777    type Item = (String, Value);
778
779    type IntoIter = IntoIter;
780
781    fn into_iter(self) -> Self::IntoIter {
782        IntoIter {
783            iter: self.inner.into_iter(),
784        }
785    }
786}
787
788pub struct Iter<'a> {
789    iter: std::slice::Iter<'a, (String, Value)>,
790}
791
792impl<'a> Iterator for Iter<'a> {
793    type Item = (&'a String, &'a Value);
794
795    fn next(&mut self) -> Option<Self::Item> {
796        self.iter.next().map(|(col, val): &(_, _)| (col, val))
797    }
798
799    fn size_hint(&self) -> (usize, Option<usize>) {
800        self.iter.size_hint()
801    }
802}
803
804impl DoubleEndedIterator for Iter<'_> {
805    fn next_back(&mut self) -> Option<Self::Item> {
806        self.iter.next_back().map(|(col, val): &(_, _)| (col, val))
807    }
808}
809
810impl ExactSizeIterator for Iter<'_> {
811    fn len(&self) -> usize {
812        self.iter.len()
813    }
814}
815
816impl FusedIterator for Iter<'_> {}
817
818impl<'a> IntoIterator for &'a Record {
819    type Item = (&'a String, &'a Value);
820
821    type IntoIter = Iter<'a>;
822
823    fn into_iter(self) -> Self::IntoIter {
824        Iter {
825            iter: self.inner.iter(),
826        }
827    }
828}
829
830pub struct IterMut<'a> {
831    iter: std::slice::IterMut<'a, (String, Value)>,
832}
833
834impl<'a> Iterator for IterMut<'a> {
835    type Item = (&'a String, &'a mut Value);
836
837    fn next(&mut self) -> Option<Self::Item> {
838        self.iter.next().map(|(col, val)| (&*col, val))
839    }
840
841    fn size_hint(&self) -> (usize, Option<usize>) {
842        self.iter.size_hint()
843    }
844}
845
846impl DoubleEndedIterator for IterMut<'_> {
847    fn next_back(&mut self) -> Option<Self::Item> {
848        self.iter.next_back().map(|(col, val)| (&*col, val))
849    }
850}
851
852impl ExactSizeIterator for IterMut<'_> {
853    fn len(&self) -> usize {
854        self.iter.len()
855    }
856}
857
858impl FusedIterator for IterMut<'_> {}
859
860impl<'a> IntoIterator for &'a mut Record {
861    type Item = (&'a String, &'a mut Value);
862
863    type IntoIter = IterMut<'a>;
864
865    fn into_iter(self) -> Self::IntoIter {
866        IterMut {
867            iter: self.inner.iter_mut(),
868        }
869    }
870}
871
872pub struct Columns<'a> {
873    iter: std::slice::Iter<'a, (String, Value)>,
874}
875
876impl<'a> Iterator for Columns<'a> {
877    type Item = &'a String;
878
879    fn next(&mut self) -> Option<Self::Item> {
880        self.iter.next().map(|(col, _)| col)
881    }
882
883    fn size_hint(&self) -> (usize, Option<usize>) {
884        self.iter.size_hint()
885    }
886}
887
888impl DoubleEndedIterator for Columns<'_> {
889    fn next_back(&mut self) -> Option<Self::Item> {
890        self.iter.next_back().map(|(col, _)| col)
891    }
892}
893
894impl ExactSizeIterator for Columns<'_> {
895    fn len(&self) -> usize {
896        self.iter.len()
897    }
898}
899
900impl FusedIterator for Columns<'_> {}
901
902pub struct IntoColumns {
903    iter: std::vec::IntoIter<(String, Value)>,
904}
905
906impl Iterator for IntoColumns {
907    type Item = String;
908
909    fn next(&mut self) -> Option<Self::Item> {
910        self.iter.next().map(|(col, _)| col)
911    }
912
913    fn size_hint(&self) -> (usize, Option<usize>) {
914        self.iter.size_hint()
915    }
916}
917
918impl DoubleEndedIterator for IntoColumns {
919    fn next_back(&mut self) -> Option<Self::Item> {
920        self.iter.next_back().map(|(col, _)| col)
921    }
922}
923
924impl ExactSizeIterator for IntoColumns {
925    fn len(&self) -> usize {
926        self.iter.len()
927    }
928}
929
930impl FusedIterator for IntoColumns {}
931
932pub struct Values<'a> {
933    iter: std::slice::Iter<'a, (String, Value)>,
934}
935
936impl<'a> Iterator for Values<'a> {
937    type Item = &'a Value;
938
939    fn next(&mut self) -> Option<Self::Item> {
940        self.iter.next().map(|(_, val)| val)
941    }
942
943    fn size_hint(&self) -> (usize, Option<usize>) {
944        self.iter.size_hint()
945    }
946}
947
948impl DoubleEndedIterator for Values<'_> {
949    fn next_back(&mut self) -> Option<Self::Item> {
950        self.iter.next_back().map(|(_, val)| val)
951    }
952}
953
954impl ExactSizeIterator for Values<'_> {
955    fn len(&self) -> usize {
956        self.iter.len()
957    }
958}
959
960impl FusedIterator for Values<'_> {}
961
962pub struct IntoValues {
963    iter: std::vec::IntoIter<(String, Value)>,
964}
965
966impl Iterator for IntoValues {
967    type Item = Value;
968
969    fn next(&mut self) -> Option<Self::Item> {
970        self.iter.next().map(|(_, val)| val)
971    }
972
973    fn size_hint(&self) -> (usize, Option<usize>) {
974        self.iter.size_hint()
975    }
976}
977
978impl DoubleEndedIterator for IntoValues {
979    fn next_back(&mut self) -> Option<Self::Item> {
980        self.iter.next_back().map(|(_, val)| val)
981    }
982}
983
984impl ExactSizeIterator for IntoValues {
985    fn len(&self) -> usize {
986        self.iter.len()
987    }
988}
989
990impl FusedIterator for IntoValues {}
991
992pub struct Drain<'a> {
993    iter: std::vec::Drain<'a, (String, Value)>,
994}
995
996impl Iterator for Drain<'_> {
997    type Item = (String, Value);
998
999    fn next(&mut self) -> Option<Self::Item> {
1000        self.iter.next()
1001    }
1002
1003    fn size_hint(&self) -> (usize, Option<usize>) {
1004        self.iter.size_hint()
1005    }
1006}
1007
1008impl DoubleEndedIterator for Drain<'_> {
1009    fn next_back(&mut self) -> Option<Self::Item> {
1010        self.iter.next_back()
1011    }
1012}
1013
1014impl ExactSizeIterator for Drain<'_> {
1015    fn len(&self) -> usize {
1016        self.iter.len()
1017    }
1018}
1019
1020impl FusedIterator for Drain<'_> {}