1use 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
30impl Hash for Record {
39 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#[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 unsafe { &*(record as *const Record as *const Self) }
72 }
73
74 #[inline]
75 const fn from_record_mut(record: &mut Record) -> &mut Self {
76 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 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 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
186pub 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 pub fn reborrow(&self) -> DynCasedRecord<&Record> {
225 DynCasedRecord {
226 record: &*self.record,
227 casing: self.casing,
228 }
229 }
230
231 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 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 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 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 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 fn remove_index(&mut self, index: usize) -> Value {
409 self.inner.remove(index).1
410 }
411
412 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 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 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 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 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 Some(TypeRelation::Equal)
623 } else {
624 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 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 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 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 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<'_> {}