1mod custom_value;
2mod duration;
3mod filesize;
4mod from_value;
5mod glob;
6mod into_value;
7mod range;
8#[cfg(test)]
9mod test_derive;
10
11#[doc(hidden)]
12pub mod macros;
13pub mod record;
14use bstr::BStr;
15pub use custom_value::CustomValue;
16pub use duration::*;
17pub use filesize::*;
18pub use from_value::FromValue;
19pub use glob::*;
20pub use into_value::{IntoValue, TryIntoValue};
21pub use nu_utils::MultiLife;
22pub use range::{FloatRange, IntRange, ParseRangeError, Range};
23pub use record::Record;
24
25use crate::{
26 BlockId, CompareTypes, Config, ShellError, Signals, Span, Type, TypeRelation,
27 ast::{Bits, Boolean, CellPath, Comparison, Math, Operator, PathMember},
28 did_you_mean,
29 engine::{Closure, EngineState},
30};
31use chrono::{DateTime, Datelike, Duration, FixedOffset, Local, Locale, TimeZone, Utc};
32use chrono_humanize::HumanTime;
33use nu_utils::{ObviousFloat, SharedCow, contains_emoji, get_locale_from_env_vars};
34pub use semver::Version as SemVerVersion;
35use serde::{Deserialize, Serialize};
36use std::{
37 borrow::Cow,
38 cmp::Ordering,
39 fmt::{self, Debug, Display, Write},
40 hash::{Hash, Hasher},
41 ops::{Bound, ControlFlow},
42 path::PathBuf,
43};
44
45#[derive(Serialize, Deserialize)]
80pub enum Value {
81 #[non_exhaustive]
82 Bool {
83 val: bool,
84 #[serde(rename = "span")]
87 internal_span: Span,
88 },
89 #[non_exhaustive]
90 Int {
91 val: i64,
92 #[serde(rename = "span")]
95 internal_span: Span,
96 },
97 #[non_exhaustive]
98 Float {
99 val: f64,
100 #[serde(rename = "span")]
103 internal_span: Span,
104 },
105 #[non_exhaustive]
106 String {
107 val: String,
108 #[serde(rename = "span")]
111 internal_span: Span,
112 },
113 #[non_exhaustive]
114 Glob {
115 val: String,
116 no_expand: bool,
117 #[serde(rename = "span")]
120 internal_span: Span,
121 },
122 #[non_exhaustive]
123 Filesize {
124 val: Filesize,
125 #[serde(rename = "span")]
128 internal_span: Span,
129 },
130 #[non_exhaustive]
131 Duration {
132 val: i64,
134 #[serde(rename = "span")]
137 internal_span: Span,
138 },
139 #[non_exhaustive]
140 Date {
141 val: DateTime<FixedOffset>,
142 #[serde(rename = "span")]
145 internal_span: Span,
146 },
147 #[non_exhaustive]
148 Range {
149 val: Box<Range>,
150 #[serde(skip)]
151 signals: Option<Signals>,
152 #[serde(rename = "span")]
155 internal_span: Span,
156 },
157 #[non_exhaustive]
158 Record {
159 val: SharedCow<Record>,
160 #[serde(rename = "span")]
163 internal_span: Span,
164 },
165 #[non_exhaustive]
166 List {
167 vals: SharedCow<Vec<Value>>,
168 #[serde(skip)]
169 signals: Option<Signals>,
170 #[serde(rename = "span")]
173 internal_span: Span,
174 },
175 #[non_exhaustive]
176 Closure {
177 val: Box<Closure>,
178 #[serde(rename = "span")]
181 internal_span: Span,
182 },
183 #[non_exhaustive]
184 Error {
185 error: Box<ShellError>,
186 #[serde(rename = "span")]
189 internal_span: Span,
190 },
191 #[non_exhaustive]
192 Binary {
193 val: SharedCow<Vec<u8>>,
194 #[serde(rename = "span")]
197 internal_span: Span,
198 },
199 #[non_exhaustive]
200 CellPath {
201 val: CellPath,
202 #[serde(rename = "span")]
205 internal_span: Span,
206 },
207 #[non_exhaustive]
208 Custom {
209 val: Box<dyn CustomValue>,
210 #[serde(rename = "span")]
213 internal_span: Span,
214 },
215 #[non_exhaustive]
216 Nothing {
217 #[serde(rename = "span")]
220 internal_span: Span,
221 },
222}
223
224fn wrap_tuple(name: &str, val: impl Debug) -> impl Debug {
225 fmt::from_fn(move |f| {
226 write!(f, "{name}(")?;
227 val.fmt(f)?;
228 write!(f, ")")
229 })
230}
231
232fn display_as_debug(val: impl Display) -> impl Debug {
233 fmt::from_fn(move |f| val.fmt(f))
234}
235
236impl Debug for Value {
237 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
238 if f.sign_minus() {
239 return match self {
240 Self::Bool { val, internal_span } => f
241 .debug_struct("Bool")
242 .field("val", val)
243 .field("internal_span", internal_span)
244 .finish(),
245 Self::Int { val, internal_span } => f
246 .debug_struct("Int")
247 .field("val", val)
248 .field("internal_span", internal_span)
249 .finish(),
250 Self::Float { val, internal_span } => f
251 .debug_struct("Float")
252 .field("val", val)
253 .field("internal_span", internal_span)
254 .finish(),
255 Self::String { val, internal_span } => f
256 .debug_struct("String")
257 .field("val", val)
258 .field("internal_span", internal_span)
259 .finish(),
260 Self::Glob {
261 val,
262 no_expand,
263 internal_span,
264 } => f
265 .debug_struct("Glob")
266 .field("val", val)
267 .field("no_expand", no_expand)
268 .field("internal_span", internal_span)
269 .finish(),
270 Self::Filesize { val, internal_span } => f
271 .debug_struct("Filesize")
272 .field("val", val)
273 .field("internal_span", internal_span)
274 .finish(),
275 Self::Duration { val, internal_span } => f
276 .debug_struct("Duration")
277 .field("val", val)
278 .field("internal_span", internal_span)
279 .finish(),
280 Self::Date { val, internal_span } => f
281 .debug_struct("Date")
282 .field("val", val)
283 .field("internal_span", internal_span)
284 .finish(),
285 Self::Range {
286 val,
287 signals,
288 internal_span,
289 } => f
290 .debug_struct("Range")
291 .field("val", val)
292 .field("signals", signals)
293 .field("internal_span", internal_span)
294 .finish(),
295 Self::Record { val, internal_span } => f
296 .debug_struct("Record")
297 .field("val", val)
298 .field("internal_span", internal_span)
299 .finish(),
300 Self::List {
301 vals,
302 signals,
303 internal_span,
304 } => f
305 .debug_struct("List")
306 .field("vals", vals)
307 .field("signals", signals)
308 .field("internal_span", internal_span)
309 .finish(),
310 Self::Closure { val, internal_span } => f
311 .debug_struct("Closure")
312 .field("val", val)
313 .field("internal_span", internal_span)
314 .finish(),
315 Self::Error {
316 error,
317 internal_span,
318 } => f
319 .debug_struct("Error")
320 .field("error", error)
321 .field("internal_span", internal_span)
322 .finish(),
323 Self::Binary { val, internal_span } => f
324 .debug_struct("Binary")
325 .field("val", val)
326 .field("internal_span", internal_span)
327 .finish(),
328 Self::CellPath { val, internal_span } => f
329 .debug_struct("CellPath")
330 .field("val", val)
331 .field("internal_span", internal_span)
332 .finish(),
333 Self::Custom { val, internal_span } => f
334 .debug_struct("Custom")
335 .field("val", val)
336 .field("internal_span", internal_span)
337 .finish(),
338 Self::Nothing { internal_span } => f
339 .debug_struct("Nothing")
340 .field("internal_span", internal_span)
341 .finish(),
342 };
343 };
344
345 match self {
346 Value::Bool { val, .. } => wrap_tuple("Bool", val).fmt(f),
347 Value::Int { val, .. } => wrap_tuple("Int", val).fmt(f),
348 Value::Float { val, .. } => wrap_tuple("Float", val).fmt(f),
349 Value::String { val, .. } => wrap_tuple("String", val).fmt(f),
350 Value::Glob { val, no_expand, .. } => wrap_tuple(
351 "Glob",
352 fmt::from_fn(|f| {
353 Debug::fmt(val, f)?;
354 if *no_expand {
355 write!(f, "!")?;
356 }
357 Ok(())
358 }),
359 )
360 .fmt(f),
361 Value::Filesize { val, .. } => wrap_tuple("Filesize", display_as_debug(val)).fmt(f),
362 Value::Duration { val, .. } => wrap_tuple(
363 "Duration",
364 display_as_debug(humantime::Duration::from(std::time::Duration::from_nanos(
365 *val as u64,
366 ))),
367 )
368 .fmt(f),
369 Value::Date { val, .. } => wrap_tuple("Date", val).fmt(f),
370 Value::Range { val, .. } => wrap_tuple("Range", display_as_debug(val)).fmt(f),
371 Value::Record { val, .. } => wrap_tuple("Record", val).fmt(f),
372 Value::List { vals, .. } => wrap_tuple("List", vals).fmt(f),
373 Value::Closure { val, .. } => wrap_tuple("Closure", val.compact_debug()).fmt(f),
374 Value::Error { error, .. } => wrap_tuple("Error", error).fmt(f),
375 Value::Binary { val, .. } => wrap_tuple("Binary", BStr::new(val.as_slice())).fmt(f),
376 Value::CellPath { val, .. } => wrap_tuple("CellPath", display_as_debug(val)).fmt(f),
377 Value::Custom { val, .. } => wrap_tuple("Custom", val).fmt(f),
378 Value::Nothing { .. } => write!(f, "Nothing"),
379 }
380 }
381}
382
383const _: () = assert!(std::mem::size_of::<Value>() <= 56);
387
388impl Clone for Value {
389 fn clone(&self) -> Self {
390 match self {
391 Value::Bool { val, internal_span } => Value::bool(*val, *internal_span),
392 Value::Int { val, internal_span } => Value::int(*val, *internal_span),
393 Value::Filesize { val, internal_span } => Value::Filesize {
394 val: *val,
395 internal_span: *internal_span,
396 },
397 Value::Duration { val, internal_span } => Value::Duration {
398 val: *val,
399 internal_span: *internal_span,
400 },
401 Value::Date { val, internal_span } => Value::Date {
402 val: *val,
403 internal_span: *internal_span,
404 },
405 Value::Range {
406 val,
407 signals,
408 internal_span,
409 } => Value::Range {
410 val: val.clone(),
411 signals: signals.clone(),
412 internal_span: *internal_span,
413 },
414 Value::Float { val, internal_span } => Value::float(*val, *internal_span),
415 Value::String { val, internal_span } => Value::String {
416 val: val.clone(),
417 internal_span: *internal_span,
418 },
419 Value::Glob {
420 val,
421 no_expand: quoted,
422 internal_span,
423 } => Value::Glob {
424 val: val.clone(),
425 no_expand: *quoted,
426 internal_span: *internal_span,
427 },
428 Value::Record { val, internal_span } => Value::Record {
429 val: val.clone(),
430 internal_span: *internal_span,
431 },
432 Value::List {
433 vals,
434 signals,
435 internal_span,
436 } => Value::List {
437 vals: vals.clone(),
438 signals: signals.clone(),
439 internal_span: *internal_span,
440 },
441 Value::Closure { val, internal_span } => Value::Closure {
442 val: val.clone(),
443 internal_span: *internal_span,
444 },
445 Value::Nothing { internal_span } => Value::Nothing {
446 internal_span: *internal_span,
447 },
448 Value::Error {
449 error,
450 internal_span,
451 } => Value::Error {
452 error: error.clone(),
453 internal_span: *internal_span,
454 },
455 Value::Binary { val, internal_span } => Value::Binary {
456 val: val.clone(),
457 internal_span: *internal_span,
458 },
459 Value::CellPath { val, internal_span } => Value::CellPath {
460 val: val.clone(),
461 internal_span: *internal_span,
462 },
463 Value::Custom { val, internal_span } => val.clone_value(*internal_span),
464 }
465 }
466}
467
468pub enum CellPathMutation {
470 Upsert,
471 Update,
472 Insert { head_span: Span },
473 Remove { member: PathMember },
474}
475
476impl Value {
477 fn cant_convert_to<T>(&self, typ: &str) -> Result<T, ShellError> {
478 Err(ShellError::CantConvert {
479 to_type: typ.into(),
480 from_type: self.get_type().to_string(),
481 span: self.span(),
482 help: None,
483 })
484 }
485
486 pub fn as_bool(&self) -> Result<bool, ShellError> {
488 if let Value::Bool { val, .. } = self {
489 Ok(*val)
490 } else {
491 self.cant_convert_to("boolean")
492 }
493 }
494
495 pub fn as_int(&self) -> Result<i64, ShellError> {
497 if let Value::Int { val, .. } = self {
498 Ok(*val)
499 } else {
500 self.cant_convert_to("int")
501 }
502 }
503
504 pub fn as_float(&self) -> Result<f64, ShellError> {
506 if let Value::Float { val, .. } = self {
507 Ok(*val)
508 } else {
509 self.cant_convert_to("float")
510 }
511 }
512
513 pub fn coerce_float(&self) -> Result<f64, ShellError> {
529 match self {
530 Value::Float { val, .. } => Ok(*val),
531 Value::Int { val, .. } => Ok(*val as f64),
532 val => val.cant_convert_to("float"),
533 }
534 }
535
536 pub fn as_filesize(&self) -> Result<Filesize, ShellError> {
538 if let Value::Filesize { val, .. } = self {
539 Ok(*val)
540 } else {
541 self.cant_convert_to("filesize")
542 }
543 }
544
545 pub fn as_duration(&self) -> Result<i64, ShellError> {
547 if let Value::Duration { val, .. } = self {
548 Ok(*val)
549 } else {
550 self.cant_convert_to("duration")
551 }
552 }
553
554 pub fn as_date(&self) -> Result<DateTime<FixedOffset>, ShellError> {
556 if let Value::Date { val, .. } = self {
557 Ok(*val)
558 } else {
559 self.cant_convert_to("datetime")
560 }
561 }
562
563 pub fn as_range(&self) -> Result<Range, ShellError> {
565 if let Value::Range { val, .. } = self {
566 Ok(**val)
567 } else {
568 self.cant_convert_to("range")
569 }
570 }
571
572 pub fn into_range(self) -> Result<Range, ShellError> {
574 if let Value::Range { val, .. } = self {
575 Ok(*val)
576 } else {
577 self.cant_convert_to("range")
578 }
579 }
580
581 pub fn as_str(&self) -> Result<&str, ShellError> {
583 if let Value::String { val, .. } = self {
584 Ok(val)
585 } else {
586 self.cant_convert_to("string")
587 }
588 }
589
590 pub fn into_string(self) -> Result<String, ShellError> {
592 if let Value::String { val, .. } = self {
593 Ok(val)
594 } else {
595 self.cant_convert_to("string")
596 }
597 }
598
599 pub fn coerce_str(&self) -> Result<Cow<'_, str>, ShellError> {
629 match self {
630 Value::Bool { val, .. } => Ok(Cow::Owned(val.to_string())),
631 Value::Int { val, .. } => Ok(Cow::Owned(val.to_string())),
632 Value::Float { val, .. } => Ok(Cow::Owned(val.to_string())),
633 Value::String { val, .. } => Ok(Cow::Borrowed(val)),
634 Value::Glob { val, .. } => Ok(Cow::Borrowed(val)),
635 Value::Binary { val, .. } => match std::str::from_utf8(val) {
636 Ok(s) => Ok(Cow::Borrowed(s)),
637 Err(_) => self.cant_convert_to("string"),
638 },
639 Value::Date { val, .. } => Ok(Cow::Owned(
640 val.to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
641 )),
642 val => val.cant_convert_to("string"),
643 }
644 }
645
646 pub fn coerce_string(&self) -> Result<String, ShellError> {
685 self.coerce_str().map(Cow::into_owned)
686 }
687
688 pub fn coerce_into_string(self) -> Result<String, ShellError> {
718 let span = self.span();
719 match self {
720 Value::Bool { val, .. } => Ok(val.to_string()),
721 Value::Int { val, .. } => Ok(val.to_string()),
722 Value::Float { val, .. } => Ok(val.to_string()),
723 Value::String { val, .. } => Ok(val),
724 Value::Glob { val, .. } => Ok(val),
725 Value::Binary { val, .. } => match String::from_utf8(val.into_owned()) {
726 Ok(s) => Ok(s),
727 Err(err) => Value::binary(err.into_bytes(), span).cant_convert_to("string"),
728 },
729 Value::Date { val, .. } => Ok(val.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)),
730 val => val.cant_convert_to("string"),
731 }
732 }
733
734 pub fn as_char(&self) -> Result<char, ShellError> {
736 let span = self.span();
737 if let Value::String { val, .. } = self {
738 let mut chars = val.chars();
739 match (chars.next(), chars.next()) {
740 (Some(c), None) => Ok(c),
741 _ => Err(ShellError::MissingParameter {
742 param_name: "single character separator".into(),
743 span,
744 }),
745 }
746 } else {
747 self.cant_convert_to("char")
748 }
749 }
750
751 pub fn to_path(&self) -> Result<PathBuf, ShellError> {
753 if let Value::String { val, .. } = self {
754 Ok(PathBuf::from(val))
755 } else {
756 self.cant_convert_to("path")
757 }
758 }
759
760 pub fn as_record(&self) -> Result<&Record, ShellError> {
762 if let Value::Record { val, .. } = self {
763 Ok(val)
764 } else {
765 self.cant_convert_to("record")
766 }
767 }
768
769 pub fn into_record(self) -> Result<Record, ShellError> {
771 if let Value::Record { val, .. } = self {
772 Ok(val.into_owned())
773 } else {
774 self.cant_convert_to("record")
775 }
776 }
777
778 pub fn as_list(&self) -> Result<&[Value], ShellError> {
780 if let Value::List { vals, .. } = self {
781 Ok(vals)
782 } else {
783 self.cant_convert_to("list")
784 }
785 }
786
787 pub fn into_list(self) -> Result<Vec<Value>, ShellError> {
789 if let Value::List { vals, .. } = self {
790 Ok(vals.into_owned())
791 } else {
792 self.cant_convert_to("list")
793 }
794 }
795
796 pub fn as_closure(&self) -> Result<&Closure, ShellError> {
798 if let Value::Closure { val, .. } = self {
799 Ok(val)
800 } else {
801 self.cant_convert_to("closure")
802 }
803 }
804
805 pub fn into_closure(self) -> Result<Closure, ShellError> {
807 if let Value::Closure { val, .. } = self {
808 Ok(*val)
809 } else {
810 self.cant_convert_to("closure")
811 }
812 }
813
814 pub fn as_binary(&self) -> Result<&[u8], ShellError> {
816 if let Value::Binary { val, .. } = self {
817 Ok(val)
818 } else {
819 self.cant_convert_to("binary")
820 }
821 }
822
823 pub fn into_binary(self) -> Result<Vec<u8>, ShellError> {
825 if let Value::Binary { val, .. } = self {
826 Ok(val.into_owned())
827 } else {
828 self.cant_convert_to("binary")
829 }
830 }
831
832 pub fn coerce_binary(&self) -> Result<&[u8], ShellError> {
851 match self {
852 Value::Binary { val, .. } => Ok(val),
853 Value::String { val, .. } => Ok(val.as_bytes()),
854 val => val.cant_convert_to("binary"),
855 }
856 }
857
858 pub fn coerce_into_binary(self) -> Result<Vec<u8>, ShellError> {
874 match self {
875 Value::Binary { val, .. } => Ok(val.into_owned()),
876 Value::String { val, .. } => Ok(val.into_bytes()),
877 val => val.cant_convert_to("binary"),
878 }
879 }
880
881 pub fn as_cell_path(&self) -> Result<&CellPath, ShellError> {
883 if let Value::CellPath { val, .. } = self {
884 Ok(val)
885 } else {
886 self.cant_convert_to("cell path")
887 }
888 }
889
890 pub fn into_cell_path(self) -> Result<CellPath, ShellError> {
892 if let Value::CellPath { val, .. } = self {
893 Ok(val)
894 } else {
895 self.cant_convert_to("cell path")
896 }
897 }
898
899 pub fn coerce_bool(&self) -> Result<bool, ShellError> {
915 match self {
916 Value::Bool { val: false, .. } | Value::Int { val: 0, .. } | Value::Nothing { .. } => {
917 Ok(false)
918 }
919 Value::Float { val, .. } if val <= &f64::EPSILON => Ok(false),
920 Value::String { val, .. } => match val.trim().to_ascii_lowercase().as_str() {
921 "" | "0" | "false" => Ok(false),
922 _ => Ok(true),
923 },
924 Value::Bool { .. } | Value::Int { .. } | Value::Float { .. } => Ok(true),
925 _ => self.cant_convert_to("bool"),
926 }
927 }
928
929 pub fn as_custom_value(&self) -> Result<&dyn CustomValue, ShellError> {
931 if let Value::Custom { val, .. } = self {
932 Ok(val.as_ref())
933 } else {
934 self.cant_convert_to("custom value")
935 }
936 }
937
938 pub fn into_custom_value(self) -> Result<Box<dyn CustomValue>, ShellError> {
940 if let Value::Custom { val, .. } = self {
941 Ok(val)
942 } else {
943 self.cant_convert_to("custom value")
944 }
945 }
946
947 pub fn span(&self) -> Span {
949 match self {
950 Value::Bool { internal_span, .. }
951 | Value::Int { internal_span, .. }
952 | Value::Float { internal_span, .. }
953 | Value::Filesize { internal_span, .. }
954 | Value::Duration { internal_span, .. }
955 | Value::Date { internal_span, .. }
956 | Value::Range { internal_span, .. }
957 | Value::String { internal_span, .. }
958 | Value::Glob { internal_span, .. }
959 | Value::Record { internal_span, .. }
960 | Value::List { internal_span, .. }
961 | Value::Closure { internal_span, .. }
962 | Value::Nothing { internal_span, .. }
963 | Value::Binary { internal_span, .. }
964 | Value::CellPath { internal_span, .. }
965 | Value::Custom { internal_span, .. }
966 | Value::Error { internal_span, .. } => *internal_span,
967 }
968 }
969
970 pub fn set_span(&mut self, new_span: Span) {
972 match self {
973 Value::Bool { internal_span, .. }
974 | Value::Int { internal_span, .. }
975 | Value::Float { internal_span, .. }
976 | Value::Filesize { internal_span, .. }
977 | Value::Duration { internal_span, .. }
978 | Value::Date { internal_span, .. }
979 | Value::Range { internal_span, .. }
980 | Value::String { internal_span, .. }
981 | Value::Glob { internal_span, .. }
982 | Value::Record { internal_span, .. }
983 | Value::List { internal_span, .. }
984 | Value::Closure { internal_span, .. }
985 | Value::Nothing { internal_span, .. }
986 | Value::Binary { internal_span, .. }
987 | Value::CellPath { internal_span, .. }
988 | Value::Custom { internal_span, .. } => *internal_span = new_span,
989 Value::Error { .. } => (),
990 }
991 }
992
993 pub fn with_span(mut self, new_span: Span) -> Value {
995 self.set_span(new_span);
996 self
997 }
998
999 pub fn get_type(&self) -> Type {
1001 match self {
1002 Value::Bool { .. } => Type::Bool,
1003 Value::Int { .. } => Type::Int,
1004 Value::Float { .. } => Type::Float,
1005 Value::Filesize { .. } => Type::Filesize,
1006 Value::Duration { .. } => Type::Duration,
1007 Value::Date { .. } => Type::Date,
1008 Value::Range { .. } => Type::Range,
1009 Value::String { .. } => Type::String,
1010 Value::Glob { .. } => Type::Glob,
1011 Value::Record { val, .. } => {
1012 Type::Record(val.iter().map(|(x, y)| (x.clone(), y.get_type())).collect())
1013 }
1014 Value::List { vals, .. } => {
1015 let ty = Type::supertype_of(vals.iter().map(Value::get_type)).unwrap_or(Type::Any);
1016
1017 match ty {
1018 Type::Record(columns) => Type::Table(columns),
1019 ty => Type::list(ty),
1020 }
1021 }
1022 Value::Nothing { .. } => Type::Nothing,
1023 Value::Closure { .. } => Type::Closure,
1024 Value::Error { .. } => Type::Error,
1025 Value::Binary { .. } => Type::Binary,
1026 Value::CellPath { .. } => Type::CellPath,
1027 Value::Custom { val, .. } => Type::Custom(val.type_name().into()),
1028 }
1029 }
1030
1031 pub fn get_type_shallow(&self) -> Type {
1034 match self {
1035 Value::Bool { .. } => Type::Bool,
1036 Value::Int { .. } => Type::Int,
1037 Value::Float { .. } => Type::Float,
1038 Value::Filesize { .. } => Type::Filesize,
1039 Value::Duration { .. } => Type::Duration,
1040 Value::Date { .. } => Type::Date,
1041 Value::Range { .. } => Type::Range,
1042 Value::String { .. } => Type::String,
1043 Value::Glob { .. } => Type::Glob,
1044 Value::Record { .. } => Type::record(),
1045 Value::List { .. } => Type::list(Type::Any),
1046 Value::Nothing { .. } => Type::Nothing,
1047 Value::Closure { .. } => Type::Closure,
1048 Value::Error { .. } => Type::Error,
1049 Value::Binary { .. } => Type::Binary,
1050 Value::CellPath { .. } => Type::CellPath,
1051 Value::Custom { val, .. } => Type::Custom(val.type_name().into()),
1052 }
1053 }
1054
1055 pub fn get_data_by_key(&self, name: &str) -> Option<Value> {
1056 let span = self.span();
1057 match self {
1058 Value::Record { val, .. } => val.get(name).cloned(),
1059 Value::List { vals, .. } => {
1060 let out = vals
1061 .iter()
1062 .map(|item| {
1063 item.as_record()
1064 .ok()
1065 .and_then(|val| val.get(name).cloned())
1066 .unwrap_or(Value::nothing(span))
1067 })
1068 .collect::<Vec<_>>();
1069
1070 if !out.is_empty() {
1071 Some(Value::list(out, span))
1072 } else {
1073 None
1074 }
1075 }
1076 _ => None,
1077 }
1078 }
1079
1080 fn format_datetime<Tz: TimeZone>(&self, date_time: &DateTime<Tz>, formatter: &str) -> String
1081 where
1082 Tz::Offset: Display,
1083 {
1084 let mut formatter_buf = String::new();
1085 let locale = get_locale_from_env_vars(Some("LC_TIME"), |name| std::env::var(name).ok())
1086 .and_then(|s| s.as_ref().try_into().ok())
1087 .unwrap_or(Locale::en_US);
1088 let format = date_time.format_localized(formatter, locale);
1089
1090 match formatter_buf.write_fmt(format_args!("{format}")) {
1091 Ok(_) => (),
1092 Err(_) => formatter_buf = format!("Invalid format string {formatter}"),
1093 }
1094 formatter_buf
1095 }
1096
1097 pub fn to_expanded_string(&self, separator: &str, config: &Config) -> String {
1102 let span = self.span();
1103 match self {
1104 Value::Bool { val, .. } => val.to_string(),
1105 Value::Int { val, .. } => val.to_string(),
1106 Value::Float { val, .. } => ObviousFloat(*val).to_string(),
1107 Value::Filesize { val, .. } => config.filesize.format(*val).to_string(),
1108 Value::Duration { val, .. } => format_duration(*val, config.duration_max_unit),
1109 Value::Date { val, .. } => match &config.datetime_format.normal {
1110 Some(format) => self.format_datetime(val, format),
1111 None => {
1112 format!(
1113 "{} ({})",
1114 if val.year() >= 0 && val.year() <= 9999 {
1115 val.to_rfc2822()
1116 } else {
1117 val.to_rfc3339()
1118 },
1119 human_time_from_now(val),
1120 )
1121 }
1122 },
1123 Value::Range { val, .. } => val.to_string(),
1124 Value::String { val, .. } => val.clone(),
1125 Value::Glob { val, .. } => val.clone(),
1126 Value::List { vals: val, .. } => format!(
1127 "[{}]",
1128 val.iter()
1129 .map(|x| x.to_expanded_string(", ", config))
1130 .collect::<Vec<_>>()
1131 .join(separator)
1132 ),
1133 Value::Record { val, .. } => format!(
1134 "{{{}}}",
1135 val.iter()
1136 .map(|(x, y)| format!("{}: {}", x, y.to_expanded_string(", ", config)))
1137 .collect::<Vec<_>>()
1138 .join(separator)
1139 ),
1140 Value::Closure { val, .. } => format!("closure_{}", val.block_id.get()),
1141 Value::Nothing { .. } => String::new(),
1142 Value::Error { error, .. } => format!("{error:?}"),
1143 Value::Binary { val, .. } => format!("{val:?}"),
1144 Value::CellPath { val, .. } => val.to_string(),
1145 Value::Custom { val, .. } => val
1148 .to_base_value(span)
1149 .map(|val| val.to_expanded_string(separator, config))
1150 .unwrap_or_else(|_| format!("<{}>", val.type_name())),
1151 }
1152 }
1153
1154 pub fn to_abbreviated_string(&self, config: &Config) -> String {
1162 match self {
1163 Value::Date { val, .. } => match &config.datetime_format.table {
1164 Some(format) => self.format_datetime(val, format),
1165 None => human_time_from_now(val).to_string(),
1166 },
1167 Value::List { vals, .. } => {
1168 if !vals.is_empty() && vals.iter().all(|x| matches!(x, Value::Record { .. })) {
1169 format!(
1170 "[table {} row{}]",
1171 vals.len(),
1172 if vals.len() == 1 { "" } else { "s" }
1173 )
1174 } else {
1175 format!(
1176 "[list {} item{}]",
1177 vals.len(),
1178 if vals.len() == 1 { "" } else { "s" }
1179 )
1180 }
1181 }
1182 Value::Record { val, .. } => format!(
1183 "{{record {} field{}}}",
1184 val.len(),
1185 if val.len() == 1 { "" } else { "s" }
1186 ),
1187 val => val.to_expanded_string(", ", config),
1188 }
1189 }
1190
1191 pub fn to_parsable_string(&self, separator: &str, config: &Config) -> String {
1201 match self {
1202 Value::String { val, .. } => format!("'{val}'"),
1204 Value::List { vals: val, .. } => format!(
1206 "[{}]",
1207 val.iter()
1208 .map(|x| x.to_parsable_string(", ", config))
1209 .collect::<Vec<_>>()
1210 .join(separator)
1211 ),
1212 Value::Record { val, .. } => format!(
1213 "{{{}}}",
1214 val.iter()
1215 .map(|(x, y)| format!("{}: {}", x, y.to_parsable_string(", ", config)))
1216 .collect::<Vec<_>>()
1217 .join(separator)
1218 ),
1219 _ => self.to_expanded_string(separator, config),
1221 }
1222 }
1223
1224 pub fn to_debug_string(&self) -> String {
1229 match self {
1230 Value::String { val, .. } => {
1231 if contains_emoji(val) {
1232 format!(
1234 "{:#?}",
1235 Value::string(val.escape_unicode().to_string(), self.span())
1236 )
1237 } else {
1238 format!("{self:-#?}")
1239 }
1240 }
1241 _ => format!("{self:-#?}"),
1242 }
1243 }
1244
1245 pub fn try_put_int_path_member_on_top(cell_paths: &[PathMember]) -> Option<Vec<PathMember>> {
1250 if !nu_experimental::REORDER_CELL_PATHS.get() {
1251 return None;
1252 }
1253 let idx = cell_paths
1254 .iter()
1255 .position(|pm| matches!(pm, PathMember::Int { .. }));
1256 idx.map(|idx| {
1257 let mut cell_paths = cell_paths.to_vec();
1258 cell_paths[0..idx + 1].rotate_right(1);
1259 cell_paths
1260 })
1261 }
1262
1263 pub fn follow_cell_path<'out>(
1265 &'out self,
1266 cell_path: &[PathMember],
1267 ) -> Result<Cow<'out, Value>, ShellError> {
1268 let mut store: Value = Value::test_nothing();
1271 let mut current: MultiLife<'out, '_, Value> = MultiLife::Out(self);
1272
1273 let reorder_cell_paths = nu_experimental::REORDER_CELL_PATHS.get();
1274
1275 let mut members: Vec<_> = if reorder_cell_paths {
1276 cell_path.iter().map(Some).collect()
1277 } else {
1278 Vec::new()
1279 };
1280 let mut members = members.as_mut_slice();
1281 let mut cell_path = cell_path;
1282
1283 loop {
1284 let member = if reorder_cell_paths {
1285 while let Some(None) = members.first() {
1287 members = &mut members[1..];
1288 }
1289
1290 if members.is_empty() {
1291 break;
1292 }
1293
1294 let member = if let Value::List { .. } = &*current {
1297 members
1299 .iter_mut()
1300 .find(|x| matches!(x, Some(PathMember::Int { .. })))
1301 .and_then(Option::take)
1303 } else {
1304 None
1305 };
1306
1307 let Some(member) = member.or_else(|| members.first_mut().and_then(Option::take))
1308 else {
1309 break;
1310 };
1311 member
1312 } else {
1313 match cell_path {
1314 [first, rest @ ..] => {
1315 cell_path = rest;
1316 first
1317 }
1318 _ => break,
1319 }
1320 };
1321
1322 current = match current {
1323 MultiLife::Out(current) => match get_value_member(current, member)? {
1324 ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
1325 ControlFlow::Continue(x) => match x {
1326 Cow::Borrowed(x) => MultiLife::Out(x),
1327 Cow::Owned(x) => {
1328 store = x;
1329 MultiLife::Local(&store)
1330 }
1331 },
1332 },
1333 MultiLife::Local(current) => match get_value_member(current, member)? {
1334 ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
1335 ControlFlow::Continue(x) => match x {
1336 Cow::Borrowed(x) => MultiLife::Local(x),
1337 Cow::Owned(x) => {
1338 store = x;
1339 MultiLife::Local(&store)
1340 }
1341 },
1342 },
1343 };
1344 }
1345
1346 if let Value::Error { error, .. } = &*current {
1349 Err(error.as_ref().clone())
1350 } else {
1351 Ok(match current {
1352 MultiLife::Out(x) => Cow::Borrowed(x),
1353 MultiLife::Local(x) => {
1354 let x = if std::ptr::eq(x, &store) {
1355 store
1356 } else {
1357 x.clone()
1358 };
1359 Cow::Owned(x)
1360 }
1361 })
1362 }
1363 }
1364
1365 pub fn upsert_cell_path(
1367 &mut self,
1368 cell_path: &[PathMember],
1369 callback: Box<dyn FnOnce(&Value) -> Value>,
1370 ) -> Result<(), ShellError> {
1371 let new_val = callback(self.follow_cell_path(cell_path)?.as_ref());
1372
1373 match new_val {
1374 Value::Error { error, .. } => Err(*error),
1375 new_val => self.upsert_data_at_cell_path(cell_path, new_val),
1376 }
1377 }
1378
1379 pub fn upsert_data_at_cell_path(
1380 &mut self,
1381 cell_path: &[PathMember],
1382 new_val: Value,
1383 ) -> Result<(), ShellError> {
1384 self.mutate_data_at_cell_path(cell_path, new_val, &CellPathMutation::Upsert)
1385 }
1386
1387 pub fn update_cell_path<'a>(
1389 &mut self,
1390 cell_path: &[PathMember],
1391 callback: Box<dyn FnOnce(&Value) -> Value + 'a>,
1392 ) -> Result<(), ShellError> {
1393 let new_val = callback(self.follow_cell_path(cell_path)?.as_ref());
1394
1395 match new_val {
1396 Value::Error { error, .. } => Err(*error),
1397 new_val => self.update_data_at_cell_path(cell_path, new_val),
1398 }
1399 }
1400
1401 pub fn update_data_at_cell_path(
1402 &mut self,
1403 cell_path: &[PathMember],
1404 new_val: Value,
1405 ) -> Result<(), ShellError> {
1406 self.mutate_data_at_cell_path(cell_path, new_val, &CellPathMutation::Update)
1407 }
1408
1409 pub fn remove_data_at_cell_path(&mut self, cell_path: &[PathMember]) -> Result<(), ShellError> {
1410 let Some((member, path)) = cell_path.split_last() else {
1411 return Ok(());
1412 };
1413 self.mutate_data_at_cell_path(
1414 path,
1415 Value::nothing(Span::unknown()),
1416 &CellPathMutation::Remove {
1417 member: member.clone(),
1418 },
1419 )
1420 }
1421
1422 pub fn insert_data_at_cell_path(
1423 &mut self,
1424 cell_path: &[PathMember],
1425 new_val: Value,
1426 head_span: Span,
1427 ) -> Result<(), ShellError> {
1428 self.mutate_data_at_cell_path(cell_path, new_val, &CellPathMutation::Insert { head_span })
1429 }
1430
1431 fn remove_member(&mut self, member: &PathMember) -> Result<(), ShellError> {
1433 let v_span = self.span();
1434 match member {
1435 PathMember::String {
1436 val: col_name,
1437 span,
1438 optional,
1439 casing,
1440 } => match self {
1441 Value::List { vals, .. } => {
1442 for val in vals.to_mut() {
1443 let v_span = val.span();
1444 match val {
1445 Value::Record { val: record, .. } => {
1446 let value = record.to_mut().cased_mut(*casing).remove(col_name);
1447 if value.is_none() && !optional {
1448 return Err(ShellError::CantFindColumn {
1449 col_name: col_name.clone(),
1450 span: Some(*span),
1451 src_span: v_span,
1452 });
1453 }
1454 }
1455 v => {
1456 return Err(ShellError::CantFindColumn {
1457 col_name: col_name.clone(),
1458 span: Some(*span),
1459 src_span: v.span(),
1460 });
1461 }
1462 }
1463 }
1464 Ok(())
1465 }
1466 Value::Record { val: record, .. } => {
1467 if record
1468 .to_mut()
1469 .cased_mut(*casing)
1470 .remove(col_name)
1471 .is_none()
1472 && !optional
1473 {
1474 return Err(ShellError::CantFindColumn {
1475 col_name: col_name.clone(),
1476 span: Some(*span),
1477 src_span: v_span,
1478 });
1479 }
1480 Ok(())
1481 }
1482 v => Err(ShellError::CantFindColumn {
1483 col_name: col_name.clone(),
1484 span: Some(*span),
1485 src_span: v.span(),
1486 }),
1487 },
1488 PathMember::Int {
1489 val: row_num,
1490 span,
1491 optional,
1492 } => match self {
1493 Value::List { vals, .. } => {
1494 if *row_num < vals.len() {
1495 vals.to_mut().remove(*row_num);
1496 Ok(())
1497 } else if *optional {
1498 Ok(())
1499 } else if vals.is_empty() {
1500 Err(ShellError::AccessEmptyContent { span: *span })
1501 } else {
1502 Err(ShellError::AccessBeyondEnd {
1503 max_idx: vals.len() - 1,
1504 span: *span,
1505 })
1506 }
1507 }
1508 v => Err(ShellError::NotAList {
1509 dst_span: *span,
1510 src_span: v.span(),
1511 }),
1512 },
1513 }
1514 }
1515
1516 fn mutate_record_at_string_member(
1517 record: &mut Record,
1518 member: &PathMember,
1519 src_span: Span,
1520 path: &[PathMember],
1521 new_val: Value,
1522 action: &CellPathMutation,
1523 ) -> Result<(), ShellError> {
1524 let PathMember::String {
1525 val: col_name,
1526 span,
1527 casing,
1528 optional,
1529 } = member
1530 else {
1531 return Err(ShellError::NushellFailed {
1532 msg: "mutate_record_at_string_member called with non-String PathMember".into(),
1533 });
1534 };
1535 if let Some(val) = record.cased_mut(*casing).get_mut(col_name) {
1536 if path.is_empty() && matches!(action, CellPathMutation::Insert { .. }) {
1537 return Err(ShellError::ColumnAlreadyExists {
1538 col_name: col_name.to_owned(),
1539 span: *span,
1540 src_span,
1541 });
1542 }
1543 val.mutate_data_at_cell_path(path, new_val, action)
1544 } else {
1545 match action {
1546 CellPathMutation::Update | CellPathMutation::Remove { .. } => {
1547 if !optional {
1548 return Err(ShellError::CantFindColumn {
1549 col_name: col_name.to_owned(),
1550 span: Some(*span),
1551 src_span,
1552 });
1553 }
1554 Ok(())
1555 }
1556 _ => {
1557 let new_col = Value::with_data_at_cell_path(path, new_val)?;
1558 record.push(col_name, new_col);
1559 Ok(())
1560 }
1561 }
1562 }
1563 }
1564
1565 pub fn mutate_data_at_cell_path(
1566 &mut self,
1567 cell_path: &[PathMember],
1568 new_val: Value,
1569 action: &CellPathMutation,
1570 ) -> Result<(), ShellError> {
1571 let v_span = self.span();
1572 let Some((member, path)) = cell_path.split_first() else {
1573 match action {
1574 CellPathMutation::Remove { member } => return self.remove_member(member),
1575 _ => {
1576 *self = new_val;
1577 return Ok(());
1578 }
1579 }
1580 };
1581
1582 match member {
1583 PathMember::String {
1584 val: col_name,
1585 span,
1586 optional,
1587 ..
1588 } => match self {
1589 Value::List { vals, .. } => {
1590 if !matches!(action, CellPathMutation::Remove { .. })
1591 && let Some(new_cell_path) = Self::try_put_int_path_member_on_top(cell_path)
1592 {
1593 self.mutate_data_at_cell_path(&new_cell_path, new_val.clone(), action)?;
1594 } else {
1595 for val in vals.to_mut() {
1596 let v_span = val.span();
1597 match val {
1598 Value::Record { val: record, .. } => {
1599 Self::mutate_record_at_string_member(
1600 record.to_mut(),
1601 member,
1602 v_span,
1603 path,
1604 new_val.clone(),
1605 action,
1606 )?;
1607 }
1608 Value::Error { error, .. } => return Err(*error.clone()),
1609 v => match action {
1610 CellPathMutation::Insert { head_span } => {
1611 return Err(ShellError::UnsupportedInput {
1612 msg: "expected table or record".into(),
1613 input: format!("input type: {:?}", v.get_type()),
1614 msg_span: *head_span,
1615 input_span: *span,
1616 });
1617 }
1618 CellPathMutation::Update | CellPathMutation::Remove { .. } => {
1619 if !*optional {
1620 return Err(ShellError::CantFindColumn {
1621 col_name: col_name.clone(),
1622 span: Some(*span),
1623 src_span: v.span(),
1624 });
1625 }
1626 }
1627 CellPathMutation::Upsert => {
1628 return Err(ShellError::CantFindColumn {
1629 col_name: col_name.clone(),
1630 span: Some(*span),
1631 src_span: v.span(),
1632 });
1633 }
1634 },
1635 }
1636 }
1637 }
1638 }
1639 Value::Record { val: record, .. } => {
1640 Self::mutate_record_at_string_member(
1641 record.to_mut(),
1642 member,
1643 v_span,
1644 path,
1645 new_val,
1646 action,
1647 )?;
1648 }
1649 Value::Error { error, .. } => return Err(*error.clone()),
1650 Value::Custom { val, .. } => {
1651 let mut full_path = vec![member.clone()];
1652 full_path.extend(path.iter().cloned());
1653 let result =
1654 val.update_data_at_cell_path(&full_path, new_val, action, v_span)?;
1655 *self = result;
1656 return Ok(());
1657 }
1658 v => match action {
1659 CellPathMutation::Insert { head_span } => {
1660 return Err(ShellError::UnsupportedInput {
1661 msg: "table or record".into(),
1662 input: format!("input type: {:?}", v.get_type()),
1663 msg_span: *head_span,
1664 input_span: *span,
1665 });
1666 }
1667 CellPathMutation::Update | CellPathMutation::Remove { .. } => {
1668 if !*optional {
1669 return Err(ShellError::CantFindColumn {
1670 col_name: col_name.clone(),
1671 span: Some(*span),
1672 src_span: v.span(),
1673 });
1674 }
1675 }
1676 CellPathMutation::Upsert => {
1677 return Err(ShellError::CantFindColumn {
1678 col_name: col_name.clone(),
1679 span: Some(*span),
1680 src_span: v.span(),
1681 });
1682 }
1683 },
1684 },
1685 PathMember::Int {
1686 val: row_num,
1687 span,
1688 optional,
1689 } => match self {
1690 Value::List { vals, .. } => {
1691 if *row_num < vals.len() {
1692 let vals = vals.to_mut();
1693 let v = &mut vals[*row_num];
1694 if path.is_empty() && matches!(action, CellPathMutation::Insert { .. }) {
1695 vals.insert(*row_num, new_val);
1696 } else {
1697 v.mutate_data_at_cell_path(path, new_val, action)?;
1698 }
1699 } else {
1700 match action {
1701 CellPathMutation::Upsert | CellPathMutation::Insert { .. } => {
1702 if vals.len() != *row_num {
1703 return Err(ShellError::InsertAfterNextFreeIndex {
1704 available_idx: vals.len(),
1705 span: *span,
1706 });
1707 }
1708 vals.to_mut()
1709 .push(Value::with_data_at_cell_path(path, new_val)?);
1710 }
1711 CellPathMutation::Update | CellPathMutation::Remove { .. } => {
1712 if !*optional {
1713 if vals.is_empty() {
1714 return Err(ShellError::AccessEmptyContent { span: *span });
1715 } else {
1716 return Err(ShellError::AccessBeyondEnd {
1717 max_idx: vals.len() - 1,
1718 span: *span,
1719 });
1720 }
1721 }
1722 }
1723 }
1724 }
1725 }
1726 Value::Error { error, .. } => return Err(*error.clone()),
1727 Value::Custom { val, .. } => {
1728 let mut full_path = vec![member.clone()];
1729 full_path.extend(path.iter().cloned());
1730 let result =
1731 val.update_data_at_cell_path(&full_path, new_val, action, v_span)?;
1732 *self = result;
1733 return Ok(());
1734 }
1735 _ => {
1736 return Err(ShellError::NotAList {
1737 dst_span: *span,
1738 src_span: v_span,
1739 });
1740 }
1741 },
1742 }
1743 Ok(())
1744 }
1745
1746 fn with_data_at_cell_path(cell_path: &[PathMember], value: Value) -> Result<Value, ShellError> {
1749 if let Some((member, path)) = cell_path.split_first() {
1750 let span = value.span();
1751 match member {
1752 PathMember::String { val, .. } => Ok(Value::record(
1753 std::iter::once((val.clone(), Value::with_data_at_cell_path(path, value)?))
1754 .collect(),
1755 span,
1756 )),
1757 PathMember::Int { val, .. } => {
1758 if *val == 0usize {
1759 Ok(Value::list(
1760 vec![Value::with_data_at_cell_path(path, value)?],
1761 span,
1762 ))
1763 } else {
1764 Err(ShellError::InsertAfterNextFreeIndex {
1765 available_idx: 0,
1766 span,
1767 })
1768 }
1769 }
1770 }
1771 } else {
1772 Ok(value)
1773 }
1774 }
1775
1776 pub fn recurse_mut<E>(
1783 &mut self,
1784 f: &mut impl FnMut(&mut Value) -> Result<(), E>,
1785 ) -> Result<(), E> {
1786 f(self)?;
1788 match self {
1790 Value::Record { val, .. } => val
1791 .to_mut()
1792 .iter_mut()
1793 .try_for_each(|(_, rec_value)| rec_value.recurse_mut(f)),
1794 Value::List { vals, .. } => vals
1795 .to_mut()
1796 .iter_mut()
1797 .try_for_each(|list_value| list_value.recurse_mut(f)),
1798 Value::Closure { val, .. } => val
1801 .captures
1802 .iter_mut()
1803 .map(|(_, captured_value)| captured_value)
1804 .try_for_each(|captured_value| captured_value.recurse_mut(f)),
1805 Value::Bool { .. }
1807 | Value::Int { .. }
1808 | Value::Float { .. }
1809 | Value::Filesize { .. }
1810 | Value::Duration { .. }
1811 | Value::Date { .. }
1812 | Value::Range { .. }
1813 | Value::String { .. }
1814 | Value::Glob { .. }
1815 | Value::Nothing { .. }
1816 | Value::Error { .. }
1817 | Value::Binary { .. }
1818 | Value::CellPath { .. } => Ok(()),
1819 Value::Custom { .. } => Ok(()),
1821 }
1822 }
1823
1824 pub fn is_empty(&self) -> bool {
1826 match self {
1827 Value::String { val, .. } => val.is_empty(),
1828 Value::List { vals, .. } => vals.is_empty(),
1829 Value::Record { val, .. } => val.is_empty(),
1830 Value::Binary { val, .. } => val.is_empty(),
1831 Value::Nothing { .. } => true,
1832 _ => false,
1833 }
1834 }
1835
1836 pub fn is_nothing(&self) -> bool {
1837 matches!(self, Value::Nothing { .. })
1838 }
1839
1840 pub fn is_error(&self) -> bool {
1841 matches!(self, Value::Error { .. })
1842 }
1843
1844 pub fn unwrap_error(self) -> Result<Self, ShellError> {
1846 match self {
1847 Self::Error { error, .. } => Err(*error),
1848 val => Ok(val),
1849 }
1850 }
1851
1852 pub fn is_true(&self) -> bool {
1853 matches!(self, Value::Bool { val: true, .. })
1854 }
1855
1856 pub fn is_false(&self) -> bool {
1857 matches!(self, Value::Bool { val: false, .. })
1858 }
1859
1860 pub fn columns(&self) -> impl Iterator<Item = &String> {
1861 let opt = match self {
1862 Value::Record { val, .. } => Some(val.columns()),
1863 _ => None,
1864 };
1865
1866 opt.into_iter().flatten()
1867 }
1868
1869 pub fn memory_size(&self) -> usize {
1871 match self {
1872 Value::Bool { .. } => std::mem::size_of::<Self>(),
1873 Value::Int { .. } => std::mem::size_of::<Self>(),
1874 Value::Float { .. } => std::mem::size_of::<Self>(),
1875 Value::Filesize { .. } => std::mem::size_of::<Self>(),
1876 Value::Duration { .. } => std::mem::size_of::<Self>(),
1877 Value::Date { .. } => std::mem::size_of::<Self>(),
1878 Value::Range { val, .. } => std::mem::size_of::<Self>() + val.memory_size(),
1879 Value::String { val, .. } => std::mem::size_of::<Self>() + val.capacity(),
1880 Value::Glob { val, .. } => std::mem::size_of::<Self>() + val.capacity(),
1881 Value::Record { val, .. } => std::mem::size_of::<Self>() + val.memory_size(),
1882 Value::List { vals, .. } => {
1883 std::mem::size_of::<Self>() + vals.iter().map(|v| v.memory_size()).sum::<usize>()
1884 }
1885 Value::Closure { val, .. } => std::mem::size_of::<Self>() + val.memory_size(),
1886 Value::Nothing { .. } => std::mem::size_of::<Self>(),
1887 Value::Error { error, .. } => {
1888 std::mem::size_of::<Self>() + std::mem::size_of_val(error)
1889 }
1890 Value::Binary { val, .. } => std::mem::size_of::<Self>() + val.capacity(),
1891 Value::CellPath { val, .. } => std::mem::size_of::<Self>() + val.memory_size(),
1892 Value::Custom { val, .. } => std::mem::size_of::<Self>() + val.memory_size(),
1893 }
1894 }
1895
1896 pub fn bool(val: bool, span: Span) -> Value {
1897 Value::Bool {
1898 val,
1899 internal_span: span,
1900 }
1901 }
1902
1903 pub fn int(val: i64, span: Span) -> Value {
1904 Value::Int {
1905 val,
1906 internal_span: span,
1907 }
1908 }
1909
1910 pub fn float(val: f64, span: Span) -> Value {
1911 Value::Float {
1912 val,
1913 internal_span: span,
1914 }
1915 }
1916
1917 pub fn filesize(val: impl Into<Filesize>, span: Span) -> Value {
1918 Value::Filesize {
1919 val: val.into(),
1920 internal_span: span,
1921 }
1922 }
1923
1924 pub fn duration(val: i64, span: Span) -> Value {
1925 Value::Duration {
1926 val,
1927 internal_span: span,
1928 }
1929 }
1930
1931 pub fn date(val: DateTime<FixedOffset>, span: Span) -> Value {
1932 Value::Date {
1933 val,
1934 internal_span: span,
1935 }
1936 }
1937
1938 pub fn range(val: Range, span: Span) -> Value {
1939 Value::Range {
1940 val: val.into(),
1941 signals: None,
1942 internal_span: span,
1943 }
1944 }
1945
1946 pub fn string(val: impl Into<String>, span: Span) -> Value {
1947 Value::String {
1948 val: val.into(),
1949 internal_span: span,
1950 }
1951 }
1952
1953 pub fn glob(val: impl Into<String>, no_expand: bool, span: Span) -> Value {
1954 Value::Glob {
1955 val: val.into(),
1956 no_expand,
1957 internal_span: span,
1958 }
1959 }
1960
1961 pub fn record(val: Record, span: Span) -> Value {
1962 Value::Record {
1963 val: SharedCow::new(val),
1964 internal_span: span,
1965 }
1966 }
1967
1968 pub fn list(vals: Vec<Value>, span: Span) -> Value {
1969 Value::list_shared(SharedCow::new(vals), span)
1970 }
1971
1972 pub fn list_shared(vals: SharedCow<Vec<Value>>, span: Span) -> Value {
1974 Value::List {
1975 vals,
1976 signals: None,
1977 internal_span: span,
1978 }
1979 }
1980
1981 pub fn closure(val: Closure, span: Span) -> Value {
1982 Value::Closure {
1983 val: val.into(),
1984 internal_span: span,
1985 }
1986 }
1987
1988 pub fn nothing(span: Span) -> Value {
1990 Value::Nothing {
1991 internal_span: span,
1992 }
1993 }
1994
1995 pub fn error(error: ShellError, span: Span) -> Value {
1996 Value::Error {
1997 error: Box::new(error),
1998 internal_span: span,
1999 }
2000 }
2001
2002 pub fn binary(val: impl Into<Vec<u8>>, span: Span) -> Value {
2003 Value::Binary {
2004 val: SharedCow::new(val.into()),
2005 internal_span: span,
2006 }
2007 }
2008
2009 pub fn cell_path(val: CellPath, span: Span) -> Value {
2010 Value::CellPath {
2011 val,
2012 internal_span: span,
2013 }
2014 }
2015
2016 pub fn custom(val: Box<dyn CustomValue>, span: Span) -> Value {
2017 Value::Custom {
2018 val,
2019 internal_span: span,
2020 }
2021 }
2022
2023 pub fn test_bool(val: bool) -> Value {
2026 Value::bool(val, Span::test_data())
2027 }
2028
2029 pub fn test_int(val: i64) -> Value {
2032 Value::int(val, Span::test_data())
2033 }
2034
2035 pub fn test_float(val: f64) -> Value {
2038 Value::float(val, Span::test_data())
2039 }
2040
2041 pub fn test_filesize(val: impl Into<Filesize>) -> Value {
2044 Value::filesize(val, Span::test_data())
2045 }
2046
2047 pub fn test_duration(val: i64) -> Value {
2050 Value::duration(val, Span::test_data())
2051 }
2052
2053 pub fn test_date(val: DateTime<FixedOffset>) -> Value {
2056 Value::date(val, Span::test_data())
2057 }
2058
2059 pub fn test_range(val: Range) -> Value {
2062 Value::range(val, Span::test_data())
2063 }
2064
2065 pub fn test_string(val: impl Into<String>) -> Value {
2068 Value::string(val, Span::test_data())
2069 }
2070
2071 pub fn test_glob(val: impl Into<String>) -> Value {
2074 Value::glob(val, false, Span::test_data())
2075 }
2076
2077 pub fn test_record(val: Record) -> Value {
2080 Value::record(val, Span::test_data())
2081 }
2082
2083 pub fn test_list(vals: Vec<Value>) -> Value {
2086 Value::list(vals, Span::test_data())
2087 }
2088
2089 pub fn test_closure(val: Closure) -> Value {
2092 Value::closure(val, Span::test_data())
2093 }
2094
2095 pub fn test_nothing() -> Value {
2098 Value::nothing(Span::test_data())
2099 }
2100
2101 pub fn test_binary(val: impl Into<Vec<u8>>) -> Value {
2104 Value::binary(val, Span::test_data())
2105 }
2106
2107 pub fn test_cell_path(val: CellPath) -> Value {
2110 Value::cell_path(val, Span::test_data())
2111 }
2112
2113 pub fn test_custom_value(val: Box<dyn CustomValue>) -> Value {
2116 Value::custom(val, Span::test_data())
2117 }
2118
2119 pub fn test_values() -> Vec<Value> {
2125 vec![
2126 Value::test_bool(false),
2127 Value::test_int(0),
2128 Value::test_filesize(0),
2129 Value::test_duration(0),
2130 Value::test_date(DateTime::UNIX_EPOCH.into()),
2131 Value::test_range(Range::IntRange(IntRange {
2132 start: 0,
2133 step: 1,
2134 end: Bound::Excluded(0),
2135 })),
2136 Value::test_float(0.0),
2137 Value::test_string(String::new()),
2138 Value::test_record(Record::new()),
2139 Value::test_list(Vec::new()),
2140 Value::test_closure(Closure {
2141 block_id: BlockId::new(0),
2142 captures: Vec::new(),
2143 }),
2144 Value::test_nothing(),
2145 Value::error(
2146 ShellError::NushellFailed { msg: String::new() },
2147 Span::test_data(),
2148 ),
2149 Value::test_binary(Vec::new()),
2150 Value::test_cell_path(CellPath {
2151 members: Vec::new(),
2152 }),
2153 ]
2155 }
2156
2157 #[track_caller]
2163 pub fn assert_eq(&self, other: impl IntoValue) {
2164 let other = other.into_value(Span::test_data());
2165 assert_eq!(self, &other)
2166 }
2167
2168 pub fn inject_signals(&mut self, engine_state: &EngineState) {
2171 match self {
2172 Value::List { signals: s, .. } | Value::Range { signals: s, .. } => {
2173 *s = Some(engine_state.signals().clone());
2174 }
2175 _ => (),
2176 }
2177 }
2178}
2179
2180impl CompareTypes<Type> for Value {
2181 fn compare_types(&self, other: &Type) -> Option<TypeRelation> {
2182 match other {
2183 Type::Any => return Some(TypeRelation::Subtype),
2184 Type::OneOf(oneof) => {
2185 return oneof
2186 .iter()
2187 .any(|ty| self.is_subtype_of(ty))
2188 .then_some(TypeRelation::Subtype);
2189 }
2190 _ => (),
2191 }
2192
2193 match self {
2194 Value::List { vals, .. } => match other {
2195 Type::List(ty) if let Type::Any = ty.as_ref() => Some(TypeRelation::Subtype),
2196 Type::List(ty) => {
2197 let ty = ty.as_ref();
2198 vals.iter()
2199 .map(|val| val.compare_types(ty))
2200 .try_fold(TypeRelation::Equal, |acc, e| acc.combine(e?))
2201 }
2202 Type::Table(cols) => vals
2203 .iter()
2204 .map(|val| val.as_record().ok().and_then(|rec| rec.compare_types(cols)))
2205 .try_fold(TypeRelation::Equal, |acc, e| acc.combine(e?)),
2206 _ => None,
2207 },
2208 Value::Record { val, .. } => match other {
2209 Type::Record(cols) => val.compare_types(cols),
2210 _ => None,
2211 },
2212 val => val.get_type().compare_types(other),
2213 }
2214 }
2215
2216 fn is_subtype_of(&self, other: &Type) -> bool {
2231 matches!(
2232 self.compare_types(other),
2233 Some(TypeRelation::Subtype | TypeRelation::Equal)
2234 )
2235 }
2236}
2237
2238fn get_value_member<'a>(
2239 current: &'a Value,
2240 member: &PathMember,
2241) -> Result<ControlFlow<Span, Cow<'a, Value>>, ShellError> {
2242 match member {
2243 PathMember::Int {
2244 val: count,
2245 span: origin_span,
2246 optional,
2247 } => {
2248 match current {
2250 Value::List { vals, .. } => {
2251 if *count < vals.len() {
2252 Ok(ControlFlow::Continue(Cow::Borrowed(&vals[*count])))
2253 } else if *optional {
2254 Ok(ControlFlow::Break(*origin_span))
2255 } else if vals.is_empty() {
2257 Err(ShellError::AccessEmptyContent { span: *origin_span })
2258 } else {
2259 Err(ShellError::AccessBeyondEnd {
2260 max_idx: vals.len() - 1,
2261 span: *origin_span,
2262 })
2263 }
2264 }
2265 Value::Binary { val, .. } => {
2266 if let Some(item) = val.get(*count) {
2267 Ok(ControlFlow::Continue(Cow::Owned(Value::int(
2268 *item as i64,
2269 *origin_span,
2270 ))))
2271 } else if *optional {
2272 Ok(ControlFlow::Break(*origin_span))
2273 } else if val.is_empty() {
2275 Err(ShellError::AccessEmptyContent { span: *origin_span })
2276 } else {
2277 Err(ShellError::AccessBeyondEnd {
2278 max_idx: val.len() - 1,
2279 span: *origin_span,
2280 })
2281 }
2282 }
2283 Value::Range { val, .. } => {
2284 if let Some(item) = val
2285 .into_range_iter(current.span(), Signals::empty())
2286 .nth(*count)
2287 {
2288 Ok(ControlFlow::Continue(Cow::Owned(item)))
2289 } else if *optional {
2290 Ok(ControlFlow::Break(*origin_span))
2291 } else {
2293 Err(ShellError::AccessBeyondEndOfStream {
2294 span: *origin_span,
2295 })
2296 }
2297 }
2298 Value::Custom { val, .. } => {
2299 match val.follow_path_int(current.span(), *count, *origin_span, *optional)
2300 {
2301 Ok(val) => Ok(ControlFlow::Continue(Cow::Owned(val))),
2302 Err(err) => {
2303 if *optional {
2304 Ok(ControlFlow::Break(*origin_span))
2305 } else {
2307 Err(err)
2308 }
2309 }
2310 }
2311 }
2312 Value::Nothing { .. } if *optional => Ok(ControlFlow::Break(*origin_span)),
2313 Value::Record { .. } => Err(ShellError::TypeMismatch {
2316 err_message:"Can't access record values with a row index. Try specifying a column name instead".into(),
2317 span: *origin_span,
2318 }),
2319 Value::Error { error, .. } => Err(*error.clone()),
2320 x => Err(ShellError::IncompatiblePathAccess { type_name: format!("{}", x.get_type()), span: *origin_span }),
2321 }
2322 }
2323 PathMember::String {
2324 val: column_name,
2325 span: origin_span,
2326 optional,
2327 casing,
2328 } => {
2329 let span = current.span();
2330 match current {
2331 Value::Record { val, .. } => {
2332 let found = val.cased(*casing).get(column_name);
2333 if let Some(found) = found {
2334 Ok(ControlFlow::Continue(Cow::Borrowed(found)))
2335 } else if *optional {
2336 Ok(ControlFlow::Break(*origin_span))
2337 } else if let Some(suggestion) = did_you_mean(val.columns(), column_name) {
2339 Err(ShellError::DidYouMean {
2340 suggestion,
2341 span: *origin_span,
2342 })
2343 } else {
2344 Err(ShellError::CantFindColumn {
2345 col_name: column_name.clone(),
2346 span: Some(*origin_span),
2347 src_span: span,
2348 })
2349 }
2350 }
2351 Value::List { vals, .. } => {
2355 let list = vals
2356 .iter()
2357 .map(|val| {
2358 let val_span = val.span();
2359 match val {
2360 Value::Record { val, .. } => {
2361 let found = val.cased(*casing).get(column_name);
2362 if let Some(found) = found {
2363 Ok(found.clone())
2364 } else if *optional {
2365 Ok(Value::nothing(*origin_span))
2366 } else if let Some(suggestion) =
2367 did_you_mean(val.columns(), column_name)
2368 {
2369 Err(ShellError::DidYouMean {
2370 suggestion,
2371 span: *origin_span,
2372 })
2373 } else {
2374 Err(ShellError::CantFindColumn {
2375 col_name: column_name.clone(),
2376 span: Some(*origin_span),
2377 src_span: val_span,
2378 })
2379 }
2380 }
2381 Value::Nothing { .. } if *optional => {
2382 Ok(Value::nothing(*origin_span))
2383 }
2384 _ => Err(ShellError::CantFindColumn {
2385 col_name: column_name.clone(),
2386 span: Some(*origin_span),
2387 src_span: val_span,
2388 }),
2389 }
2390 })
2391 .collect::<Result<_, _>>()?;
2392
2393 Ok(ControlFlow::Continue(Cow::Owned(Value::list(list, span))))
2394 }
2395 Value::Custom { val, .. } => {
2396 match val.follow_path_string(
2397 current.span(),
2398 column_name.clone(),
2399 *origin_span,
2400 *optional,
2401 *casing,
2402 ) {
2403 Ok(val) => Ok(ControlFlow::Continue(Cow::Owned(val))),
2404 Err(err) => {
2405 if *optional {
2406 Ok(ControlFlow::Break(*origin_span))
2407 } else {
2409 Err(err)
2410 }
2411 }
2412 }
2413 }
2414 Value::Nothing { .. } if *optional => Ok(ControlFlow::Break(*origin_span)),
2415 Value::Error { error, .. } => Err(error.as_ref().clone()),
2416 x => Err(ShellError::IncompatiblePathAccess {
2417 type_name: format!("{}", x.get_type()),
2418 span: *origin_span,
2419 }),
2420 }
2421 }
2422 }
2423}
2424
2425impl Default for Value {
2426 fn default() -> Self {
2427 Value::Nothing {
2428 internal_span: Span::unknown(),
2429 }
2430 }
2431}
2432
2433impl PartialOrd for Value {
2434 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
2435 fn compare_floats(val: f64, other: f64) -> Option<Ordering> {
2442 let prec = f64::EPSILON.max(val.abs().max(other.abs()) * f64::EPSILON);
2443
2444 if val.is_finite() && other.is_finite() && (other - val).abs() <= prec {
2445 return Some(Ordering::Equal);
2446 }
2447
2448 val.partial_cmp(&other)
2449 }
2450
2451 match (self, other) {
2452 (Value::Bool { val: lhs, .. }, rhs) => match rhs {
2453 Value::Bool { val: rhs, .. } => lhs.partial_cmp(rhs),
2454 Value::Int { .. } => Some(Ordering::Less),
2455 Value::Float { .. } => Some(Ordering::Less),
2456 Value::String { .. } => Some(Ordering::Less),
2457 Value::Glob { .. } => Some(Ordering::Less),
2458 Value::Filesize { .. } => Some(Ordering::Less),
2459 Value::Duration { .. } => Some(Ordering::Less),
2460 Value::Date { .. } => Some(Ordering::Less),
2461 Value::Range { .. } => Some(Ordering::Less),
2462 Value::Record { .. } => Some(Ordering::Less),
2463 Value::List { .. } => Some(Ordering::Less),
2464 Value::Closure { .. } => Some(Ordering::Less),
2465 Value::Error { .. } => Some(Ordering::Less),
2466 Value::Binary { .. } => Some(Ordering::Less),
2467 Value::CellPath { .. } => Some(Ordering::Less),
2468 Value::Custom { .. } => Some(Ordering::Less),
2469 Value::Nothing { .. } => Some(Ordering::Less),
2470 },
2471 (Value::Int { val: lhs, .. }, rhs) => match rhs {
2472 Value::Bool { .. } => Some(Ordering::Greater),
2473 Value::Int { val: rhs, .. } => lhs.partial_cmp(rhs),
2474 Value::Float { val: rhs, .. } => compare_floats(*lhs as f64, *rhs),
2475 Value::String { .. } => Some(Ordering::Less),
2476 Value::Glob { .. } => Some(Ordering::Less),
2477 Value::Filesize { .. } => Some(Ordering::Less),
2478 Value::Duration { .. } => Some(Ordering::Less),
2479 Value::Date { .. } => Some(Ordering::Less),
2480 Value::Range { .. } => Some(Ordering::Less),
2481 Value::Record { .. } => Some(Ordering::Less),
2482 Value::List { .. } => Some(Ordering::Less),
2483 Value::Closure { .. } => Some(Ordering::Less),
2484 Value::Error { .. } => Some(Ordering::Less),
2485 Value::Binary { .. } => Some(Ordering::Less),
2486 Value::CellPath { .. } => Some(Ordering::Less),
2487 Value::Custom { .. } => Some(Ordering::Less),
2488 Value::Nothing { .. } => Some(Ordering::Less),
2489 },
2490 (Value::Float { val: lhs, .. }, rhs) => match rhs {
2491 Value::Bool { .. } => Some(Ordering::Greater),
2492 Value::Int { val: rhs, .. } => compare_floats(*lhs, *rhs as f64),
2493 Value::Float { val: rhs, .. } => compare_floats(*lhs, *rhs),
2494 Value::String { .. } => Some(Ordering::Less),
2495 Value::Glob { .. } => Some(Ordering::Less),
2496 Value::Filesize { .. } => Some(Ordering::Less),
2497 Value::Duration { .. } => Some(Ordering::Less),
2498 Value::Date { .. } => Some(Ordering::Less),
2499 Value::Range { .. } => Some(Ordering::Less),
2500 Value::Record { .. } => Some(Ordering::Less),
2501 Value::List { .. } => Some(Ordering::Less),
2502 Value::Closure { .. } => Some(Ordering::Less),
2503 Value::Error { .. } => Some(Ordering::Less),
2504 Value::Binary { .. } => Some(Ordering::Less),
2505 Value::CellPath { .. } => Some(Ordering::Less),
2506 Value::Custom { .. } => Some(Ordering::Less),
2507 Value::Nothing { .. } => Some(Ordering::Less),
2508 },
2509 (Value::String { val: lhs, .. }, rhs) => match rhs {
2510 Value::Bool { .. } => Some(Ordering::Greater),
2511 Value::Int { .. } => Some(Ordering::Greater),
2512 Value::Float { .. } => Some(Ordering::Greater),
2513 Value::String { val: rhs, .. } => lhs.partial_cmp(rhs),
2514 Value::Glob { val: rhs, .. } => lhs.partial_cmp(rhs),
2515 Value::Filesize { .. } => Some(Ordering::Less),
2516 Value::Duration { .. } => Some(Ordering::Less),
2517 Value::Date { .. } => Some(Ordering::Less),
2518 Value::Range { .. } => Some(Ordering::Less),
2519 Value::Record { .. } => Some(Ordering::Less),
2520 Value::List { .. } => Some(Ordering::Less),
2521 Value::Closure { .. } => Some(Ordering::Less),
2522 Value::Error { .. } => Some(Ordering::Less),
2523 Value::Binary { .. } => Some(Ordering::Less),
2524 Value::CellPath { .. } => Some(Ordering::Less),
2525 Value::Custom { .. } => Some(Ordering::Less),
2526 Value::Nothing { .. } => Some(Ordering::Less),
2527 },
2528 (Value::Glob { val: lhs, .. }, rhs) => match rhs {
2529 Value::Bool { .. } => Some(Ordering::Greater),
2530 Value::Int { .. } => Some(Ordering::Greater),
2531 Value::Float { .. } => Some(Ordering::Greater),
2532 Value::String { val: rhs, .. } => lhs.partial_cmp(rhs),
2533 Value::Glob { val: rhs, .. } => lhs.partial_cmp(rhs),
2534 Value::Filesize { .. } => Some(Ordering::Less),
2535 Value::Duration { .. } => Some(Ordering::Less),
2536 Value::Date { .. } => Some(Ordering::Less),
2537 Value::Range { .. } => Some(Ordering::Less),
2538 Value::Record { .. } => Some(Ordering::Less),
2539 Value::List { .. } => Some(Ordering::Less),
2540 Value::Closure { .. } => Some(Ordering::Less),
2541 Value::Error { .. } => Some(Ordering::Less),
2542 Value::Binary { .. } => Some(Ordering::Less),
2543 Value::CellPath { .. } => Some(Ordering::Less),
2544 Value::Custom { .. } => Some(Ordering::Less),
2545 Value::Nothing { .. } => Some(Ordering::Less),
2546 },
2547 (Value::Filesize { val: lhs, .. }, rhs) => match rhs {
2548 Value::Bool { .. } => Some(Ordering::Greater),
2549 Value::Int { .. } => Some(Ordering::Greater),
2550 Value::Float { .. } => Some(Ordering::Greater),
2551 Value::String { .. } => Some(Ordering::Greater),
2552 Value::Glob { .. } => Some(Ordering::Greater),
2553 Value::Filesize { val: rhs, .. } => lhs.partial_cmp(rhs),
2554 Value::Duration { .. } => Some(Ordering::Less),
2555 Value::Date { .. } => Some(Ordering::Less),
2556 Value::Range { .. } => Some(Ordering::Less),
2557 Value::Record { .. } => Some(Ordering::Less),
2558 Value::List { .. } => Some(Ordering::Less),
2559 Value::Closure { .. } => Some(Ordering::Less),
2560 Value::Error { .. } => Some(Ordering::Less),
2561 Value::Binary { .. } => Some(Ordering::Less),
2562 Value::CellPath { .. } => Some(Ordering::Less),
2563 Value::Custom { .. } => Some(Ordering::Less),
2564 Value::Nothing { .. } => Some(Ordering::Less),
2565 },
2566 (Value::Duration { val: lhs, .. }, rhs) => match rhs {
2567 Value::Bool { .. } => Some(Ordering::Greater),
2568 Value::Int { .. } => Some(Ordering::Greater),
2569 Value::Float { .. } => Some(Ordering::Greater),
2570 Value::String { .. } => Some(Ordering::Greater),
2571 Value::Glob { .. } => Some(Ordering::Greater),
2572 Value::Filesize { .. } => Some(Ordering::Greater),
2573 Value::Duration { val: rhs, .. } => lhs.partial_cmp(rhs),
2574 Value::Date { .. } => Some(Ordering::Less),
2575 Value::Range { .. } => Some(Ordering::Less),
2576 Value::Record { .. } => Some(Ordering::Less),
2577 Value::List { .. } => Some(Ordering::Less),
2578 Value::Closure { .. } => Some(Ordering::Less),
2579 Value::Error { .. } => Some(Ordering::Less),
2580 Value::Binary { .. } => Some(Ordering::Less),
2581 Value::CellPath { .. } => Some(Ordering::Less),
2582 Value::Custom { .. } => Some(Ordering::Less),
2583 Value::Nothing { .. } => Some(Ordering::Less),
2584 },
2585 (Value::Date { val: lhs, .. }, rhs) => match rhs {
2586 Value::Bool { .. } => Some(Ordering::Greater),
2587 Value::Int { .. } => Some(Ordering::Greater),
2588 Value::Float { .. } => Some(Ordering::Greater),
2589 Value::String { .. } => Some(Ordering::Greater),
2590 Value::Glob { .. } => Some(Ordering::Greater),
2591 Value::Filesize { .. } => Some(Ordering::Greater),
2592 Value::Duration { .. } => Some(Ordering::Greater),
2593 Value::Date { val: rhs, .. } => lhs.partial_cmp(rhs),
2594 Value::Range { .. } => Some(Ordering::Less),
2595 Value::Record { .. } => Some(Ordering::Less),
2596 Value::List { .. } => Some(Ordering::Less),
2597 Value::Closure { .. } => Some(Ordering::Less),
2598 Value::Error { .. } => Some(Ordering::Less),
2599 Value::Binary { .. } => Some(Ordering::Less),
2600 Value::CellPath { .. } => Some(Ordering::Less),
2601 Value::Custom { .. } => Some(Ordering::Less),
2602 Value::Nothing { .. } => Some(Ordering::Less),
2603 },
2604 (Value::Range { val: lhs, .. }, rhs) => match rhs {
2605 Value::Bool { .. } => Some(Ordering::Greater),
2606 Value::Int { .. } => Some(Ordering::Greater),
2607 Value::Float { .. } => Some(Ordering::Greater),
2608 Value::String { .. } => Some(Ordering::Greater),
2609 Value::Glob { .. } => Some(Ordering::Greater),
2610 Value::Filesize { .. } => Some(Ordering::Greater),
2611 Value::Duration { .. } => Some(Ordering::Greater),
2612 Value::Date { .. } => Some(Ordering::Greater),
2613 Value::Range { val: rhs, .. } => lhs.partial_cmp(rhs),
2614 Value::Record { .. } => Some(Ordering::Less),
2615 Value::List { .. } => Some(Ordering::Less),
2616 Value::Closure { .. } => Some(Ordering::Less),
2617 Value::Error { .. } => Some(Ordering::Less),
2618 Value::Binary { .. } => Some(Ordering::Less),
2619 Value::CellPath { .. } => Some(Ordering::Less),
2620 Value::Custom { .. } => Some(Ordering::Less),
2621 Value::Nothing { .. } => Some(Ordering::Less),
2622 },
2623 (Value::Record { val: lhs, .. }, rhs) => match rhs {
2624 Value::Bool { .. } => Some(Ordering::Greater),
2625 Value::Int { .. } => Some(Ordering::Greater),
2626 Value::Float { .. } => Some(Ordering::Greater),
2627 Value::String { .. } => Some(Ordering::Greater),
2628 Value::Glob { .. } => Some(Ordering::Greater),
2629 Value::Filesize { .. } => Some(Ordering::Greater),
2630 Value::Duration { .. } => Some(Ordering::Greater),
2631 Value::Date { .. } => Some(Ordering::Greater),
2632 Value::Range { .. } => Some(Ordering::Greater),
2633 Value::Record { val: rhs, .. } => {
2634 let mut lhs = lhs.clone().into_owned();
2638 let mut rhs = rhs.clone().into_owned();
2639 lhs.sort_cols();
2640 rhs.sort_cols();
2641
2642 for (a, b) in lhs.columns().zip(rhs.columns()) {
2644 let result = a.partial_cmp(b);
2645 if result != Some(Ordering::Equal) {
2646 return result;
2647 }
2648 }
2649 for (a, b) in lhs.values().zip(rhs.values()) {
2651 let result = a.partial_cmp(b);
2652 if result != Some(Ordering::Equal) {
2653 return result;
2654 }
2655 }
2656 lhs.len().partial_cmp(&rhs.len())
2659 }
2660 Value::List { .. } => Some(Ordering::Less),
2661 Value::Closure { .. } => Some(Ordering::Less),
2662 Value::Error { .. } => Some(Ordering::Less),
2663 Value::Binary { .. } => Some(Ordering::Less),
2664 Value::CellPath { .. } => Some(Ordering::Less),
2665 Value::Custom { .. } => Some(Ordering::Less),
2666 Value::Nothing { .. } => Some(Ordering::Less),
2667 },
2668 (Value::List { vals: lhs, .. }, rhs) => match rhs {
2669 Value::Bool { .. } => Some(Ordering::Greater),
2670 Value::Int { .. } => Some(Ordering::Greater),
2671 Value::Float { .. } => Some(Ordering::Greater),
2672 Value::String { .. } => Some(Ordering::Greater),
2673 Value::Glob { .. } => Some(Ordering::Greater),
2674 Value::Filesize { .. } => Some(Ordering::Greater),
2675 Value::Duration { .. } => Some(Ordering::Greater),
2676 Value::Date { .. } => Some(Ordering::Greater),
2677 Value::Range { .. } => Some(Ordering::Greater),
2678 Value::Record { .. } => Some(Ordering::Greater),
2679 Value::List { vals: rhs, .. } => lhs.partial_cmp(rhs),
2680 Value::Closure { .. } => Some(Ordering::Less),
2681 Value::Error { .. } => Some(Ordering::Less),
2682 Value::Binary { .. } => Some(Ordering::Less),
2683 Value::CellPath { .. } => Some(Ordering::Less),
2684 Value::Custom { .. } => Some(Ordering::Less),
2685 Value::Nothing { .. } => Some(Ordering::Less),
2686 },
2687 (Value::Closure { val: lhs, .. }, rhs) => match rhs {
2688 Value::Bool { .. } => Some(Ordering::Greater),
2689 Value::Int { .. } => Some(Ordering::Greater),
2690 Value::Float { .. } => Some(Ordering::Greater),
2691 Value::String { .. } => Some(Ordering::Greater),
2692 Value::Glob { .. } => Some(Ordering::Greater),
2693 Value::Filesize { .. } => Some(Ordering::Greater),
2694 Value::Duration { .. } => Some(Ordering::Greater),
2695 Value::Date { .. } => Some(Ordering::Greater),
2696 Value::Range { .. } => Some(Ordering::Greater),
2697 Value::Record { .. } => Some(Ordering::Greater),
2698 Value::List { .. } => Some(Ordering::Greater),
2699 Value::Closure { val: rhs, .. } => lhs.block_id.partial_cmp(&rhs.block_id),
2700 Value::Error { .. } => Some(Ordering::Less),
2701 Value::Binary { .. } => Some(Ordering::Less),
2702 Value::CellPath { .. } => Some(Ordering::Less),
2703 Value::Custom { .. } => Some(Ordering::Less),
2704 Value::Nothing { .. } => Some(Ordering::Less),
2705 },
2706 (Value::Error { .. }, rhs) => match rhs {
2707 Value::Bool { .. } => Some(Ordering::Greater),
2708 Value::Int { .. } => Some(Ordering::Greater),
2709 Value::Float { .. } => Some(Ordering::Greater),
2710 Value::String { .. } => Some(Ordering::Greater),
2711 Value::Glob { .. } => Some(Ordering::Greater),
2712 Value::Filesize { .. } => Some(Ordering::Greater),
2713 Value::Duration { .. } => Some(Ordering::Greater),
2714 Value::Date { .. } => Some(Ordering::Greater),
2715 Value::Range { .. } => Some(Ordering::Greater),
2716 Value::Record { .. } => Some(Ordering::Greater),
2717 Value::List { .. } => Some(Ordering::Greater),
2718 Value::Closure { .. } => Some(Ordering::Greater),
2719 Value::Error { .. } => Some(Ordering::Equal),
2720 Value::Binary { .. } => Some(Ordering::Less),
2721 Value::CellPath { .. } => Some(Ordering::Less),
2722 Value::Custom { .. } => Some(Ordering::Less),
2723 Value::Nothing { .. } => Some(Ordering::Less),
2724 },
2725 (Value::Binary { val: lhs, .. }, rhs) => match rhs {
2726 Value::Bool { .. } => Some(Ordering::Greater),
2727 Value::Int { .. } => Some(Ordering::Greater),
2728 Value::Float { .. } => Some(Ordering::Greater),
2729 Value::String { .. } => Some(Ordering::Greater),
2730 Value::Glob { .. } => Some(Ordering::Greater),
2731 Value::Filesize { .. } => Some(Ordering::Greater),
2732 Value::Duration { .. } => Some(Ordering::Greater),
2733 Value::Date { .. } => Some(Ordering::Greater),
2734 Value::Range { .. } => Some(Ordering::Greater),
2735 Value::Record { .. } => Some(Ordering::Greater),
2736 Value::List { .. } => Some(Ordering::Greater),
2737 Value::Closure { .. } => Some(Ordering::Greater),
2738 Value::Error { .. } => Some(Ordering::Greater),
2739 Value::Binary { val: rhs, .. } => lhs.partial_cmp(rhs),
2740 Value::CellPath { .. } => Some(Ordering::Less),
2741 Value::Custom { .. } => Some(Ordering::Less),
2742 Value::Nothing { .. } => Some(Ordering::Less),
2743 },
2744 (Value::CellPath { val: lhs, .. }, rhs) => match rhs {
2745 Value::Bool { .. } => Some(Ordering::Greater),
2746 Value::Int { .. } => Some(Ordering::Greater),
2747 Value::Float { .. } => Some(Ordering::Greater),
2748 Value::String { .. } => Some(Ordering::Greater),
2749 Value::Glob { .. } => Some(Ordering::Greater),
2750 Value::Filesize { .. } => Some(Ordering::Greater),
2751 Value::Duration { .. } => Some(Ordering::Greater),
2752 Value::Date { .. } => Some(Ordering::Greater),
2753 Value::Range { .. } => Some(Ordering::Greater),
2754 Value::Record { .. } => Some(Ordering::Greater),
2755 Value::List { .. } => Some(Ordering::Greater),
2756 Value::Closure { .. } => Some(Ordering::Greater),
2757 Value::Error { .. } => Some(Ordering::Greater),
2758 Value::Binary { .. } => Some(Ordering::Greater),
2759 Value::CellPath { val: rhs, .. } => lhs.partial_cmp(rhs),
2760 Value::Custom { .. } => Some(Ordering::Less),
2761 Value::Nothing { .. } => Some(Ordering::Less),
2762 },
2763 (Value::Custom { val: lhs, .. }, rhs) => lhs.partial_cmp(rhs),
2764 (Value::Nothing { .. }, rhs) => match rhs {
2765 Value::Bool { .. } => Some(Ordering::Greater),
2766 Value::Int { .. } => Some(Ordering::Greater),
2767 Value::Float { .. } => Some(Ordering::Greater),
2768 Value::String { .. } => Some(Ordering::Greater),
2769 Value::Glob { .. } => Some(Ordering::Greater),
2770 Value::Filesize { .. } => Some(Ordering::Greater),
2771 Value::Duration { .. } => Some(Ordering::Greater),
2772 Value::Date { .. } => Some(Ordering::Greater),
2773 Value::Range { .. } => Some(Ordering::Greater),
2774 Value::Record { .. } => Some(Ordering::Greater),
2775 Value::List { .. } => Some(Ordering::Greater),
2776 Value::Closure { .. } => Some(Ordering::Greater),
2777 Value::Error { .. } => Some(Ordering::Greater),
2778 Value::Binary { .. } => Some(Ordering::Greater),
2779 Value::CellPath { .. } => Some(Ordering::Greater),
2780 Value::Custom { .. } => Some(Ordering::Greater),
2781 Value::Nothing { .. } => Some(Ordering::Equal),
2782 },
2783 }
2784 }
2785}
2786
2787impl PartialEq for Value {
2788 fn eq(&self, other: &Self) -> bool {
2789 self.partial_cmp(other).is_some_and(Ordering::is_eq)
2790 }
2791}
2792
2793pub const HASH_ITEM_LIMIT: usize = 5;
2799
2800pub fn hash_f64<H: Hasher>(val: f64, state: &mut H) {
2805 let val = if val == 0.0 {
2806 0.0
2807 } else if val.is_nan() {
2808 f64::NAN
2809 } else {
2810 val
2811 };
2812 val.to_bits().hash(state);
2813}
2814
2815impl Hash for Value {
2825 fn hash<H: Hasher>(&self, state: &mut H) {
2826 std::mem::discriminant(self).hash(state);
2827 match self {
2828 Value::Bool { val, .. } => val.hash(state),
2829 Value::Int { val, .. } => val.hash(state),
2830 Value::Float { val, .. } => hash_f64(*val, state),
2831 Value::String { val, .. } => val.hash(state),
2832 Value::Glob { val, no_expand, .. } => {
2833 val.hash(state);
2834 no_expand.hash(state);
2835 }
2836 Value::Filesize { val, .. } => val.hash(state),
2837 Value::Duration { val, .. } => val.hash(state),
2838 Value::Date { val, .. } => val.hash(state),
2840 Value::Range { val, .. } => {
2842 std::mem::discriminant(&**val).hash(state);
2843 match **val {
2844 Range::IntRange(range) => range.hash(state),
2845 Range::FloatRange(range) => range.hash(state),
2846 }
2847 }
2848 Value::Record { val, .. } => val.hash(state),
2849 Value::List { vals, .. } => {
2850 vals.len().hash(state);
2851 let tail_start = vals
2855 .len()
2856 .saturating_sub(HASH_ITEM_LIMIT)
2857 .max(HASH_ITEM_LIMIT);
2858 for val in vals
2859 .iter()
2860 .take(HASH_ITEM_LIMIT)
2861 .chain(vals.iter().skip(tail_start))
2862 {
2863 val.hash(state);
2864 }
2865 }
2866 Value::Closure { val, .. } => val.hash(state),
2867 Value::Error { .. } => {}
2869 Value::Binary { val, .. } => val.hash(state),
2870 Value::CellPath { val, .. } => val.hash(state),
2871 Value::Custom { val, .. } => {
2873 val.type_name().hash(state);
2874 val.hash_value(state);
2875 }
2876 Value::Nothing { .. } => {}
2877 }
2878 }
2879}
2880
2881impl Value {
2882 pub fn strict_eq(&self, other: &Value) -> bool {
2904 match (self, other) {
2905 (Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => lhs == rhs,
2906 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => lhs == rhs,
2907 (Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
2908 lhs == rhs || (lhs.is_nan() && rhs.is_nan())
2909 }
2910 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
2911 (
2912 Value::Glob {
2913 val: lhs,
2914 no_expand: lhs_no_expand,
2915 ..
2916 },
2917 Value::Glob {
2918 val: rhs,
2919 no_expand: rhs_no_expand,
2920 ..
2921 },
2922 ) => lhs == rhs && lhs_no_expand == rhs_no_expand,
2923 (Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => lhs == rhs,
2924 (Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => lhs == rhs,
2925 (Value::Date { val: lhs, .. }, Value::Date { val: rhs, .. }) => lhs == rhs,
2926 (Value::Range { val: lhs, .. }, Value::Range { val: rhs, .. }) => {
2927 match (**lhs, **rhs) {
2928 (Range::IntRange(lhs), Range::IntRange(rhs)) => lhs == rhs,
2929 (Range::FloatRange(lhs), Range::FloatRange(rhs)) => lhs == rhs,
2930 _ => false,
2931 }
2932 }
2933 (Value::Record { val: lhs, .. }, Value::Record { val: rhs, .. }) => {
2934 lhs.len() == rhs.len()
2935 && if lhs.columns().eq(rhs.columns()) {
2936 lhs.values()
2937 .zip(rhs.values())
2938 .all(|(lhs, rhs)| lhs.strict_eq(rhs))
2939 } else {
2940 let mut lhs: Vec<_> = lhs.iter().collect();
2943 let mut rhs: Vec<_> = rhs.iter().collect();
2944 lhs.sort_by_key(|(key, _)| *key);
2945 rhs.sort_by_key(|(key, _)| *key);
2946 lhs.into_iter()
2947 .zip(rhs)
2948 .all(|((lhs_key, lhs), (rhs_key, rhs))| {
2949 lhs_key == rhs_key && lhs.strict_eq(rhs)
2950 })
2951 }
2952 }
2953 (Value::List { vals: lhs, .. }, Value::List { vals: rhs, .. }) => {
2954 lhs.len() == rhs.len() && lhs.iter().zip(rhs).all(|(lhs, rhs)| lhs.strict_eq(rhs))
2955 }
2956 (Value::Closure { val: lhs, .. }, Value::Closure { val: rhs, .. }) => {
2957 lhs.block_id == rhs.block_id
2958 && lhs.captures.len() == rhs.captures.len()
2959 && lhs.captures.iter().zip(&rhs.captures).all(
2960 |((lhs_var, lhs), (rhs_var, rhs))| lhs_var == rhs_var && lhs.strict_eq(rhs),
2961 )
2962 }
2963 (Value::Error { error: lhs, .. }, Value::Error { error: rhs, .. }) => lhs == rhs,
2964 (Value::Binary { val: lhs, .. }, Value::Binary { val: rhs, .. }) => lhs == rhs,
2965 (Value::CellPath { val: lhs, .. }, Value::CellPath { val: rhs, .. }) => lhs == rhs,
2966 (Value::Custom { val: lhs, .. }, Value::Custom { val: rhs, .. }) => {
2967 lhs.type_name() == rhs.type_name()
2968 && lhs.partial_cmp(other).is_some_and(Ordering::is_eq)
2969 }
2970 (Value::Nothing { .. }, Value::Nothing { .. }) => true,
2971 (
2974 Value::Bool { .. }
2975 | Value::Int { .. }
2976 | Value::Float { .. }
2977 | Value::String { .. }
2978 | Value::Glob { .. }
2979 | Value::Filesize { .. }
2980 | Value::Duration { .. }
2981 | Value::Date { .. }
2982 | Value::Range { .. }
2983 | Value::Record { .. }
2984 | Value::List { .. }
2985 | Value::Closure { .. }
2986 | Value::Error { .. }
2987 | Value::Binary { .. }
2988 | Value::CellPath { .. }
2989 | Value::Custom { .. }
2990 | Value::Nothing { .. },
2991 _,
2992 ) => false,
2993 }
2994 }
2995}
2996
2997fn checked_duration_operation<F>(a: i64, b: i64, op: F, span: Span) -> Result<Value, ShellError>
2998where
2999 F: Fn(i64, i64) -> Option<i64>,
3000{
3001 if let Some(val) = op(a, b) {
3002 Ok(Value::duration(val, span))
3003 } else {
3004 Err(ShellError::OperatorOverflow {
3005 msg: "operation overflowed".to_owned(),
3006 span,
3007 help: None,
3008 })
3009 }
3010}
3011
3012impl Value {
3013 pub fn add(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3014 match (self, rhs) {
3015 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3016 if let Some(val) = lhs.checked_add(*rhs) {
3017 Ok(Value::int(val, span))
3018 } else {
3019 Err(ShellError::OperatorOverflow {
3020 msg: "add operation overflowed".into(),
3021 span,
3022 help: Some("Consider using floating point values for increased range by promoting operand with 'into float'. Note: float has reduced precision!".into()),
3023 })
3024 }
3025 }
3026 (Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3027 Ok(Value::float(*lhs as f64 + *rhs, span))
3028 }
3029 (Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3030 Ok(Value::float(*lhs + *rhs as f64, span))
3031 }
3032 (Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3033 Ok(Value::float(lhs + rhs, span))
3034 }
3035 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
3036 Ok(Value::string(lhs.to_string() + rhs, span))
3037 }
3038 (Value::Duration { val: lhs, .. }, Value::Date { val: rhs, .. }) => {
3039 if let Some(val) = rhs.checked_add_signed(chrono::Duration::nanoseconds(*lhs)) {
3040 Ok(Value::date(val, span))
3041 } else {
3042 Err(ShellError::OperatorOverflow {
3043 msg: "addition operation overflowed".into(),
3044 span,
3045 help: None,
3046 })
3047 }
3048 }
3049 (Value::Date { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3050 if let Some(val) = lhs.checked_add_signed(chrono::Duration::nanoseconds(*rhs)) {
3051 Ok(Value::date(val, span))
3052 } else {
3053 Err(ShellError::OperatorOverflow {
3054 msg: "addition operation overflowed".into(),
3055 span,
3056 help: None,
3057 })
3058 }
3059 }
3060 (Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3061 checked_duration_operation(*lhs, *rhs, i64::checked_add, span)
3062 }
3063 (Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
3064 if let Some(val) = *lhs + *rhs {
3065 Ok(Value::filesize(val, span))
3066 } else {
3067 Err(ShellError::OperatorOverflow {
3068 msg: "add operation overflowed".into(),
3069 span,
3070 help: None,
3071 })
3072 }
3073 }
3074 (Value::Custom { val: lhs, .. }, rhs) => {
3075 lhs.operation(self.span(), Operator::Math(Math::Add), op, rhs)
3076 }
3077 _ => Err(operator_type_error(
3078 Operator::Math(Math::Add),
3079 op,
3080 self,
3081 rhs,
3082 |val| {
3083 matches!(
3084 val,
3085 Value::Int { .. }
3086 | Value::Float { .. }
3087 | Value::String { .. }
3088 | Value::Date { .. }
3089 | Value::Duration { .. }
3090 | Value::Filesize { .. },
3091 )
3092 },
3093 )),
3094 }
3095 }
3096
3097 pub fn sub(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3098 match (self, rhs) {
3099 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3100 if let Some(val) = lhs.checked_sub(*rhs) {
3101 Ok(Value::int(val, span))
3102 } else {
3103 Err(ShellError::OperatorOverflow {
3104 msg: "subtraction operation overflowed".into(),
3105 span,
3106 help: Some("Consider using floating point values for increased range by promoting operand with 'into float'. Note: float has reduced precision!".into()),
3107 })
3108 }
3109 }
3110 (Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3111 Ok(Value::float(*lhs as f64 - *rhs, span))
3112 }
3113 (Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3114 Ok(Value::float(*lhs - *rhs as f64, span))
3115 }
3116 (Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3117 Ok(Value::float(lhs - rhs, span))
3118 }
3119 (Value::Date { val: lhs, .. }, Value::Date { val: rhs, .. }) => {
3120 let result = lhs.signed_duration_since(*rhs);
3121 if let Some(v) = result.num_nanoseconds() {
3122 Ok(Value::duration(v, span))
3123 } else {
3124 Err(ShellError::OperatorOverflow {
3125 msg: "subtraction operation overflowed".into(),
3126 span,
3127 help: None,
3128 })
3129 }
3130 }
3131 (Value::Date { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3132 match lhs.checked_sub_signed(chrono::Duration::nanoseconds(*rhs)) {
3133 Some(val) => Ok(Value::date(val, span)),
3134 _ => Err(ShellError::OperatorOverflow {
3135 msg: "subtraction operation overflowed".into(),
3136 span,
3137 help: None,
3138 }),
3139 }
3140 }
3141 (Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3142 checked_duration_operation(*lhs, *rhs, i64::checked_sub, span)
3143 }
3144 (Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
3145 if let Some(val) = *lhs - *rhs {
3146 Ok(Value::filesize(val, span))
3147 } else {
3148 Err(ShellError::OperatorOverflow {
3149 msg: "add operation overflowed".into(),
3150 span,
3151 help: None,
3152 })
3153 }
3154 }
3155 (Value::Custom { val: lhs, .. }, rhs) => {
3156 lhs.operation(self.span(), Operator::Math(Math::Subtract), op, rhs)
3157 }
3158 _ => Err(operator_type_error(
3159 Operator::Math(Math::Subtract),
3160 op,
3161 self,
3162 rhs,
3163 |val| {
3164 matches!(
3165 val,
3166 Value::Int { .. }
3167 | Value::Float { .. }
3168 | Value::Date { .. }
3169 | Value::Duration { .. }
3170 | Value::Filesize { .. },
3171 )
3172 },
3173 )),
3174 }
3175 }
3176
3177 pub fn mul(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3178 match (self, rhs) {
3179 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3180 if let Some(val) = lhs.checked_mul(*rhs) {
3181 Ok(Value::int(val, span))
3182 } else {
3183 Err(ShellError::OperatorOverflow {
3184 msg: "multiply operation overflowed".into(),
3185 span,
3186 help: Some("Consider using floating point values for increased range by promoting operand with 'into float'. Note: float has reduced precision!".into()),
3187 })
3188 }
3189 }
3190 (Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3191 Ok(Value::float(*lhs as f64 * *rhs, span))
3192 }
3193 (Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3194 Ok(Value::float(*lhs * *rhs as f64, span))
3195 }
3196 (Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3197 Ok(Value::float(lhs * rhs, span))
3198 }
3199 (Value::Int { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
3200 if let Some(val) = *lhs * *rhs {
3201 Ok(Value::filesize(val, span))
3202 } else {
3203 Err(ShellError::OperatorOverflow {
3204 msg: "multiply operation overflowed".into(),
3205 span,
3206 help: None,
3207 })
3208 }
3209 }
3210 (Value::Filesize { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3211 if let Some(val) = *lhs * *rhs {
3212 Ok(Value::filesize(val, span))
3213 } else {
3214 Err(ShellError::OperatorOverflow {
3215 msg: "multiply operation overflowed".into(),
3216 span,
3217 help: None,
3218 })
3219 }
3220 }
3221 (Value::Float { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
3222 if let Some(val) = *lhs * *rhs {
3223 Ok(Value::filesize(val, span))
3224 } else {
3225 Err(ShellError::OperatorOverflow {
3226 msg: "multiply operation overflowed".into(),
3227 span,
3228 help: None,
3229 })
3230 }
3231 }
3232 (Value::Filesize { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3233 if let Some(val) = *lhs * *rhs {
3234 Ok(Value::filesize(val, span))
3235 } else {
3236 Err(ShellError::OperatorOverflow {
3237 msg: "multiply operation overflowed".into(),
3238 span,
3239 help: None,
3240 })
3241 }
3242 }
3243 (Value::Int { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3244 checked_duration_operation(*lhs, *rhs, i64::checked_mul, span)
3245 }
3246 (Value::Duration { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3247 checked_duration_operation(*lhs, *rhs, i64::checked_mul, span)
3248 }
3249 (Value::Duration { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3250 Ok(Value::duration((*lhs as f64 * *rhs) as i64, span))
3251 }
3252 (Value::Float { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3253 Ok(Value::duration((*lhs * *rhs as f64) as i64, span))
3254 }
3255 (Value::Custom { val: lhs, .. }, rhs) => {
3256 lhs.operation(self.span(), Operator::Math(Math::Multiply), op, rhs)
3257 }
3258 _ => Err(operator_type_error(
3259 Operator::Math(Math::Multiply),
3260 op,
3261 self,
3262 rhs,
3263 |val| {
3264 matches!(
3265 val,
3266 Value::Int { .. }
3267 | Value::Float { .. }
3268 | Value::Duration { .. }
3269 | Value::Filesize { .. },
3270 )
3271 },
3272 )),
3273 }
3274 }
3275
3276 pub fn div(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3277 match (self, rhs) {
3278 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3279 if *rhs == 0 {
3280 Err(ShellError::DivisionByZero { span: op })
3281 } else {
3282 Ok(Value::float(*lhs as f64 / *rhs as f64, span))
3283 }
3284 }
3285 (Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3286 if *rhs != 0.0 {
3287 Ok(Value::float(*lhs as f64 / *rhs, span))
3288 } else {
3289 Err(ShellError::DivisionByZero { span: op })
3290 }
3291 }
3292 (Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3293 if *rhs != 0 {
3294 Ok(Value::float(*lhs / *rhs as f64, span))
3295 } else {
3296 Err(ShellError::DivisionByZero { span: op })
3297 }
3298 }
3299 (Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3300 if *rhs != 0.0 {
3301 Ok(Value::float(lhs / rhs, span))
3302 } else {
3303 Err(ShellError::DivisionByZero { span: op })
3304 }
3305 }
3306 (Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
3307 if *rhs == Filesize::ZERO {
3308 Err(ShellError::DivisionByZero { span: op })
3309 } else {
3310 Ok(Value::float(lhs.get() as f64 / rhs.get() as f64, span))
3311 }
3312 }
3313 (Value::Filesize { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3314 if let Some(val) = lhs.get().checked_div(*rhs) {
3315 Ok(Value::filesize(val, span))
3316 } else if *rhs == 0 {
3317 Err(ShellError::DivisionByZero { span: op })
3318 } else {
3319 Err(ShellError::OperatorOverflow {
3320 msg: "division operation overflowed".into(),
3321 span,
3322 help: None,
3323 })
3324 }
3325 }
3326 (Value::Filesize { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3327 if *rhs != 0.0 {
3328 if let Ok(val) = Filesize::try_from(lhs.get() as f64 / rhs) {
3329 Ok(Value::filesize(val, span))
3330 } else {
3331 Err(ShellError::OperatorOverflow {
3332 msg: "division operation overflowed".into(),
3333 span,
3334 help: None,
3335 })
3336 }
3337 } else {
3338 Err(ShellError::DivisionByZero { span: op })
3339 }
3340 }
3341 (Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3342 if *rhs == 0 {
3343 Err(ShellError::DivisionByZero { span: op })
3344 } else {
3345 Ok(Value::float(*lhs as f64 / *rhs as f64, span))
3346 }
3347 }
3348 (Value::Duration { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3349 if let Some(val) = lhs.checked_div(*rhs) {
3350 Ok(Value::duration(val, span))
3351 } else if *rhs == 0 {
3352 Err(ShellError::DivisionByZero { span: op })
3353 } else {
3354 Err(ShellError::OperatorOverflow {
3355 msg: "division operation overflowed".into(),
3356 span,
3357 help: None,
3358 })
3359 }
3360 }
3361 (Value::Duration { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3362 if *rhs != 0.0 {
3363 let val = *lhs as f64 / rhs;
3364 if i64::MIN as f64 <= val && val <= i64::MAX as f64 {
3365 Ok(Value::duration(val as i64, span))
3366 } else {
3367 Err(ShellError::OperatorOverflow {
3368 msg: "division operation overflowed".into(),
3369 span,
3370 help: None,
3371 })
3372 }
3373 } else {
3374 Err(ShellError::DivisionByZero { span: op })
3375 }
3376 }
3377 (Value::Custom { val: lhs, .. }, rhs) => {
3378 lhs.operation(self.span(), Operator::Math(Math::Divide), op, rhs)
3379 }
3380 _ => Err(operator_type_error(
3381 Operator::Math(Math::Divide),
3382 op,
3383 self,
3384 rhs,
3385 |val| {
3386 matches!(
3387 val,
3388 Value::Int { .. }
3389 | Value::Float { .. }
3390 | Value::Duration { .. }
3391 | Value::Filesize { .. },
3392 )
3393 },
3394 )),
3395 }
3396 }
3397
3398 pub fn floor_div(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3399 fn checked_div_floor_i64(dividend: i64, divisor: i64) -> Option<i64> {
3401 let quotient = dividend.checked_div(divisor)?;
3402 let remainder = dividend.checked_rem(divisor)?;
3403 if (remainder > 0 && divisor < 0) || (remainder < 0 && divisor > 0) {
3404 Some(quotient - 1)
3410 } else {
3411 Some(quotient)
3412 }
3413 }
3414
3415 fn checked_div_floor_f64(dividend: f64, divisor: f64) -> Option<f64> {
3416 if divisor == 0.0 {
3417 None
3418 } else {
3419 Some((dividend / divisor).floor())
3420 }
3421 }
3422
3423 match (self, rhs) {
3424 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3425 if let Some(val) = checked_div_floor_i64(*lhs, *rhs) {
3426 Ok(Value::int(val, span))
3427 } else if *rhs == 0 {
3428 Err(ShellError::DivisionByZero { span: op })
3429 } else {
3430 Err(ShellError::OperatorOverflow {
3431 msg: "division operation overflowed".into(),
3432 span,
3433 help: None,
3434 })
3435 }
3436 }
3437 (Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3438 if let Some(val) = checked_div_floor_f64(*lhs as f64, *rhs) {
3439 Ok(Value::float(val, span))
3440 } else {
3441 Err(ShellError::DivisionByZero { span: op })
3442 }
3443 }
3444 (Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3445 if let Some(val) = checked_div_floor_f64(*lhs, *rhs as f64) {
3446 Ok(Value::float(val, span))
3447 } else {
3448 Err(ShellError::DivisionByZero { span: op })
3449 }
3450 }
3451 (Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3452 if let Some(val) = checked_div_floor_f64(*lhs, *rhs) {
3453 Ok(Value::float(val, span))
3454 } else {
3455 Err(ShellError::DivisionByZero { span: op })
3456 }
3457 }
3458 (Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
3459 if let Some(val) = checked_div_floor_i64(lhs.get(), rhs.get()) {
3460 Ok(Value::int(val, span))
3461 } else if *rhs == Filesize::ZERO {
3462 Err(ShellError::DivisionByZero { span: op })
3463 } else {
3464 Err(ShellError::OperatorOverflow {
3465 msg: "division operation overflowed".into(),
3466 span,
3467 help: None,
3468 })
3469 }
3470 }
3471 (Value::Filesize { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3472 if let Some(val) = checked_div_floor_i64(lhs.get(), *rhs) {
3473 Ok(Value::filesize(val, span))
3474 } else if *rhs == 0 {
3475 Err(ShellError::DivisionByZero { span: op })
3476 } else {
3477 Err(ShellError::OperatorOverflow {
3478 msg: "division operation overflowed".into(),
3479 span,
3480 help: None,
3481 })
3482 }
3483 }
3484 (Value::Filesize { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3485 if let Some(val) = checked_div_floor_f64(lhs.get() as f64, *rhs) {
3486 if let Ok(val) = Filesize::try_from(val) {
3487 Ok(Value::filesize(val, span))
3488 } else {
3489 Err(ShellError::OperatorOverflow {
3490 msg: "division operation overflowed".into(),
3491 span,
3492 help: None,
3493 })
3494 }
3495 } else {
3496 Err(ShellError::DivisionByZero { span: op })
3497 }
3498 }
3499 (Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3500 if let Some(val) = checked_div_floor_i64(*lhs, *rhs) {
3501 Ok(Value::int(val, span))
3502 } else if *rhs == 0 {
3503 Err(ShellError::DivisionByZero { span: op })
3504 } else {
3505 Err(ShellError::OperatorOverflow {
3506 msg: "division operation overflowed".into(),
3507 span,
3508 help: None,
3509 })
3510 }
3511 }
3512 (Value::Duration { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3513 if let Some(val) = checked_div_floor_i64(*lhs, *rhs) {
3514 Ok(Value::duration(val, span))
3515 } else if *rhs == 0 {
3516 Err(ShellError::DivisionByZero { span: op })
3517 } else {
3518 Err(ShellError::OperatorOverflow {
3519 msg: "division operation overflowed".into(),
3520 span,
3521 help: None,
3522 })
3523 }
3524 }
3525 (Value::Duration { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3526 if let Some(val) = checked_div_floor_f64(*lhs as f64, *rhs) {
3527 if i64::MIN as f64 <= val && val <= i64::MAX as f64 {
3528 Ok(Value::duration(val as i64, span))
3529 } else {
3530 Err(ShellError::OperatorOverflow {
3531 msg: "division operation overflowed".into(),
3532 span,
3533 help: None,
3534 })
3535 }
3536 } else {
3537 Err(ShellError::DivisionByZero { span: op })
3538 }
3539 }
3540 (Value::Custom { val: lhs, .. }, rhs) => {
3541 lhs.operation(self.span(), Operator::Math(Math::FloorDivide), op, rhs)
3542 }
3543 _ => Err(operator_type_error(
3544 Operator::Math(Math::FloorDivide),
3545 op,
3546 self,
3547 rhs,
3548 |val| {
3549 matches!(
3550 val,
3551 Value::Int { .. }
3552 | Value::Float { .. }
3553 | Value::Duration { .. }
3554 | Value::Filesize { .. },
3555 )
3556 },
3557 )),
3558 }
3559 }
3560
3561 pub fn modulo(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3562 fn checked_mod_i64(dividend: i64, divisor: i64) -> Option<i64> {
3565 if divisor == -1 {
3568 return Some(0);
3569 }
3570 let remainder = dividend.checked_rem(divisor)?;
3571 if (remainder > 0 && divisor < 0) || (remainder < 0 && divisor > 0) {
3572 Some(remainder + divisor)
3575 } else {
3576 Some(remainder)
3577 }
3578 }
3579
3580 fn checked_mod_f64(dividend: f64, divisor: f64) -> Option<f64> {
3581 if divisor == 0.0 {
3582 None
3583 } else {
3584 let remainder = dividend % divisor;
3585 if (remainder > 0.0 && divisor < 0.0) || (remainder < 0.0 && divisor > 0.0) {
3586 Some(remainder + divisor)
3587 } else {
3588 Some(remainder)
3589 }
3590 }
3591 }
3592
3593 match (self, rhs) {
3594 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3595 if let Some(val) = checked_mod_i64(*lhs, *rhs) {
3596 Ok(Value::int(val, span))
3597 } else {
3598 Err(ShellError::DivisionByZero { span: op })
3599 }
3600 }
3601 (Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3602 if let Some(val) = checked_mod_f64(*lhs as f64, *rhs) {
3603 Ok(Value::float(val, span))
3604 } else {
3605 Err(ShellError::DivisionByZero { span: op })
3606 }
3607 }
3608 (Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3609 if let Some(val) = checked_mod_f64(*lhs, *rhs as f64) {
3610 Ok(Value::float(val, span))
3611 } else {
3612 Err(ShellError::DivisionByZero { span: op })
3613 }
3614 }
3615 (Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3616 if let Some(val) = checked_mod_f64(*lhs, *rhs) {
3617 Ok(Value::float(val, span))
3618 } else {
3619 Err(ShellError::DivisionByZero { span: op })
3620 }
3621 }
3622 (Value::Filesize { val: lhs, .. }, Value::Filesize { val: rhs, .. }) => {
3623 if let Some(val) = checked_mod_i64(lhs.get(), rhs.get()) {
3624 Ok(Value::filesize(val, span))
3625 } else {
3626 Err(ShellError::DivisionByZero { span: op })
3627 }
3628 }
3629 (Value::Filesize { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3630 if let Some(val) = checked_mod_i64(lhs.get(), *rhs) {
3631 Ok(Value::filesize(val, span))
3632 } else {
3633 Err(ShellError::DivisionByZero { span: op })
3634 }
3635 }
3636 (Value::Filesize { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3637 if let Some(val) = checked_mod_f64(lhs.get() as f64, *rhs) {
3638 if let Ok(val) = Filesize::try_from(val) {
3639 Ok(Value::filesize(val, span))
3640 } else {
3641 Err(ShellError::OperatorOverflow {
3642 msg: "modulo operation overflowed".into(),
3643 span,
3644 help: None,
3645 })
3646 }
3647 } else {
3648 Err(ShellError::DivisionByZero { span: op })
3649 }
3650 }
3651 (Value::Duration { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
3652 if let Some(val) = checked_mod_i64(*lhs, *rhs) {
3653 Ok(Value::duration(val, span))
3654 } else {
3655 Err(ShellError::DivisionByZero { span: op })
3656 }
3657 }
3658 (Value::Duration { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3659 if let Some(val) = checked_mod_i64(*lhs, *rhs) {
3660 Ok(Value::duration(val, span))
3661 } else {
3662 Err(ShellError::DivisionByZero { span: op })
3663 }
3664 }
3665 (Value::Duration { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3666 if let Some(val) = checked_mod_f64(*lhs as f64, *rhs) {
3667 if i64::MIN as f64 <= val && val <= i64::MAX as f64 {
3668 Ok(Value::duration(val as i64, span))
3669 } else {
3670 Err(ShellError::OperatorOverflow {
3671 msg: "division operation overflowed".into(),
3672 span,
3673 help: None,
3674 })
3675 }
3676 } else {
3677 Err(ShellError::DivisionByZero { span: op })
3678 }
3679 }
3680 (Value::Custom { val: lhs, .. }, rhs) => {
3681 lhs.operation(span, Operator::Math(Math::Modulo), op, rhs)
3682 }
3683 _ => Err(operator_type_error(
3684 Operator::Math(Math::Modulo),
3685 op,
3686 self,
3687 rhs,
3688 |val| {
3689 matches!(
3690 val,
3691 Value::Int { .. }
3692 | Value::Float { .. }
3693 | Value::Duration { .. }
3694 | Value::Filesize { .. },
3695 )
3696 },
3697 )),
3698 }
3699 }
3700
3701 pub fn pow(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3702 match (self, rhs) {
3703 (Value::Int { val: lhs, .. }, Value::Int { val: rhsv, .. }) => {
3704 if *rhsv < 0 {
3705 return Err(ShellError::IncorrectValue {
3706 msg: "Negative exponent for integer power is unsupported; use floats instead.".into(),
3707 val_span: rhs.span(),
3708 call_span: op,
3709 });
3710 }
3711
3712 if let Some(val) = lhs.checked_pow(*rhsv as u32) {
3713 Ok(Value::int(val, span))
3714 } else {
3715 Err(ShellError::OperatorOverflow {
3716 msg: "pow operation overflowed".into(),
3717 span,
3718 help: Some("Consider using floating point values for increased range by promoting operand with 'into float'. Note: float has reduced precision!".into()),
3719 })
3720 }
3721 }
3722 (Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3723 Ok(Value::float((*lhs as f64).powf(*rhs), span))
3724 }
3725 (Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
3726 Ok(Value::float(lhs.powf(*rhs as f64), span))
3727 }
3728 (Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
3729 Ok(Value::float(lhs.powf(*rhs), span))
3730 }
3731 (Value::Custom { val: lhs, .. }, rhs) => {
3732 lhs.operation(span, Operator::Math(Math::Pow), op, rhs)
3733 }
3734 _ => Err(operator_type_error(
3735 Operator::Math(Math::Pow),
3736 op,
3737 self,
3738 rhs,
3739 |val| matches!(val, Value::Int { .. } | Value::Float { .. }),
3740 )),
3741 }
3742 }
3743
3744 pub fn concat(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3745 match (self, rhs) {
3746 (Value::List { vals: lhs, .. }, Value::List { vals: rhs, .. }) => {
3747 if lhs.is_empty() {
3748 Ok(Value::list_shared(rhs.clone(), span))
3749 } else if rhs.is_empty() {
3750 Ok(Value::list_shared(lhs.clone(), span))
3751 } else {
3752 let mut new_vals = Vec::with_capacity(lhs.len() + rhs.len());
3753 new_vals.extend_from_slice(lhs);
3754 new_vals.extend_from_slice(rhs);
3755 Ok(Value::list(new_vals, span))
3756 }
3757 }
3758 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
3759 Ok(Value::string([lhs.as_str(), rhs.as_str()].join(""), span))
3760 }
3761 (Value::Binary { val: lhs, .. }, Value::Binary { val: rhs, .. }) => Ok(Value::binary(
3762 [lhs.as_slice(), rhs.as_slice()].concat(),
3763 span,
3764 )),
3765 (Value::Custom { val: lhs, .. }, rhs) => {
3766 lhs.operation(self.span(), Operator::Math(Math::Concatenate), op, rhs)
3767 }
3768 _ => {
3769 let help = if matches!(self, Value::List { .. })
3770 || matches!(rhs, Value::List { .. })
3771 {
3772 Some(
3773 "if you meant to append a value to a list or a record to a table, use the `append` command or wrap the value in a list. For example: `$list ++ $value` should be `$list ++ [$value]` or `$list | append $value`.",
3774 )
3775 } else {
3776 None
3777 };
3778 let is_supported = |val: &Value| {
3779 matches!(
3780 val,
3781 Value::List { .. }
3782 | Value::String { .. }
3783 | Value::Binary { .. }
3784 | Value::Custom { .. }
3785 )
3786 };
3787 Err(match (is_supported(self), is_supported(rhs)) {
3788 (true, true) => ShellError::OperatorIncompatibleTypes {
3789 op: Operator::Math(Math::Concatenate),
3790 lhs: self.get_type(),
3791 rhs: rhs.get_type(),
3792 op_span: op,
3793 lhs_span: self.span(),
3794 rhs_span: rhs.span(),
3795 help,
3796 },
3797 (true, false) => ShellError::OperatorUnsupportedType {
3798 op: Operator::Math(Math::Concatenate),
3799 unsupported: rhs.get_type(),
3800 op_span: op,
3801 unsupported_span: rhs.span(),
3802 help,
3803 },
3804 (false, _) => ShellError::OperatorUnsupportedType {
3805 op: Operator::Math(Math::Concatenate),
3806 unsupported: self.get_type(),
3807 op_span: op,
3808 unsupported_span: self.span(),
3809 help,
3810 },
3811 })
3812 }
3813 }
3814 }
3815
3816 pub fn lt(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3817 if let (Value::Custom { val: lhs, .. }, rhs) = (self, rhs) {
3818 return lhs.operation(
3819 self.span(),
3820 Operator::Comparison(Comparison::LessThan),
3821 op,
3822 rhs,
3823 );
3824 }
3825
3826 if matches!(self, Value::Nothing { .. }) || matches!(rhs, Value::Nothing { .. }) {
3827 return Ok(Value::nothing(span));
3828 }
3829
3830 if !type_compatible(self.get_type(), rhs.get_type()) {
3831 return Err(operator_type_error(
3832 Operator::Comparison(Comparison::LessThan),
3833 op,
3834 self,
3835 rhs,
3836 |val| {
3837 matches!(
3838 val,
3839 Value::Int { .. }
3840 | Value::Float { .. }
3841 | Value::String { .. }
3842 | Value::Filesize { .. }
3843 | Value::Duration { .. }
3844 | Value::Date { .. }
3845 | Value::Bool { .. }
3846 | Value::Nothing { .. }
3847 )
3848 },
3849 ));
3850 }
3851
3852 Ok(Value::bool(
3853 matches!(self.partial_cmp(rhs), Some(Ordering::Less)),
3854 span,
3855 ))
3856 }
3857
3858 pub fn lte(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3859 if let (Value::Custom { val: lhs, .. }, rhs) = (self, rhs) {
3860 return lhs.operation(
3861 self.span(),
3862 Operator::Comparison(Comparison::LessThanOrEqual),
3863 op,
3864 rhs,
3865 );
3866 }
3867
3868 if matches!(self, Value::Nothing { .. }) || matches!(rhs, Value::Nothing { .. }) {
3869 return Ok(Value::nothing(span));
3870 }
3871
3872 if !type_compatible(self.get_type(), rhs.get_type()) {
3873 return Err(operator_type_error(
3874 Operator::Comparison(Comparison::LessThanOrEqual),
3875 op,
3876 self,
3877 rhs,
3878 |val| {
3879 matches!(
3880 val,
3881 Value::Int { .. }
3882 | Value::Float { .. }
3883 | Value::String { .. }
3884 | Value::Filesize { .. }
3885 | Value::Duration { .. }
3886 | Value::Date { .. }
3887 | Value::Bool { .. }
3888 | Value::Nothing { .. }
3889 )
3890 },
3891 ));
3892 }
3893
3894 Ok(Value::bool(
3895 matches!(
3896 self.partial_cmp(rhs),
3897 Some(Ordering::Less | Ordering::Equal)
3898 ),
3899 span,
3900 ))
3901 }
3902
3903 pub fn gt(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3904 if let (Value::Custom { val: lhs, .. }, rhs) = (self, rhs) {
3905 return lhs.operation(
3906 self.span(),
3907 Operator::Comparison(Comparison::GreaterThan),
3908 op,
3909 rhs,
3910 );
3911 }
3912
3913 if matches!(self, Value::Nothing { .. }) || matches!(rhs, Value::Nothing { .. }) {
3914 return Ok(Value::nothing(span));
3915 }
3916
3917 if !type_compatible(self.get_type(), rhs.get_type()) {
3918 return Err(operator_type_error(
3919 Operator::Comparison(Comparison::GreaterThan),
3920 op,
3921 self,
3922 rhs,
3923 |val| {
3924 matches!(
3925 val,
3926 Value::Int { .. }
3927 | Value::Float { .. }
3928 | Value::String { .. }
3929 | Value::Filesize { .. }
3930 | Value::Duration { .. }
3931 | Value::Date { .. }
3932 | Value::Bool { .. }
3933 | Value::Nothing { .. }
3934 )
3935 },
3936 ));
3937 }
3938
3939 Ok(Value::bool(
3940 matches!(self.partial_cmp(rhs), Some(Ordering::Greater)),
3941 span,
3942 ))
3943 }
3944
3945 pub fn gte(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3946 if let (Value::Custom { val: lhs, .. }, rhs) = (self, rhs) {
3947 return lhs.operation(
3948 self.span(),
3949 Operator::Comparison(Comparison::GreaterThanOrEqual),
3950 op,
3951 rhs,
3952 );
3953 }
3954
3955 if matches!(self, Value::Nothing { .. }) || matches!(rhs, Value::Nothing { .. }) {
3956 return Ok(Value::nothing(span));
3957 }
3958
3959 if !type_compatible(self.get_type(), rhs.get_type()) {
3960 return Err(operator_type_error(
3961 Operator::Comparison(Comparison::GreaterThanOrEqual),
3962 op,
3963 self,
3964 rhs,
3965 |val| {
3966 matches!(
3967 val,
3968 Value::Int { .. }
3969 | Value::Float { .. }
3970 | Value::String { .. }
3971 | Value::Filesize { .. }
3972 | Value::Duration { .. }
3973 | Value::Date { .. }
3974 | Value::Bool { .. }
3975 | Value::Nothing { .. }
3976 )
3977 },
3978 ));
3979 }
3980
3981 Ok(Value::bool(
3982 matches!(
3983 self.partial_cmp(rhs),
3984 Some(Ordering::Greater | Ordering::Equal)
3985 ),
3986 span,
3987 ))
3988 }
3989
3990 pub fn eq(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
3991 if let (Value::Custom { val: lhs, .. }, rhs) = (self, rhs) {
3992 return lhs.operation(
3993 self.span(),
3994 Operator::Comparison(Comparison::Equal),
3995 op,
3996 rhs,
3997 );
3998 }
3999
4000 Ok(Value::bool(
4001 matches!(self.partial_cmp(rhs), Some(Ordering::Equal)),
4002 span,
4003 ))
4004 }
4005
4006 pub fn ne(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4007 if let (Value::Custom { val: lhs, .. }, rhs) = (self, rhs) {
4008 return lhs.operation(
4009 self.span(),
4010 Operator::Comparison(Comparison::NotEqual),
4011 op,
4012 rhs,
4013 );
4014 }
4015
4016 Ok(Value::bool(
4017 !matches!(self.partial_cmp(rhs), Some(Ordering::Equal)),
4018 span,
4019 ))
4020 }
4021
4022 pub fn r#in(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4023 match (self, rhs) {
4024 (lhs, Value::Range { val: rhs, .. }) => Ok(Value::bool(rhs.contains(lhs), span)),
4025 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
4026 Ok(Value::bool(rhs.contains(lhs), span))
4027 }
4028 (lhs, Value::List { vals: rhs, .. }) => Ok(Value::bool(rhs.contains(lhs), span)),
4029 (Value::String { val: lhs, .. }, Value::Record { val: rhs, .. }) => {
4030 Ok(Value::bool(rhs.contains(lhs), span))
4031 }
4032 (Value::String { .. } | Value::Int { .. }, Value::CellPath { val: rhs, .. }) => {
4033 let val = rhs.members.iter().any(|member| match (self, member) {
4034 (Value::Int { val: lhs, .. }, PathMember::Int { val: rhs, .. }) => {
4035 *lhs == *rhs as i64
4036 }
4037 (Value::String { val: lhs, .. }, PathMember::String { val: rhs, .. }) => {
4038 lhs == rhs
4039 }
4040 (Value::String { .. }, PathMember::Int { .. })
4041 | (Value::Int { .. }, PathMember::String { .. }) => false,
4042 _ => unreachable!(
4043 "outer match arm ensures `self` is either a `String` or `Int` variant"
4044 ),
4045 });
4046
4047 Ok(Value::bool(val, span))
4048 }
4049 (Value::CellPath { val: lhs, .. }, Value::CellPath { val: rhs, .. }) => {
4050 let found = lhs.members.is_empty()
4054 || rhs
4055 .members
4056 .windows(lhs.members.len())
4057 .any(|member_window| member_window == lhs.members);
4058 Ok(Value::bool(found, span))
4059 }
4060 (Value::Custom { val: lhs, .. }, rhs) => {
4061 lhs.operation(self.span(), Operator::Comparison(Comparison::In), op, rhs)
4062 }
4063 (lhs, rhs) => Err(
4064 if let Value::List { .. }
4065 | Value::Range { .. }
4066 | Value::String { .. }
4067 | Value::Record { .. }
4068 | Value::Custom { .. } = rhs
4069 {
4070 ShellError::OperatorIncompatibleTypes {
4071 op: Operator::Comparison(Comparison::In),
4072 lhs: lhs.get_type(),
4073 rhs: rhs.get_type(),
4074 op_span: op,
4075 lhs_span: lhs.span(),
4076 rhs_span: rhs.span(),
4077 help: None,
4078 }
4079 } else {
4080 ShellError::OperatorUnsupportedType {
4081 op: Operator::Comparison(Comparison::In),
4082 unsupported: rhs.get_type(),
4083 op_span: op,
4084 unsupported_span: rhs.span(),
4085 help: None,
4086 }
4087 },
4088 ),
4089 }
4090 }
4091
4092 pub fn not_in(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4093 match (self, rhs) {
4094 (lhs, Value::Range { val: rhs, .. }) => Ok(Value::bool(!rhs.contains(lhs), span)),
4095 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
4096 Ok(Value::bool(!rhs.contains(lhs), span))
4097 }
4098 (lhs, Value::List { vals: rhs, .. }) => Ok(Value::bool(!rhs.contains(lhs), span)),
4099 (Value::String { val: lhs, .. }, Value::Record { val: rhs, .. }) => {
4100 Ok(Value::bool(!rhs.contains(lhs), span))
4101 }
4102 (Value::String { .. } | Value::Int { .. }, Value::CellPath { val: rhs, .. }) => {
4103 let val = rhs.members.iter().any(|member| match (self, member) {
4104 (Value::Int { val: lhs, .. }, PathMember::Int { val: rhs, .. }) => {
4105 *lhs != *rhs as i64
4106 }
4107 (Value::String { val: lhs, .. }, PathMember::String { val: rhs, .. }) => {
4108 lhs != rhs
4109 }
4110 (Value::String { .. }, PathMember::Int { .. })
4111 | (Value::Int { .. }, PathMember::String { .. }) => true,
4112 _ => unreachable!(
4113 "outer match arm ensures `self` is either a `String` or `Int` variant"
4114 ),
4115 });
4116
4117 Ok(Value::bool(val, span))
4118 }
4119 (Value::CellPath { .. }, Value::CellPath { .. }) => {
4120 Ok(Value::bool(!self.r#in(op, rhs, span)?.is_true(), span))
4121 }
4122 (Value::Custom { val: lhs, .. }, rhs) => lhs.operation(
4123 self.span(),
4124 Operator::Comparison(Comparison::NotIn),
4125 op,
4126 rhs,
4127 ),
4128 (lhs, rhs) => Err(
4129 if let Value::List { .. }
4130 | Value::Range { .. }
4131 | Value::String { .. }
4132 | Value::Record { .. }
4133 | Value::Custom { .. } = rhs
4134 {
4135 ShellError::OperatorIncompatibleTypes {
4136 op: Operator::Comparison(Comparison::NotIn),
4137 lhs: lhs.get_type(),
4138 rhs: rhs.get_type(),
4139 op_span: op,
4140 lhs_span: lhs.span(),
4141 rhs_span: rhs.span(),
4142 help: None,
4143 }
4144 } else {
4145 ShellError::OperatorUnsupportedType {
4146 op: Operator::Comparison(Comparison::NotIn),
4147 unsupported: rhs.get_type(),
4148 op_span: op,
4149 unsupported_span: rhs.span(),
4150 help: None,
4151 }
4152 },
4153 ),
4154 }
4155 }
4156
4157 pub fn has(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4158 rhs.r#in(op, self, span)
4159 }
4160
4161 pub fn not_has(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4162 rhs.r#not_in(op, self, span)
4163 }
4164
4165 pub fn regex_match(
4166 &self,
4167 engine_state: &EngineState,
4168 op: Span,
4169 rhs: &Value,
4170 invert: bool,
4171 span: Span,
4172 ) -> Result<Value, ShellError> {
4173 let rhs_span = rhs.span();
4174 match (self, rhs) {
4175 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
4176 let regex = engine_state.compile_regex(rhs, rhs_span)?;
4177 let is_match = regex.is_match(lhs);
4178
4179 Ok(Value::bool(
4180 if invert {
4181 !is_match.unwrap_or(false)
4182 } else {
4183 is_match.unwrap_or(true)
4184 },
4185 span,
4186 ))
4187 }
4188 (Value::Custom { val: lhs, .. }, rhs) => lhs.operation(
4189 span,
4190 if invert {
4191 Operator::Comparison(Comparison::NotRegexMatch)
4192 } else {
4193 Operator::Comparison(Comparison::RegexMatch)
4194 },
4195 op,
4196 rhs,
4197 ),
4198 _ => Err(operator_type_error(
4199 if invert {
4200 Operator::Comparison(Comparison::NotRegexMatch)
4201 } else {
4202 Operator::Comparison(Comparison::RegexMatch)
4203 },
4204 op,
4205 self,
4206 rhs,
4207 |val| matches!(val, Value::String { .. }),
4208 )),
4209 }
4210 }
4211
4212 pub fn starts_with(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4213 match (self, rhs) {
4214 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
4215 Ok(Value::bool(lhs.starts_with(rhs), span))
4216 }
4217 (Value::Custom { val: lhs, .. }, rhs) => lhs.operation(
4218 self.span(),
4219 Operator::Comparison(Comparison::StartsWith),
4220 op,
4221 rhs,
4222 ),
4223 _ => Err(operator_type_error(
4224 Operator::Comparison(Comparison::StartsWith),
4225 op,
4226 self,
4227 rhs,
4228 |val| matches!(val, Value::String { .. }),
4229 )),
4230 }
4231 }
4232
4233 pub fn not_starts_with(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4234 match (self, rhs) {
4235 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
4236 Ok(Value::bool(!lhs.starts_with(rhs), span))
4237 }
4238 (Value::Custom { val: lhs, .. }, rhs) => lhs.operation(
4239 self.span(),
4240 Operator::Comparison(Comparison::NotStartsWith),
4241 op,
4242 rhs,
4243 ),
4244 _ => Err(operator_type_error(
4245 Operator::Comparison(Comparison::NotStartsWith),
4246 op,
4247 self,
4248 rhs,
4249 |val| matches!(val, Value::String { .. }),
4250 )),
4251 }
4252 }
4253
4254 pub fn ends_with(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4255 match (self, rhs) {
4256 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
4257 Ok(Value::bool(lhs.ends_with(rhs), span))
4258 }
4259 (Value::Custom { val: lhs, .. }, rhs) => lhs.operation(
4260 self.span(),
4261 Operator::Comparison(Comparison::EndsWith),
4262 op,
4263 rhs,
4264 ),
4265 _ => Err(operator_type_error(
4266 Operator::Comparison(Comparison::EndsWith),
4267 op,
4268 self,
4269 rhs,
4270 |val| matches!(val, Value::String { .. }),
4271 )),
4272 }
4273 }
4274
4275 pub fn not_ends_with(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4276 match (self, rhs) {
4277 (Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => {
4278 Ok(Value::bool(!lhs.ends_with(rhs), span))
4279 }
4280 (Value::Custom { val: lhs, .. }, rhs) => lhs.operation(
4281 self.span(),
4282 Operator::Comparison(Comparison::NotEndsWith),
4283 op,
4284 rhs,
4285 ),
4286 _ => Err(operator_type_error(
4287 Operator::Comparison(Comparison::NotEndsWith),
4288 op,
4289 self,
4290 rhs,
4291 |val| matches!(val, Value::String { .. }),
4292 )),
4293 }
4294 }
4295
4296 pub fn older_than(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4313 self.cmp_to_point_in_time(Comparison::OlderThan, op, rhs, span)
4314 }
4315
4316 pub fn newer_than(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4320 self.cmp_to_point_in_time(Comparison::NewerThan, op, rhs, span)
4321 }
4322
4323 fn cmp_to_point_in_time(
4327 &self,
4328 comparison: Comparison,
4329 op: Span,
4330 rhs: &Value,
4331 span: Span,
4332 ) -> Result<Value, ShellError> {
4333 let wanted = match comparison {
4334 Comparison::NewerThan => Ordering::Greater,
4335 _ => Ordering::Less,
4336 };
4337 let (lhs, point) = match (self, rhs) {
4338 (Value::Custom { val: lhs, .. }, rhs) => {
4339 return lhs.operation(self.span(), Operator::Comparison(comparison), op, rhs);
4340 }
4341 (Value::Nothing { .. }, _) | (_, Value::Nothing { .. }) => {
4342 return Ok(Value::nothing(span));
4343 }
4344 (Value::Date { val: lhs, .. }, Value::Date { val: rhs, .. }) => (lhs, *rhs),
4345 (Value::Date { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
4346 let point = Utc::now()
4348 .fixed_offset()
4349 .checked_sub_signed(Duration::nanoseconds(*rhs))
4350 .ok_or_else(|| ShellError::OperatorOverflow {
4351 msg: "subtracting the duration from now overflowed".into(),
4352 span,
4353 help: None,
4354 })?;
4355 (lhs, point)
4356 }
4357 _ => {
4358 return Err(operator_type_error(
4359 Operator::Comparison(comparison),
4360 op,
4361 self,
4362 rhs,
4363 |val| matches!(val, Value::Date { .. } | Value::Duration { .. }),
4364 ));
4365 }
4366 };
4367 Ok(Value::bool(lhs.cmp(&point) == wanted, span))
4369 }
4370
4371 pub fn bit_or(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4372 match (self, rhs) {
4373 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
4374 Ok(Value::int(*lhs | rhs, span))
4375 }
4376 (Value::Custom { val: lhs, .. }, rhs) => {
4377 lhs.operation(span, Operator::Bits(Bits::BitOr), op, rhs)
4378 }
4379 _ => Err(operator_type_error(
4380 Operator::Bits(Bits::BitOr),
4381 op,
4382 self,
4383 rhs,
4384 |val| matches!(val, Value::Int { .. }),
4385 )),
4386 }
4387 }
4388
4389 pub fn bit_xor(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4390 match (self, rhs) {
4391 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
4392 Ok(Value::int(*lhs ^ rhs, span))
4393 }
4394 (Value::Custom { val: lhs, .. }, rhs) => {
4395 lhs.operation(span, Operator::Bits(Bits::BitXor), op, rhs)
4396 }
4397 _ => Err(operator_type_error(
4398 Operator::Bits(Bits::BitXor),
4399 op,
4400 self,
4401 rhs,
4402 |val| matches!(val, Value::Int { .. }),
4403 )),
4404 }
4405 }
4406
4407 pub fn bit_and(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4408 match (self, rhs) {
4409 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
4410 Ok(Value::int(*lhs & rhs, span))
4411 }
4412 (Value::Custom { val: lhs, .. }, rhs) => {
4413 lhs.operation(span, Operator::Bits(Bits::BitAnd), op, rhs)
4414 }
4415 _ => Err(operator_type_error(
4416 Operator::Bits(Bits::BitAnd),
4417 op,
4418 self,
4419 rhs,
4420 |val| matches!(val, Value::Int { .. }),
4421 )),
4422 }
4423 }
4424
4425 pub fn bit_shl(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4426 match (self, rhs) {
4427 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
4428 if let Some(val) = (*rhs).try_into().ok().and_then(|rhs| lhs.checked_shl(rhs)) {
4431 Ok(Value::int(val, span))
4432 } else {
4433 Err(ShellError::OperatorOverflow {
4434 msg: "right operand to bit-shl exceeds available bits in underlying data"
4435 .into(),
4436 span,
4437 help: Some(format!("Limit operand to 0 <= rhs < {}", i64::BITS)),
4438 })
4439 }
4440 }
4441 (Value::Custom { val: lhs, .. }, rhs) => {
4442 lhs.operation(span, Operator::Bits(Bits::ShiftLeft), op, rhs)
4443 }
4444 _ => Err(operator_type_error(
4445 Operator::Bits(Bits::ShiftLeft),
4446 op,
4447 self,
4448 rhs,
4449 |val| matches!(val, Value::Int { .. }),
4450 )),
4451 }
4452 }
4453
4454 pub fn bit_shr(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4455 match (self, rhs) {
4456 (Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
4457 if let Some(val) = (*rhs).try_into().ok().and_then(|rhs| lhs.checked_shr(rhs)) {
4460 Ok(Value::int(val, span))
4461 } else {
4462 Err(ShellError::OperatorOverflow {
4463 msg: "right operand to bit-shr exceeds available bits in underlying data"
4464 .into(),
4465 span,
4466 help: Some(format!("Limit operand to 0 <= rhs < {}", i64::BITS)),
4467 })
4468 }
4469 }
4470 (Value::Custom { val: lhs, .. }, rhs) => {
4471 lhs.operation(span, Operator::Bits(Bits::ShiftRight), op, rhs)
4472 }
4473 _ => Err(operator_type_error(
4474 Operator::Bits(Bits::ShiftRight),
4475 op,
4476 self,
4477 rhs,
4478 |val| matches!(val, Value::Int { .. }),
4479 )),
4480 }
4481 }
4482
4483 pub fn or(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4484 match (self, rhs) {
4485 (Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => {
4486 Ok(Value::bool(*lhs || *rhs, span))
4487 }
4488 (Value::Custom { val: lhs, .. }, rhs) => {
4489 lhs.operation(span, Operator::Boolean(Boolean::Or), op, rhs)
4490 }
4491 _ => Err(operator_type_error(
4492 Operator::Boolean(Boolean::Or),
4493 op,
4494 self,
4495 rhs,
4496 |val| matches!(val, Value::Bool { .. }),
4497 )),
4498 }
4499 }
4500
4501 pub fn xor(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4502 match (self, rhs) {
4503 (Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => {
4504 Ok(Value::bool((*lhs && !*rhs) || (!*lhs && *rhs), span))
4505 }
4506 (Value::Custom { val: lhs, .. }, rhs) => {
4507 lhs.operation(span, Operator::Boolean(Boolean::Xor), op, rhs)
4508 }
4509 _ => Err(operator_type_error(
4510 Operator::Boolean(Boolean::Xor),
4511 op,
4512 self,
4513 rhs,
4514 |val| matches!(val, Value::Bool { .. }),
4515 )),
4516 }
4517 }
4518
4519 pub fn and(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
4520 match (self, rhs) {
4521 (Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => {
4522 Ok(Value::bool(*lhs && *rhs, span))
4523 }
4524 (Value::Custom { val: lhs, .. }, rhs) => {
4525 lhs.operation(span, Operator::Boolean(Boolean::And), op, rhs)
4526 }
4527 _ => Err(operator_type_error(
4528 Operator::Boolean(Boolean::And),
4529 op,
4530 self,
4531 rhs,
4532 |val| matches!(val, Value::Bool { .. }),
4533 )),
4534 }
4535 }
4536}
4537
4538fn type_compatible(a: Type, b: Type) -> bool {
4541 if a == b {
4542 return true;
4543 }
4544
4545 matches!((a, b), (Type::Int, Type::Float) | (Type::Float, Type::Int))
4546}
4547
4548fn operator_type_error(
4549 op: Operator,
4550 op_span: Span,
4551 lhs: &Value,
4552 rhs: &Value,
4553 is_supported: fn(&Value) -> bool,
4554) -> ShellError {
4555 let is_supported = |val| is_supported(val) || matches!(val, Value::Custom { .. });
4556 match (is_supported(lhs), is_supported(rhs)) {
4557 (true, true) => {
4558 let (lhs_ty, rhs_ty) = (lhs.get_type(), rhs.get_type());
4559 ShellError::OperatorIncompatibleTypes {
4560 help: op.incompatible_types_help(&lhs_ty, &rhs_ty),
4561 op,
4562 lhs: lhs_ty,
4563 rhs: rhs_ty,
4564 op_span,
4565 lhs_span: lhs.span(),
4566 rhs_span: rhs.span(),
4567 }
4568 }
4569 (true, false) => ShellError::OperatorUnsupportedType {
4570 op,
4571 unsupported: rhs.get_type(),
4572 op_span,
4573 unsupported_span: rhs.span(),
4574 help: None,
4575 },
4576 (false, _) => ShellError::OperatorUnsupportedType {
4577 op,
4578 unsupported: lhs.get_type(),
4579 op_span,
4580 unsupported_span: lhs.span(),
4581 help: None,
4582 },
4583 }
4584}
4585
4586pub fn human_time_from_now(val: &DateTime<FixedOffset>) -> HumanTime {
4587 let now = Local::now().with_timezone(val.offset());
4588 let delta = *val - now;
4589 match delta.num_nanoseconds() {
4590 Some(num_nanoseconds) => {
4591 let delta_seconds = num_nanoseconds as f64 / 1_000_000_000.0;
4592 let delta_seconds_rounded = delta_seconds.round() as i64;
4593 HumanTime::from(Duration::seconds(delta_seconds_rounded))
4594 }
4595 None => {
4596 let delta_years = val.year() - now.year();
4599 HumanTime::from(Duration::days(delta_years as i64 * 365))
4600 }
4601 }
4602}
4603
4604#[cfg(test)]
4605mod tests {
4606 use super::{Record, Value};
4607 use crate::record;
4608 use indoc::indoc;
4609
4610 mod debug {
4611 use super::*;
4612 use crate::{
4613 BlockId, CustomValue, IntRange, Range, ShellError, Span, VarId,
4614 ast::{CellPath, PathMember},
4615 casing::Casing,
4616 engine::Closure,
4617 };
4618 use chrono::DateTime;
4619 use pretty_assertions::assert_eq;
4620 use serde::{Deserialize, Serialize};
4621 use std::ops::Bound;
4622
4623 #[derive(Debug, Clone, Serialize, Deserialize)]
4624 struct TinyCustomValue;
4625
4626 #[typetag::serde]
4627 impl CustomValue for TinyCustomValue {
4628 fn clone_value(&self, span: Span) -> Value {
4629 Value::custom(Box::new(self.clone()), span)
4630 }
4631
4632 fn type_name(&self) -> String {
4633 "TinyCustomValue".into()
4634 }
4635
4636 fn to_base_value(&self, span: Span) -> Result<Value, ShellError> {
4637 Ok(Value::nothing(span))
4638 }
4639
4640 fn as_any(&self) -> &dyn std::any::Any {
4641 self
4642 }
4643
4644 fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
4645 self
4646 }
4647 }
4648
4649 struct DebugFormats {
4650 value: Value,
4651 expanded: &'static str,
4652 expanded_alternate: &'static str,
4653 compact: &'static str,
4654 compact_alternate: &'static str,
4655 }
4656
4657 impl DebugFormats {
4658 #[track_caller]
4659 fn assert(&self) {
4660 let value = &self.value;
4661 assert_eq!(format!("{value:-?}"), self.expanded);
4662 assert_eq!(format!("{value:-#?}"), self.expanded_alternate);
4663 assert_eq!(format!("{value:?}"), self.compact);
4664 assert_eq!(format!("{value:#?}"), self.compact_alternate);
4665 }
4666 }
4667
4668 #[test]
4669 fn bool() {
4670 let value = Value::test_bool(true);
4671 DebugFormats {
4672 value,
4673 expanded: "Bool { val: true, internal_span: Span(TEST) }",
4674 expanded_alternate: indoc! {"
4675 Bool {
4676 val: true,
4677 internal_span: Span(TEST),
4678 }"
4679 },
4680 compact: "Bool(true)",
4681 compact_alternate: "Bool(true)",
4682 }
4683 .assert();
4684 }
4685
4686 #[test]
4687 fn int() {
4688 let value = Value::test_int(42);
4689 DebugFormats {
4690 value,
4691 expanded: "Int { val: 42, internal_span: Span(TEST) }",
4692 expanded_alternate: indoc! {"
4693 Int {
4694 val: 42,
4695 internal_span: Span(TEST),
4696 }"
4697 },
4698 compact: "Int(42)",
4699 compact_alternate: "Int(42)",
4700 }
4701 .assert();
4702 }
4703
4704 #[test]
4705 fn float() {
4706 let value = Value::test_float(4.2);
4707 DebugFormats {
4708 value,
4709 expanded: "Float { val: 4.2, internal_span: Span(TEST) }",
4710 expanded_alternate: indoc! {"
4711 Float {
4712 val: 4.2,
4713 internal_span: Span(TEST),
4714 }"
4715 },
4716 compact: "Float(4.2)",
4717 compact_alternate: "Float(4.2)",
4718 }
4719 .assert();
4720 }
4721
4722 #[test]
4723 fn filesize() {
4724 let value = Value::test_filesize(42);
4725 DebugFormats {
4726 value,
4727 expanded: "Filesize { val: Filesize(42), internal_span: Span(TEST) }",
4728 expanded_alternate: indoc! {"
4729 Filesize {
4730 val: Filesize(
4731 42,
4732 ),
4733 internal_span: Span(TEST),
4734 }"
4735 },
4736 compact: "Filesize(42 B)",
4737 compact_alternate: "Filesize(42 B)",
4738 }
4739 .assert();
4740 }
4741
4742 #[test]
4743 fn duration() {
4744 let value = Value::test_duration(42);
4745 DebugFormats {
4746 value,
4747 expanded: "Duration { val: 42, internal_span: Span(TEST) }",
4748 expanded_alternate: indoc! {"
4749 Duration {
4750 val: 42,
4751 internal_span: Span(TEST),
4752 }"
4753 },
4754 compact: "Duration(42ns)",
4755 compact_alternate: "Duration(42ns)",
4756 }
4757 .assert();
4758 }
4759
4760 #[test]
4761 fn date() {
4762 let value = Value::test_date(DateTime::UNIX_EPOCH.into());
4763 DebugFormats {
4764 value,
4765 expanded: "Date { val: 1970-01-01T00:00:00+00:00, internal_span: Span(TEST) }",
4766 expanded_alternate: indoc! {"
4767 Date {
4768 val: 1970-01-01T00:00:00+00:00,
4769 internal_span: Span(TEST),
4770 }"
4771 },
4772 compact: "Date(1970-01-01T00:00:00+00:00)",
4773 compact_alternate: "Date(1970-01-01T00:00:00+00:00)",
4774 }
4775 .assert();
4776 }
4777
4778 #[test]
4779 fn range() {
4780 let value = Value::test_range(Range::IntRange(IntRange {
4781 start: 1,
4782 step: 2,
4783 end: Bound::Excluded(5),
4784 }));
4785 DebugFormats {
4786 value,
4787 expanded: "Range { val: IntRange(IntRange { start: 1, step: 2, end: Excluded(5) }), signals: None, internal_span: Span(TEST) }",
4788 expanded_alternate: indoc! {"
4789 Range {
4790 val: IntRange(
4791 IntRange {
4792 start: 1,
4793 step: 2,
4794 end: Excluded(
4795 5,
4796 ),
4797 },
4798 ),
4799 signals: None,
4800 internal_span: Span(TEST),
4801 }"
4802 },
4803 compact: "Range(1..3..<5)",
4804 compact_alternate: "Range(1..3..<5)",
4805 }
4806 .assert();
4807 }
4808
4809 #[test]
4810 fn string() {
4811 let value = Value::test_string("Ellie");
4812 DebugFormats {
4813 value,
4814 expanded: r#"String { val: "Ellie", internal_span: Span(TEST) }"#,
4815 expanded_alternate: indoc! {r#"
4816 String {
4817 val: "Ellie",
4818 internal_span: Span(TEST),
4819 }"#
4820 },
4821 compact: r#"String("Ellie")"#,
4822 compact_alternate: r#"String("Ellie")"#,
4823 }
4824 .assert();
4825 }
4826
4827 #[test]
4828 fn glob() {
4829 let value = Value::test_glob("*.nu");
4830 DebugFormats {
4831 value,
4832 expanded: r#"Glob { val: "*.nu", no_expand: false, internal_span: Span(TEST) }"#,
4833 expanded_alternate: indoc! {r#"
4834 Glob {
4835 val: "*.nu",
4836 no_expand: false,
4837 internal_span: Span(TEST),
4838 }"#
4839 },
4840 compact: r#"Glob("*.nu")"#,
4841 compact_alternate: r#"Glob("*.nu")"#,
4842 }
4843 .assert();
4844 }
4845
4846 #[test]
4847 fn record() {
4848 let value = Value::test_record(record!("name" => Value::test_string("Ellie")));
4849 DebugFormats {
4850 value,
4851 expanded: r#"Record { val: {"name": String { val: "Ellie", internal_span: Span(TEST) }}, internal_span: Span(TEST) }"#,
4852 expanded_alternate: indoc! {r#"
4853 Record {
4854 val: {
4855 "name": String {
4856 val: "Ellie",
4857 internal_span: Span(TEST),
4858 },
4859 },
4860 internal_span: Span(TEST),
4861 }"#
4862 },
4863 compact: r#"Record({"name": String("Ellie")})"#,
4864 compact_alternate: indoc! {r#"
4865 Record({
4866 "name": String("Ellie"),
4867 })"#
4868 },
4869 }
4870 .assert();
4871 }
4872
4873 #[test]
4874 fn list() {
4875 let value = Value::test_list(vec![Value::test_int(42), Value::test_string("Ellie")]);
4876 DebugFormats {
4877 value,
4878 expanded: r#"List { vals: [Int { val: 42, internal_span: Span(TEST) }, String { val: "Ellie", internal_span: Span(TEST) }], signals: None, internal_span: Span(TEST) }"#,
4879 expanded_alternate: indoc! {r#"
4880 List {
4881 vals: [
4882 Int {
4883 val: 42,
4884 internal_span: Span(TEST),
4885 },
4886 String {
4887 val: "Ellie",
4888 internal_span: Span(TEST),
4889 },
4890 ],
4891 signals: None,
4892 internal_span: Span(TEST),
4893 }"#
4894 },
4895 compact: r#"List([Int(42), String("Ellie")])"#,
4896 compact_alternate: indoc! {r#"
4897 List([
4898 Int(42),
4899 String("Ellie"),
4900 ])"#
4901 },
4902 }
4903 .assert();
4904 }
4905
4906 #[test]
4907 fn closure() {
4908 let value = Value::test_closure(Closure {
4909 block_id: BlockId::new(42),
4910 captures: vec![(VarId::new(7), Value::test_int(1))],
4911 });
4912 DebugFormats {
4913 value,
4914 expanded: "Closure { val: Closure { block_id: BlockId(42), captures: {VarId(7): Int { val: 1, internal_span: Span(TEST) }} }, internal_span: Span(TEST) }",
4915 expanded_alternate: indoc! {"
4916 Closure {
4917 val: Closure {
4918 block_id: BlockId(42),
4919 captures: {
4920 VarId(7): Int {
4921 val: 1,
4922 internal_span: Span(TEST),
4923 },
4924 },
4925 },
4926 internal_span: Span(TEST),
4927 }"
4928 },
4929 compact: "Closure(BlockId(42): {VarId(7): Int(1)})",
4930 compact_alternate: indoc! {"
4931 Closure(BlockId(42): {
4932 VarId(7): Int(1),
4933 })"
4934 },
4935 }
4936 .assert();
4937 }
4938
4939 #[test]
4940 fn error() {
4941 let value = Value::error(
4942 ShellError::NushellFailed { msg: "oops".into() },
4943 Span::test_data(),
4944 );
4945 DebugFormats {
4946 value,
4947 expanded: r#"Error { error: NushellFailed { msg: "oops" }, internal_span: Span(TEST) }"#,
4948 expanded_alternate: indoc! {r#"
4949 Error {
4950 error: NushellFailed {
4951 msg: "oops",
4952 },
4953 internal_span: Span(TEST),
4954 }"#
4955 },
4956 compact: r#"Error(NushellFailed { msg: "oops" })"#,
4957 compact_alternate: indoc! {r#"
4958 Error(NushellFailed {
4959 msg: "oops",
4960 })"#
4961 },
4962 }
4963 .assert();
4964 }
4965
4966 #[test]
4967 fn binary() {
4968 let mut bytes = b"Ellie".to_vec();
4969 bytes.extend([0xFF]);
4970 let value = Value::test_binary(bytes);
4971 DebugFormats {
4972 value,
4973 expanded: "Binary { val: [69, 108, 108, 105, 101, 255], internal_span: Span(TEST) }",
4974 expanded_alternate: indoc! {"
4975 Binary {
4976 val: [
4977 69,
4978 108,
4979 108,
4980 105,
4981 101,
4982 255,
4983 ],
4984 internal_span: Span(TEST),
4985 }"
4986 },
4987 compact: r#"Binary("Ellie\xff")"#,
4988 compact_alternate: r#"Binary("Ellie\xff")"#,
4989 }
4990 .assert();
4991 }
4992
4993 #[test]
4994 fn cell_path() {
4995 let value = Value::test_cell_path(CellPath {
4996 members: vec![
4997 PathMember::test_string("name", false, Casing::Sensitive),
4998 PathMember::test_int(1, true),
4999 ],
5000 });
5001 DebugFormats {
5002 value,
5003 expanded: r#"CellPath { val: CellPath { members: [String { val: "name", span: Span(TEST), optional: false, casing: Sensitive }, Int { val: 1, span: Span(TEST), optional: true }] }, internal_span: Span(TEST) }"#,
5004 expanded_alternate: indoc! {r#"
5005 CellPath {
5006 val: CellPath {
5007 members: [
5008 String {
5009 val: "name",
5010 span: Span(TEST),
5011 optional: false,
5012 casing: Sensitive,
5013 },
5014 Int {
5015 val: 1,
5016 span: Span(TEST),
5017 optional: true,
5018 },
5019 ],
5020 },
5021 internal_span: Span(TEST),
5022 }"#
5023 },
5024 compact: "CellPath($.name.1?)",
5025 compact_alternate: "CellPath($.name.1?)",
5026 }
5027 .assert();
5028 }
5029
5030 #[test]
5031 fn nothing() {
5032 let value = Value::test_nothing();
5033 DebugFormats {
5034 value,
5035 expanded: "Nothing { internal_span: Span(TEST) }",
5036 expanded_alternate: indoc! {"
5037 Nothing {
5038 internal_span: Span(TEST),
5039 }"
5040 },
5041 compact: "Nothing",
5042 compact_alternate: "Nothing",
5043 }
5044 .assert();
5045 }
5046
5047 #[test]
5048 fn custom() {
5049 let value = Value::test_custom_value(Box::new(TinyCustomValue));
5050 DebugFormats {
5051 value,
5052 expanded: "Custom { val: TinyCustomValue, internal_span: Span(TEST) }",
5053 expanded_alternate: indoc! {"
5054 Custom {
5055 val: TinyCustomValue,
5056 internal_span: Span(TEST),
5057 }"
5058 },
5059 compact: "Custom(TinyCustomValue)",
5060 compact_alternate: "Custom(TinyCustomValue)",
5061 }
5062 .assert();
5063 }
5064 }
5065
5066 mod at_cell_path {
5067 use crate::casing::Casing;
5068
5069 use crate::{IntoValue, ShellError, Span};
5070
5071 use super::super::PathMember;
5072 use super::*;
5073
5074 #[test]
5075 fn test_record_with_data_at_cell_path() {
5076 let value_to_insert = Value::test_string("value");
5077 let span = Span::test_data();
5078 assert_eq!(
5079 Value::with_data_at_cell_path(
5080 &[
5081 PathMember::test_string("a".to_string(), false, Casing::Sensitive),
5082 PathMember::test_string("b".to_string(), false, Casing::Sensitive),
5083 PathMember::test_string("c".to_string(), false, Casing::Sensitive),
5084 PathMember::test_string("d".to_string(), false, Casing::Sensitive),
5085 ],
5086 value_to_insert,
5087 ),
5088 Ok(record!(
5090 "a" => record!(
5091 "b" => record!(
5092 "c" => record!(
5093 "d" => Value::test_string("value")
5094 ).into_value(span)
5095 ).into_value(span)
5096 ).into_value(span)
5097 )
5098 .into_value(span))
5099 );
5100 }
5101
5102 #[test]
5103 fn test_lists_with_data_at_cell_path() {
5104 let value_to_insert = Value::test_string("value");
5105 assert_eq!(
5106 Value::with_data_at_cell_path(
5107 &[
5108 PathMember::test_int(0, false),
5109 PathMember::test_int(0, false),
5110 PathMember::test_int(0, false),
5111 PathMember::test_int(0, false),
5112 ],
5113 value_to_insert.clone(),
5114 ),
5115 Ok(Value::test_list(vec![Value::test_list(vec![
5117 Value::test_list(vec![Value::test_list(vec![value_to_insert])])
5118 ])]))
5119 );
5120 }
5121 #[test]
5122 fn test_mixed_with_data_at_cell_path() {
5123 let value_to_insert = Value::test_string("value");
5124 let span = Span::test_data();
5125 assert_eq!(
5126 Value::with_data_at_cell_path(
5127 &[
5128 PathMember::test_string("a".to_string(), false, Casing::Sensitive),
5129 PathMember::test_int(0, false),
5130 PathMember::test_string("b".to_string(), false, Casing::Sensitive),
5131 PathMember::test_int(0, false),
5132 PathMember::test_string("c".to_string(), false, Casing::Sensitive),
5133 PathMember::test_int(0, false),
5134 PathMember::test_string("d".to_string(), false, Casing::Sensitive),
5135 PathMember::test_int(0, false),
5136 ],
5137 value_to_insert.clone(),
5138 ),
5139 Ok(record!(
5141 "a" => Value::test_list(vec![record!(
5142 "b" => Value::test_list(vec![record!(
5143 "c" => Value::test_list(vec![record!(
5144 "d" => Value::test_list(vec![value_to_insert])
5145 ).into_value(span)])
5146 ).into_value(span)])
5147 ).into_value(span)])
5148 )
5149 .into_value(span))
5150 );
5151 }
5152
5153 #[test]
5154 fn test_nested_upsert_data_at_cell_path() {
5155 let span = Span::test_data();
5156 let mut base_value = record!(
5157 "a" => Value::test_list(vec![])
5158 )
5159 .into_value(span);
5160
5161 let value_to_insert = Value::test_string("value");
5162 let res = base_value.upsert_data_at_cell_path(
5163 &[
5164 PathMember::test_string("a".to_string(), false, Casing::Sensitive),
5165 PathMember::test_int(0, false),
5166 PathMember::test_string("b".to_string(), false, Casing::Sensitive),
5167 PathMember::test_int(0, false),
5168 ],
5169 value_to_insert.clone(),
5170 );
5171 assert_eq!(res, Ok(()));
5172 assert_eq!(
5173 base_value,
5174 record!(
5176 "a" => Value::test_list(vec![
5177 record!(
5178 "b" => Value::test_list(vec![value_to_insert])
5179 )
5180 .into_value(span)
5181 ])
5182 )
5183 .into_value(span)
5184 );
5185 }
5186
5187 #[test]
5188 fn test_nested_insert_data_at_cell_path() {
5189 let span = Span::test_data();
5190 let mut base_value = record!(
5191 "a" => Value::test_list(vec![])
5192 )
5193 .into_value(span);
5194
5195 let value_to_insert = Value::test_string("value");
5196 let res = base_value.insert_data_at_cell_path(
5197 &[
5198 PathMember::test_string("a".to_string(), false, Casing::Sensitive),
5199 PathMember::test_int(0, false),
5200 PathMember::test_string("b".to_string(), false, Casing::Sensitive),
5201 PathMember::test_int(0, false),
5202 ],
5203 value_to_insert.clone(),
5204 span,
5205 );
5206 assert_eq!(res, Ok(()));
5207 assert_eq!(
5208 base_value,
5209 record!(
5211 "a" => Value::test_list(vec![
5212 record!(
5213 "b" => Value::test_list(vec![value_to_insert])
5214 )
5215 .into_value(span)
5216 ])
5217 )
5218 .into_value(span)
5219 );
5220 }
5221
5222 #[test]
5223 fn update_existing_record_field() {
5224 let span = Span::test_data();
5225 let mut val = record!("a" => Value::test_int(1)).into_value(span);
5226 let res = val.update_data_at_cell_path(
5227 &[PathMember::test_string("a", false, Casing::Sensitive)],
5228 Value::test_int(2),
5229 );
5230 assert_eq!(res, Ok(()));
5231 assert_eq!(val, record!("a" => Value::test_int(2)).into_value(span));
5232 }
5233
5234 #[test]
5235 fn update_existing_list_element() {
5236 let mut val = Value::test_list(vec![Value::test_int(10), Value::test_int(20)]);
5237 let res = val
5238 .update_data_at_cell_path(&[PathMember::test_int(1, false)], Value::test_int(99));
5239 assert_eq!(res, Ok(()));
5240 assert_eq!(
5241 val,
5242 Value::test_list(vec![Value::test_int(10), Value::test_int(99)])
5243 );
5244 }
5245
5246 #[test]
5247 fn update_missing_record_field_errors() {
5248 let span = Span::test_data();
5249 let mut val = record!("a" => Value::test_int(1)).into_value(span);
5250 let res = val.update_data_at_cell_path(
5251 &[PathMember::test_string("b", false, Casing::Sensitive)],
5252 Value::test_int(2),
5253 );
5254 assert!(matches!(res, Err(ShellError::CantFindColumn { .. })));
5255 }
5256
5257 #[test]
5258 fn update_out_of_bounds_list_errors() {
5259 let mut val = Value::test_list(vec![Value::test_int(1)]);
5260 let res =
5261 val.update_data_at_cell_path(&[PathMember::test_int(5, false)], Value::test_int(2));
5262 assert!(matches!(res, Err(ShellError::AccessBeyondEnd { .. })));
5263 }
5264
5265 #[test]
5266 fn update_empty_list_errors() {
5267 let mut val = Value::test_list(vec![]);
5268 let res =
5269 val.update_data_at_cell_path(&[PathMember::test_int(0, false)], Value::test_int(2));
5270 assert!(matches!(res, Err(ShellError::AccessEmptyContent { .. })));
5271 }
5272
5273 #[test]
5274 fn update_optional_missing_field_ok() {
5275 let span = Span::test_data();
5276 let mut val = record!("a" => Value::test_int(1)).into_value(span);
5277 let res = val.update_data_at_cell_path(
5278 &[PathMember::test_string("z", true, Casing::Sensitive)],
5279 Value::test_int(2),
5280 );
5281 assert_eq!(res, Ok(()));
5282 assert_eq!(val, record!("a" => Value::test_int(1)).into_value(span));
5283 }
5284
5285 #[test]
5286 fn update_optional_out_of_bounds_ok() {
5287 let mut val = Value::test_list(vec![Value::test_int(1)]);
5288 let res =
5289 val.update_data_at_cell_path(&[PathMember::test_int(5, true)], Value::test_int(2));
5290 assert_eq!(res, Ok(()));
5291 assert_eq!(val, Value::test_list(vec![Value::test_int(1)]));
5292 }
5293
5294 #[test]
5295 fn update_nested_record_field() {
5296 let span = Span::test_data();
5297 let mut val = record!(
5298 "a" => record!("b" => Value::test_int(1)).into_value(span)
5299 )
5300 .into_value(span);
5301 let res = val.update_data_at_cell_path(
5302 &[
5303 PathMember::test_string("a", false, Casing::Sensitive),
5304 PathMember::test_string("b", false, Casing::Sensitive),
5305 ],
5306 Value::test_int(99),
5307 );
5308 assert_eq!(res, Ok(()));
5309 assert_eq!(
5310 val,
5311 record!(
5312 "a" => record!("b" => Value::test_int(99)).into_value(span)
5313 )
5314 .into_value(span)
5315 );
5316 }
5317
5318 #[test]
5319 fn update_in_table() {
5320 let span = Span::test_data();
5321 let mut val = Value::test_list(vec![
5322 record!("x" => Value::test_int(1)).into_value(span),
5323 record!("x" => Value::test_int(2)).into_value(span),
5324 ]);
5325 let res = val.update_data_at_cell_path(
5326 &[PathMember::test_string("x", false, Casing::Sensitive)],
5327 Value::test_int(0),
5328 );
5329 assert_eq!(res, Ok(()));
5330 assert_eq!(
5331 val,
5332 Value::test_list(vec![
5333 record!("x" => Value::test_int(0)).into_value(span),
5334 record!("x" => Value::test_int(0)).into_value(span),
5335 ])
5336 );
5337 }
5338
5339 #[test]
5340 fn remove_record_column() {
5341 let span = Span::test_data();
5342 let mut val =
5343 record!("a" => Value::test_int(1), "b" => Value::test_int(2)).into_value(span);
5344 let res = val.remove_data_at_cell_path(&[PathMember::test_string(
5345 "a",
5346 false,
5347 Casing::Sensitive,
5348 )]);
5349 assert_eq!(res, Ok(()));
5350 assert_eq!(val, record!("b" => Value::test_int(2)).into_value(span));
5351 }
5352
5353 #[test]
5354 fn remove_list_element() {
5355 let mut val = Value::test_list(vec![
5356 Value::test_int(10),
5357 Value::test_int(20),
5358 Value::test_int(30),
5359 ]);
5360 let res = val.remove_data_at_cell_path(&[PathMember::test_int(1, false)]);
5361 assert_eq!(res, Ok(()));
5362 assert_eq!(
5363 val,
5364 Value::test_list(vec![Value::test_int(10), Value::test_int(30)])
5365 );
5366 }
5367
5368 #[test]
5369 fn remove_nested_field() {
5370 let span = Span::test_data();
5371 let mut val = record!(
5372 "a" => record!("b" => Value::test_int(1), "c" => Value::test_int(2)).into_value(span)
5373 )
5374 .into_value(span);
5375 let res = val.remove_data_at_cell_path(&[
5376 PathMember::test_string("a", false, Casing::Sensitive),
5377 PathMember::test_string("b", false, Casing::Sensitive),
5378 ]);
5379 assert_eq!(res, Ok(()));
5380 assert_eq!(
5381 val,
5382 record!(
5383 "a" => record!("c" => Value::test_int(2)).into_value(span)
5384 )
5385 .into_value(span)
5386 );
5387 }
5388
5389 #[test]
5390 fn remove_column_from_table() {
5391 let span = Span::test_data();
5392 let mut val = Value::test_list(vec![
5393 record!("x" => Value::test_int(1), "y" => Value::test_int(2)).into_value(span),
5394 record!("x" => Value::test_int(3), "y" => Value::test_int(4)).into_value(span),
5395 ]);
5396 let res = val.remove_data_at_cell_path(&[PathMember::test_string(
5397 "x",
5398 false,
5399 Casing::Sensitive,
5400 )]);
5401 assert_eq!(res, Ok(()));
5402 assert_eq!(
5403 val,
5404 Value::test_list(vec![
5405 record!("y" => Value::test_int(2)).into_value(span),
5406 record!("y" => Value::test_int(4)).into_value(span),
5407 ])
5408 );
5409 }
5410
5411 #[test]
5412 fn upsert_overwrite_existing_record_field() {
5413 let span = Span::test_data();
5414 let mut val = record!("a" => Value::test_int(1)).into_value(span);
5415 let res = val.upsert_data_at_cell_path(
5416 &[PathMember::test_string("a", false, Casing::Sensitive)],
5417 Value::test_int(99),
5418 );
5419 assert_eq!(res, Ok(()));
5420 assert_eq!(val, record!("a" => Value::test_int(99)).into_value(span));
5421 }
5422
5423 #[test]
5424 fn upsert_overwrite_existing_list_element() {
5425 let mut val = Value::test_list(vec![Value::test_int(10), Value::test_int(20)]);
5426 let res = val
5427 .upsert_data_at_cell_path(&[PathMember::test_int(0, false)], Value::test_int(99));
5428 assert_eq!(res, Ok(()));
5429 assert_eq!(
5430 val,
5431 Value::test_list(vec![Value::test_int(99), Value::test_int(20)])
5432 );
5433 }
5434
5435 #[test]
5436 fn upsert_creates_new_record_field() {
5437 let span = Span::test_data();
5438 let mut val = record!("a" => Value::test_int(1)).into_value(span);
5439 let res = val.upsert_data_at_cell_path(
5440 &[PathMember::test_string("b", false, Casing::Sensitive)],
5441 Value::test_int(2),
5442 );
5443 assert_eq!(res, Ok(()));
5444 assert_eq!(
5445 val,
5446 record!("a" => Value::test_int(1), "b" => Value::test_int(2)).into_value(span)
5447 );
5448 }
5449
5450 #[test]
5451 fn upsert_appends_to_list() {
5452 let mut val = Value::test_list(vec![Value::test_int(1)]);
5453 let res =
5454 val.upsert_data_at_cell_path(&[PathMember::test_int(1, false)], Value::test_int(2));
5455 assert_eq!(res, Ok(()));
5456 assert_eq!(
5457 val,
5458 Value::test_list(vec![Value::test_int(1), Value::test_int(2)])
5459 );
5460 }
5461
5462 #[test]
5463 fn upsert_in_table() {
5464 let span = Span::test_data();
5465 let mut val = Value::test_list(vec![
5466 record!("x" => Value::test_int(1)).into_value(span),
5467 record!("x" => Value::test_int(2)).into_value(span),
5468 ]);
5469 let res = val.upsert_data_at_cell_path(
5470 &[PathMember::test_string("x", false, Casing::Sensitive)],
5471 Value::test_int(0),
5472 );
5473 assert_eq!(res, Ok(()));
5474 assert_eq!(
5475 val,
5476 Value::test_list(vec![
5477 record!("x" => Value::test_int(0)).into_value(span),
5478 record!("x" => Value::test_int(0)).into_value(span),
5479 ])
5480 );
5481 }
5482
5483 #[test]
5484 fn insert_new_record_field() {
5485 let span = Span::test_data();
5486 let mut val = record!("a" => Value::test_int(1)).into_value(span);
5487 let res = val.insert_data_at_cell_path(
5488 &[PathMember::test_string("b", false, Casing::Sensitive)],
5489 Value::test_int(2),
5490 span,
5491 );
5492 assert_eq!(res, Ok(()));
5493 assert_eq!(
5494 val,
5495 record!("a" => Value::test_int(1), "b" => Value::test_int(2)).into_value(span)
5496 );
5497 }
5498
5499 #[test]
5500 fn insert_existing_record_field_errors() {
5501 let span = Span::test_data();
5502 let mut val = record!("a" => Value::test_int(1)).into_value(span);
5503 let res = val.insert_data_at_cell_path(
5504 &[PathMember::test_string("a", false, Casing::Sensitive)],
5505 Value::test_int(2),
5506 span,
5507 );
5508 assert!(matches!(res, Err(ShellError::ColumnAlreadyExists { .. })));
5509 }
5510
5511 #[test]
5512 fn insert_at_existing_list_index_shifts() {
5513 let mut val = Value::test_list(vec![Value::test_int(1), Value::test_int(2)]);
5514 let span = Span::test_data();
5515 let res = val.insert_data_at_cell_path(
5516 &[PathMember::test_int(0, false)],
5517 Value::test_int(99),
5518 span,
5519 );
5520 assert_eq!(res, Ok(()));
5521 assert_eq!(
5522 val,
5523 Value::test_list(vec![
5524 Value::test_int(99),
5525 Value::test_int(1),
5526 Value::test_int(2),
5527 ])
5528 );
5529 }
5530
5531 #[test]
5532 fn insert_appends_at_end_of_list() {
5533 let mut val = Value::test_list(vec![Value::test_int(1)]);
5534 let span = Span::test_data();
5535 let res = val.insert_data_at_cell_path(
5536 &[PathMember::test_int(1, false)],
5537 Value::test_int(2),
5538 span,
5539 );
5540 assert_eq!(res, Ok(()));
5541 assert_eq!(
5542 val,
5543 Value::test_list(vec![Value::test_int(1), Value::test_int(2)])
5544 );
5545 }
5546
5547 #[test]
5548 fn insert_beyond_end_errors() {
5549 let mut val = Value::test_list(vec![Value::test_int(1)]);
5550 let span = Span::test_data();
5551 let res = val.insert_data_at_cell_path(
5552 &[PathMember::test_int(5, false)],
5553 Value::test_int(2),
5554 span,
5555 );
5556 assert!(matches!(
5557 res,
5558 Err(ShellError::InsertAfterNextFreeIndex { .. })
5559 ));
5560 }
5561
5562 #[test]
5563 fn insert_existing_column_in_table_errors() {
5564 let span = Span::test_data();
5565 let mut val =
5566 Value::test_list(vec![record!("x" => Value::test_int(1)).into_value(span)]);
5567 let res = val.insert_data_at_cell_path(
5568 &[PathMember::test_string("x", false, Casing::Sensitive)],
5569 Value::test_int(0),
5570 span,
5571 );
5572 assert!(matches!(res, Err(ShellError::ColumnAlreadyExists { .. })));
5573 }
5574
5575 #[test]
5576 fn insert_new_column_in_table() {
5577 let span = Span::test_data();
5578 let mut val =
5579 Value::test_list(vec![record!("x" => Value::test_int(1)).into_value(span)]);
5580 let res = val.insert_data_at_cell_path(
5581 &[PathMember::test_string("y", false, Casing::Sensitive)],
5582 Value::test_int(2),
5583 span,
5584 );
5585 assert_eq!(res, Ok(()));
5586 assert_eq!(
5587 val,
5588 Value::test_list(vec![
5589 record!("x" => Value::test_int(1), "y" => Value::test_int(2)).into_value(span),
5590 ])
5591 );
5592 }
5593 }
5594
5595 mod is_empty {
5596 use super::*;
5597
5598 #[test]
5599 fn test_string() {
5600 let value = Value::test_string("");
5601 assert!(value.is_empty());
5602 }
5603
5604 #[test]
5605 fn test_list() {
5606 let list_with_no_values = Value::test_list(vec![]);
5607 let list_with_one_empty_string = Value::test_list(vec![Value::test_string("")]);
5608
5609 assert!(list_with_no_values.is_empty());
5610 assert!(!list_with_one_empty_string.is_empty());
5611 }
5612
5613 #[test]
5614 fn test_record() {
5615 let no_columns_nor_cell_values = Value::test_record(Record::new());
5616
5617 let one_column_and_one_cell_value_with_empty_strings = Value::test_record(record! {
5618 "" => Value::test_string(""),
5619 });
5620
5621 let one_column_with_a_string_and_one_cell_value_with_empty_string =
5622 Value::test_record(record! {
5623 "column" => Value::test_string(""),
5624 });
5625
5626 let one_column_with_empty_string_and_one_value_with_a_string =
5627 Value::test_record(record! {
5628 "" => Value::test_string("text"),
5629 });
5630
5631 assert!(no_columns_nor_cell_values.is_empty());
5632 assert!(!one_column_and_one_cell_value_with_empty_strings.is_empty());
5633 assert!(!one_column_with_a_string_and_one_cell_value_with_empty_string.is_empty());
5634 assert!(!one_column_with_empty_string_and_one_value_with_a_string.is_empty());
5635 }
5636 }
5637
5638 mod get_type {
5639 use crate::Type;
5640
5641 use super::*;
5642
5643 #[test]
5644 fn test_list() {
5645 let list_of_ints = Value::test_list(vec![Value::test_int(0)]);
5646 let list_of_floats = Value::test_list(vec![Value::test_float(0.0)]);
5647 let list_of_ints_and_floats =
5648 Value::test_list(vec![Value::test_int(0), Value::test_float(0.0)]);
5649 let list_of_ints_and_floats_and_bools = Value::test_list(vec![
5650 Value::test_int(0),
5651 Value::test_float(0.0),
5652 Value::test_bool(false),
5653 ]);
5654 assert_eq!(list_of_ints.get_type(), Type::List(Box::new(Type::Int)));
5655 assert_eq!(list_of_floats.get_type(), Type::List(Box::new(Type::Float)));
5656 assert_eq!(
5657 list_of_ints_and_floats_and_bools.get_type(),
5658 Type::List(Box::new(Type::one_of([Type::Number, Type::Bool])))
5659 );
5660 assert_eq!(
5661 list_of_ints_and_floats.get_type(),
5662 Type::List(Box::new(Type::Number))
5663 );
5664 }
5665 }
5666
5667 mod is_subtype {
5668 use crate::{CompareTypes, Type};
5669
5670 use super::*;
5671
5672 #[track_caller]
5673 fn assert_subtype_equivalent(value: &Value, ty: &Type) {
5674 assert_eq!(value.is_subtype_of(ty), value.get_type().is_subtype_of(ty));
5675 }
5676
5677 #[test]
5678 fn test_list() {
5679 let ty_int_list = Type::list(Type::Int);
5680 let ty_str_list = Type::list(Type::String);
5681 let ty_any_list = Type::list(Type::Any);
5682 let ty_list_list_int = Type::list(Type::list(Type::Int));
5683
5684 let list = Value::test_list(vec![
5685 Value::test_int(1),
5686 Value::test_int(2),
5687 Value::test_int(3),
5688 ]);
5689
5690 assert_subtype_equivalent(&list, &ty_int_list);
5691 assert_subtype_equivalent(&list, &ty_str_list);
5692 assert_subtype_equivalent(&list, &ty_any_list);
5693
5694 let list = Value::test_list(vec![
5695 Value::test_int(1),
5696 Value::test_string("hi"),
5697 Value::test_int(3),
5698 ]);
5699
5700 assert_subtype_equivalent(&list, &ty_int_list);
5701 assert_subtype_equivalent(&list, &ty_str_list);
5702 assert_subtype_equivalent(&list, &ty_any_list);
5703
5704 let list = Value::test_list(vec![Value::test_list(vec![Value::test_int(1)])]);
5705
5706 assert_subtype_equivalent(&list, &ty_list_list_int);
5707
5708 let ty_table = {
5710 Type::Table(
5711 vec![
5712 ("a".into(), Type::Int),
5713 ("b".into(), Type::Int),
5714 ("c".into(), Type::Int),
5715 ]
5716 .into(),
5717 )
5718 };
5719 let empty = Value::test_list(vec![]);
5720
5721 assert_subtype_equivalent(&empty, &ty_any_list);
5722 assert!(empty.is_subtype_of(&ty_int_list));
5723 assert!(empty.is_subtype_of(&ty_table));
5724 }
5725
5726 #[test]
5727 fn test_record() {
5728 let ty_abc = {
5729 Type::Record(
5730 vec![
5731 ("a".into(), Type::Int),
5732 ("b".into(), Type::Int),
5733 ("c".into(), Type::Int),
5734 ]
5735 .into(),
5736 )
5737 };
5738 let ty_ab = Type::Record(vec![("a".into(), Type::Int), ("b".into(), Type::Int)].into());
5739 let ty_inner = Type::Record(vec![("inner".into(), ty_abc.clone())].into());
5740
5741 let record_abc = Value::test_record(record! {
5742 "a" => Value::test_int(1),
5743 "b" => Value::test_int(2),
5744 "c" => Value::test_int(3),
5745 });
5746 let record_ab = Value::test_record(record! {
5747 "a" => Value::test_int(1),
5748 "b" => Value::test_int(2),
5749 });
5750
5751 assert_subtype_equivalent(&record_abc, &ty_abc);
5752 assert_subtype_equivalent(&record_abc, &ty_ab);
5753 assert_subtype_equivalent(&record_ab, &ty_abc);
5754 assert_subtype_equivalent(&record_ab, &ty_ab);
5755
5756 let record_inner = Value::test_record(record! {
5757 "inner" => record_abc
5758 });
5759 assert_subtype_equivalent(&record_inner, &ty_inner);
5760 }
5761
5762 #[test]
5763 fn test_table() {
5764 let ty_abc = Type::Table(
5765 vec![
5766 ("a".into(), Type::Int),
5767 ("b".into(), Type::Int),
5768 ("c".into(), Type::Int),
5769 ]
5770 .into(),
5771 );
5772 let ty_ab = Type::Table(vec![("a".into(), Type::Int), ("b".into(), Type::Int)].into());
5773 let ty_list_any = Type::list(Type::Any);
5774
5775 let record_abc = Value::test_record(record! {
5776 "a" => Value::test_int(1),
5777 "b" => Value::test_int(2),
5778 "c" => Value::test_int(3),
5779 });
5780 let record_ab = Value::test_record(record! {
5781 "a" => Value::test_int(1),
5782 "b" => Value::test_int(2),
5783 });
5784
5785 let table_abc = Value::test_list(vec![record_abc.clone(), record_abc.clone()]);
5786 let table_ab = Value::test_list(vec![record_ab.clone(), record_ab.clone()]);
5787
5788 assert_subtype_equivalent(&table_abc, &ty_abc);
5789 assert_subtype_equivalent(&table_abc, &ty_ab);
5790 assert_subtype_equivalent(&table_ab, &ty_abc);
5791 assert_subtype_equivalent(&table_ab, &ty_ab);
5792 assert_subtype_equivalent(&table_abc, &ty_list_any);
5793
5794 let table_mixed = Value::test_list(vec![record_abc.clone(), record_ab.clone()]);
5795 assert_subtype_equivalent(&table_mixed, &ty_abc);
5796 assert!(table_mixed.is_subtype_of(&ty_ab));
5797
5798 let ty_a = Type::Table(vec![("a".into(), Type::Any)].into());
5799 let table_mixed_types = Value::test_list(vec![
5800 Value::test_record(record! {
5801 "a" => Value::test_int(1),
5802 }),
5803 Value::test_record(record! {
5804 "a" => Value::test_string("a"),
5805 }),
5806 ]);
5807 assert!(table_mixed_types.is_subtype_of(&ty_a));
5808 }
5809 }
5810
5811 mod into_string {
5812 use chrono::{DateTime, FixedOffset};
5813
5814 use super::*;
5815
5816 #[test]
5817 fn test_datetime() {
5818 let date = DateTime::from_timestamp_millis(-123456789)
5819 .unwrap()
5820 .with_timezone(&FixedOffset::east_opt(0).unwrap());
5821
5822 let string = Value::test_date(date).to_expanded_string("", &Default::default());
5823
5824 let formatted = string.split('(').next().unwrap();
5827 assert_eq!("Tue, 30 Dec 1969 13:42:23 +0000 ", formatted);
5828 }
5829
5830 #[test]
5831 fn test_negative_year_datetime() {
5832 let date = DateTime::from_timestamp_millis(-72135596800000)
5833 .unwrap()
5834 .with_timezone(&FixedOffset::east_opt(0).unwrap());
5835
5836 let string = Value::test_date(date).to_expanded_string("", &Default::default());
5837
5838 let formatted = string.split(' ').next().unwrap();
5841 assert_eq!("-0316-02-11T06:13:20+00:00", formatted);
5842 }
5843 }
5844
5845 #[test]
5846 fn test_env_as_bool() {
5847 assert_eq!(Value::test_bool(false).coerce_bool(), Ok(false));
5849 assert_eq!(Value::test_int(0).coerce_bool(), Ok(false));
5850 assert_eq!(Value::test_float(0.0).coerce_bool(), Ok(false));
5851 assert_eq!(Value::test_string("").coerce_bool(), Ok(false));
5852 assert_eq!(Value::test_string("0").coerce_bool(), Ok(false));
5853 assert_eq!(Value::test_nothing().coerce_bool(), Ok(false));
5854
5855 assert_eq!(Value::test_bool(true).coerce_bool(), Ok(true));
5857 assert_eq!(Value::test_int(1).coerce_bool(), Ok(true));
5858 assert_eq!(Value::test_float(1.0).coerce_bool(), Ok(true));
5859 assert_eq!(Value::test_string("1").coerce_bool(), Ok(true));
5860
5861 assert_eq!(Value::test_int(42).coerce_bool(), Ok(true));
5863 assert_eq!(Value::test_float(0.5).coerce_bool(), Ok(true));
5864 assert_eq!(Value::test_string("not zero").coerce_bool(), Ok(true));
5865
5866 assert!(Value::test_record(Record::default()).coerce_bool().is_err());
5868 assert!(
5869 Value::test_list(vec![Value::test_int(1)])
5870 .coerce_bool()
5871 .is_err()
5872 );
5873 assert!(
5874 Value::test_date(
5875 chrono::DateTime::parse_from_rfc3339("2024-01-01T12:00:00+00:00").unwrap(),
5876 )
5877 .coerce_bool()
5878 .is_err()
5879 );
5880 assert!(Value::test_glob("*.rs").coerce_bool().is_err());
5881 assert!(Value::test_binary(vec![1, 2, 3]).coerce_bool().is_err());
5882 assert!(Value::test_duration(3600).coerce_bool().is_err());
5883 }
5884
5885 mod list {
5886 use super::*;
5887 use crate::ast::PathMember;
5888 use nu_utils::SharedCow;
5889
5890 #[test]
5891 fn clone_shares_data() {
5892 let value = Value::test_list(vec![Value::test_int(1), Value::test_int(2)]);
5893 let clone = value.clone();
5894
5895 let (
5896 Value::List { vals, .. },
5897 Value::List {
5898 vals: cloned_vals, ..
5899 },
5900 ) = (&value, &clone)
5901 else {
5902 unreachable!();
5903 };
5904
5905 assert_eq!(SharedCow::ref_count(vals), 2);
5906 assert!(std::ptr::eq(vals.as_ptr(), cloned_vals.as_ptr()));
5907 }
5908
5909 #[test]
5910 fn mutation_is_copy_on_write() {
5911 let value = Value::test_list(vec![Value::test_int(1), Value::test_int(2)]);
5912 let mut clone = value.clone();
5913
5914 clone
5915 .upsert_data_at_cell_path(&[PathMember::test_int(0, false)], Value::test_int(3))
5916 .unwrap();
5917
5918 assert_eq!(
5919 value.as_list(),
5920 Ok([Value::test_int(1), Value::test_int(2)].as_slice())
5921 );
5922 assert_eq!(
5923 clone.as_list(),
5924 Ok([Value::test_int(3), Value::test_int(2)].as_slice())
5925 );
5926 }
5927
5928 #[test]
5929 fn into_list_preserves_shared_value() {
5930 let value = Value::test_list(vec![Value::test_int(1), Value::test_int(2)]);
5931 let clone = value.clone();
5932
5933 assert_eq!(
5934 clone.into_list(),
5935 Ok(vec![Value::test_int(1), Value::test_int(2)])
5936 );
5937 assert_eq!(
5938 value.as_list(),
5939 Ok([Value::test_int(1), Value::test_int(2)].as_slice())
5940 );
5941 }
5942 }
5943
5944 mod binary {
5945 use super::*;
5946 use nu_utils::SharedCow;
5947
5948 #[test]
5949 fn clone_shares_data() {
5950 let value = Value::test_binary(vec![1, 2, 3]);
5951 let clone = value.clone();
5952
5953 let (
5954 Value::Binary { val, .. },
5955 Value::Binary {
5956 val: cloned_val, ..
5957 },
5958 ) = (&value, &clone)
5959 else {
5960 unreachable!();
5961 };
5962
5963 assert_eq!(SharedCow::ref_count(val), 2);
5964 assert!(std::ptr::eq(val.as_ptr(), cloned_val.as_ptr()));
5965 }
5966
5967 #[test]
5968 fn mutation_is_copy_on_write() {
5969 let value = Value::test_binary(vec![1, 2, 3]);
5970 let mut clone = value.clone();
5971
5972 let Value::Binary { val, .. } = &mut clone else {
5973 unreachable!();
5974 };
5975 val.to_mut()[0] = 4;
5976
5977 assert_eq!(value.as_binary(), Ok([1, 2, 3].as_slice()));
5978 assert_eq!(clone.as_binary(), Ok([4, 2, 3].as_slice()));
5979 }
5980
5981 #[test]
5982 fn into_binary_preserves_shared_value() {
5983 let value = Value::test_binary(vec![1, 2, 3]);
5984 let clone = value.clone();
5985
5986 assert_eq!(clone.into_binary(), Ok(vec![1, 2, 3]));
5987 assert_eq!(value.as_binary(), Ok([1, 2, 3].as_slice()));
5988 }
5989 }
5990
5991 mod memory_size {
5992 use super::*;
5993
5994 #[test]
5995 fn test_primitive_sizes() {
5996 let base_size = std::mem::size_of::<Value>();
5998
5999 assert_eq!(Value::test_bool(true).memory_size(), base_size);
6000 assert_eq!(Value::test_int(42).memory_size(), base_size);
6001 assert_eq!(Value::test_float(1.5).memory_size(), base_size);
6002 assert_eq!(Value::test_nothing().memory_size(), base_size);
6003 }
6004
6005 #[test]
6006 fn test_string_size() {
6007 let s = "hello world";
6008 let val = Value::test_string(s);
6009 let base_size = std::mem::size_of::<Value>();
6010 let string_val = String::from(s);
6012 let expected = base_size + string_val.capacity();
6013 assert_eq!(val.memory_size(), expected);
6014 }
6015
6016 #[test]
6017 fn test_binary_size() {
6018 let data = vec![1, 2, 3, 4, 5];
6019 let val = Value::test_binary(data.clone());
6020 let base_size = std::mem::size_of::<Value>();
6021 let expected = base_size + data.capacity();
6022 assert_eq!(val.memory_size(), expected);
6023 }
6024
6025 #[test]
6026 fn test_list_size() {
6027 let list = Value::test_list(vec![
6028 Value::test_int(1),
6029 Value::test_int(2),
6030 Value::test_int(3),
6031 ]);
6032
6033 let base_size = std::mem::size_of::<Value>();
6034 let element_size = std::mem::size_of::<Value>();
6035 let expected = base_size + 3 * element_size;
6037 assert_eq!(list.memory_size(), expected);
6038 }
6039
6040 #[test]
6041 fn test_record_size() {
6042 let record = Value::test_record(record! {
6043 "a" => Value::test_int(1),
6044 "b" => Value::test_string("hello"),
6045 });
6046
6047 let base_size = std::mem::size_of::<Value>();
6048 let record_base_size = std::mem::size_of::<Record>();
6049 let key1_size = String::from("a").capacity();
6050 let key2_size = String::from("b").capacity();
6051 let val1_size = std::mem::size_of::<Value>();
6052 let val2_base_size = std::mem::size_of::<Value>();
6053 let val2_string_size = String::from("hello").capacity();
6054
6055 let expected = base_size
6056 + record_base_size
6057 + key1_size
6058 + key2_size
6059 + val1_size
6060 + (val2_base_size + val2_string_size);
6061 assert_eq!(record.memory_size(), expected);
6062 }
6063
6064 #[test]
6065 fn test_nested_structure_size() {
6066 let inner_record = Value::test_record(record! {
6068 "x" => Value::test_int(10),
6069 "y" => Value::test_string("test"),
6070 });
6071
6072 let list = Value::test_list(vec![inner_record]);
6073
6074 let record_size = list.memory_size();
6075 let base_size = std::mem::size_of::<Value>();
6077 assert!(record_size > base_size);
6078
6079 let simple_list = Value::test_list(vec![Value::test_int(1)]);
6081 assert!(record_size > simple_list.memory_size());
6082 }
6083 }
6084
6085 mod hash {
6086 use super::*;
6087 use crate::ast::{CellPath, PathMember};
6088 use crate::engine::Closure;
6089 use crate::value::HASH_ITEM_LIMIT;
6090 use crate::{
6091 BlockId, CustomValue, Filesize, Range, Record, ShellError, Span, VarId, casing::Casing,
6092 };
6093 use chrono::FixedOffset;
6094 use indexmap::IndexMap;
6095 use serde::{Deserialize, Serialize};
6096 use std::any::Any;
6097 use std::cmp::Ordering;
6098 use std::collections::hash_map::DefaultHasher;
6099 use std::hash::{Hash, Hasher};
6100 use std::ops::Bound;
6101
6102 fn hash_value(val: &Value) -> u64 {
6103 let mut hasher = DefaultHasher::new();
6104 val.hash(&mut hasher);
6105 hasher.finish()
6106 }
6107
6108 fn assert_strict_eq_same_hash(a: &Value, b: &Value) {
6110 assert!(a.strict_eq(b), "{a:?} should strict_eq {b:?}");
6111 assert!(b.strict_eq(a), "{b:?} should strict_eq {a:?}");
6112 assert_eq!(hash_value(a), hash_value(b));
6113 }
6114
6115 fn assert_strict_ne(a: &Value, b: &Value) {
6116 assert!(!a.strict_eq(b), "{a:?} should not strict_eq {b:?}");
6117 assert!(!b.strict_eq(a), "{b:?} should not strict_eq {a:?}");
6118 }
6119
6120 fn wide_record(keys: impl IntoIterator<Item = &'static str>) -> Value {
6121 Value::test_record(
6122 keys.into_iter()
6123 .map(|key| (key.to_string(), Value::test_string(key)))
6124 .collect(),
6125 )
6126 }
6127
6128 fn record_with_duplicates(pairs: &[(&str, i64)]) -> Value {
6130 let mut record = Record::new();
6131 for (key, val) in pairs {
6132 record.push(*key, Value::test_int(*val));
6133 }
6134 Value::test_record(record)
6135 }
6136
6137 struct Key(Value);
6140
6141 impl PartialEq for Key {
6142 fn eq(&self, other: &Self) -> bool {
6143 self.0.strict_eq(&other.0)
6144 }
6145 }
6146
6147 impl Eq for Key {}
6148
6149 impl Hash for Key {
6150 fn hash<H: Hasher>(&self, state: &mut H) {
6151 self.0.hash(state);
6152 }
6153 }
6154
6155 #[derive(Debug, Clone, Serialize, Deserialize)]
6156 struct PrefixedCustom {
6157 version: u64,
6158 prefix: String,
6159 }
6160
6161 #[typetag::serde]
6162 impl CustomValue for PrefixedCustom {
6163 fn clone_value(&self, span: Span) -> Value {
6164 Value::custom(Box::new(self.clone()), span)
6165 }
6166
6167 fn type_name(&self) -> String {
6168 "prefixed".into()
6169 }
6170
6171 fn to_base_value(&self, span: Span) -> Result<Value, ShellError> {
6172 Ok(Value::string(
6173 format!("{}{}", self.prefix, self.version),
6174 span,
6175 ))
6176 }
6177
6178 fn as_any(&self) -> &dyn Any {
6179 self
6180 }
6181
6182 fn as_mut_any(&mut self) -> &mut dyn Any {
6183 self
6184 }
6185
6186 fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
6187 other
6188 .as_custom_value()
6189 .ok()?
6190 .as_any()
6191 .downcast_ref::<Self>()
6192 .and_then(|other| self.version.partial_cmp(&other.version))
6193 }
6194
6195 fn hash_value(&self, mut state: &mut dyn Hasher) {
6196 self.version.hash(&mut state);
6197 }
6198 }
6199
6200 #[derive(Debug, Clone, Serialize, Deserialize)]
6203 struct Opaque {
6204 loose: bool,
6205 }
6206
6207 #[typetag::serde]
6208 impl CustomValue for Opaque {
6209 fn clone_value(&self, span: Span) -> Value {
6210 Value::custom(Box::new(self.clone()), span)
6211 }
6212
6213 fn type_name(&self) -> String {
6214 "opaque".into()
6215 }
6216
6217 fn to_base_value(&self, span: Span) -> Result<Value, ShellError> {
6218 Ok(Value::nothing(span))
6219 }
6220
6221 fn as_any(&self) -> &dyn Any {
6222 self
6223 }
6224
6225 fn as_mut_any(&mut self) -> &mut dyn Any {
6226 self
6227 }
6228
6229 fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
6230 (self.loose && other.as_custom_value().is_ok()).then_some(Ordering::Equal)
6231 }
6232 }
6233
6234 #[test]
6235 fn same_value_same_hash() {
6236 assert_strict_eq_same_hash(&Value::test_int(1), &Value::test_int(1));
6237 assert_strict_eq_same_hash(&Value::test_string("hello"), &Value::test_string("hello"));
6238 assert_strict_eq_same_hash(&Value::test_bool(true), &Value::test_bool(true));
6239 assert_strict_eq_same_hash(&Value::test_nothing(), &Value::test_nothing());
6240 }
6241
6242 #[test]
6243 fn different_values_different_hash() {
6244 assert_strict_ne(&Value::test_int(1), &Value::test_int(2));
6245 assert_ne!(
6246 hash_value(&Value::test_int(1)),
6247 hash_value(&Value::test_int(2))
6248 );
6249 assert_strict_ne(&Value::test_string("a"), &Value::test_string("b"));
6250 assert_ne!(
6251 hash_value(&Value::test_string("a")),
6252 hash_value(&Value::test_string("b"))
6253 );
6254 }
6255
6256 #[test]
6257 fn bool_and_string_true_are_distinct() {
6258 assert_strict_ne(&Value::test_bool(true), &Value::test_string("true"));
6259 assert_ne!(
6260 hash_value(&Value::test_bool(true)),
6261 hash_value(&Value::test_string("true"))
6262 );
6263 }
6264
6265 #[test]
6266 fn nothing_and_empty_string_are_distinct() {
6267 assert_strict_ne(&Value::test_nothing(), &Value::test_string(""));
6268 assert_ne!(
6269 hash_value(&Value::test_nothing()),
6270 hash_value(&Value::test_string(""))
6271 );
6272 }
6273
6274 #[test]
6275 fn int_and_float_are_distinct() {
6276 let int = Value::test_int(1);
6277 let float = Value::test_float(1.0);
6278 assert_eq!(int, float);
6280 assert_strict_ne(&int, &float);
6281 assert_ne!(hash_value(&int), hash_value(&float));
6282 }
6283
6284 #[test]
6285 fn string_and_glob_are_distinct() {
6286 let string = Value::test_string("*.txt");
6287 let glob = Value::glob("*.txt", false, Span::test_data());
6288 assert_eq!(string, glob);
6289 assert_strict_ne(&string, &glob);
6290 assert_ne!(hash_value(&string), hash_value(&glob));
6291 }
6292
6293 #[test]
6294 fn glob_no_expand_is_part_of_identity() {
6295 let expand = Value::glob("*.txt", false, Span::test_data());
6296 let no_expand = Value::glob("*.txt", true, Span::test_data());
6297 assert_eq!(expand, no_expand);
6298 assert_strict_ne(&expand, &no_expand);
6299 assert_strict_eq_same_hash(&expand, &expand.clone());
6300 }
6301
6302 #[test]
6303 fn float_zero_and_negative_zero_are_equal() {
6304 assert_strict_eq_same_hash(&Value::test_float(0.0), &Value::test_float(-0.0));
6305 }
6306
6307 #[test]
6308 fn float_nan_equals_nan() {
6309 let nan = Value::test_float(f64::NAN);
6310 let same_nan = nan.clone();
6311 let other_payload = Value::test_float(f64::from_bits(f64::NAN.to_bits() | 1));
6312 assert_ne!(nan, same_nan, "Nushell `==` keeps IEEE NaN semantics");
6313 assert_strict_eq_same_hash(&nan, &same_nan);
6314 assert_strict_eq_same_hash(&nan, &other_payload);
6315 assert_strict_ne(&nan, &Value::test_float(1.0));
6316 }
6317
6318 #[test]
6319 fn float_infinities_hash_by_sign() {
6320 let pos = Value::test_float(f64::INFINITY);
6321 let neg = Value::test_float(f64::NEG_INFINITY);
6322 assert_strict_eq_same_hash(&pos, &pos);
6323 assert_strict_ne(&pos, &neg);
6324 assert_ne!(hash_value(&pos), hash_value(&neg));
6325 }
6326
6327 #[test]
6328 fn nearby_floats_are_distinct() {
6329 let a = Value::test_float(1.0);
6331 let b = Value::test_float(1.0 + f64::EPSILON);
6332 assert_eq!(a, b);
6333 assert_strict_ne(&a, &b);
6334 }
6335
6336 #[test]
6337 fn date_offset_does_not_affect_identity() {
6338 let utc = chrono::DateTime::parse_from_rfc3339("2020-01-01T00:00:00.123456789Z")
6339 .expect("rfc3339");
6340 let east = utc.with_timezone(&FixedOffset::east_opt(3600).expect("offset"));
6341 assert_strict_eq_same_hash(&Value::test_date(utc), &Value::test_date(east));
6342 }
6343
6344 #[test]
6345 fn date_subseconds_affect_identity() {
6346 let a = chrono::DateTime::parse_from_rfc3339("2020-01-01T00:00:00.000000000Z")
6347 .expect("rfc3339");
6348 let b = chrono::DateTime::parse_from_rfc3339("2020-01-01T00:00:00.000000001Z")
6349 .expect("rfc3339");
6350 assert_strict_ne(&Value::test_date(a), &Value::test_date(b));
6351 assert_ne!(
6352 hash_value(&Value::test_date(a)),
6353 hash_value(&Value::test_date(b))
6354 );
6355 }
6356
6357 #[test]
6358 fn custom_hash_matches_partial_cmp_not_base_value() {
6359 let a = Value::custom(
6360 Box::new(PrefixedCustom {
6361 version: 1,
6362 prefix: "v".into(),
6363 }),
6364 Span::test_data(),
6365 );
6366 let b = Value::custom(
6367 Box::new(PrefixedCustom {
6368 version: 1,
6369 prefix: String::new(),
6370 }),
6371 Span::test_data(),
6372 );
6373 let c = Value::custom(
6374 Box::new(PrefixedCustom {
6375 version: 2,
6376 prefix: String::new(),
6377 }),
6378 Span::test_data(),
6379 );
6380 assert_eq!(a, b);
6381 assert_strict_eq_same_hash(&a, &b);
6382 assert_strict_ne(&a, &c);
6383 }
6384
6385 #[test]
6386 fn custom_without_partial_cmp_is_not_strict_eq_to_itself() {
6387 let opaque = Value::custom(Box::new(Opaque { loose: false }), Span::test_data());
6388 assert!(!opaque.strict_eq(&opaque));
6389 }
6390
6391 #[test]
6392 fn custom_values_of_different_types_are_distinct() {
6393 let loose = Value::custom(Box::new(Opaque { loose: true }), Span::test_data());
6396 let prefixed = Value::custom(
6397 Box::new(PrefixedCustom {
6398 version: 1,
6399 prefix: String::new(),
6400 }),
6401 Span::test_data(),
6402 );
6403 assert_eq!(loose, prefixed);
6404 assert_strict_ne(&loose, &prefixed);
6405 assert_strict_eq_same_hash(&loose, &loose.clone());
6406 }
6407
6408 #[test]
6409 fn filesize_identity() {
6410 let a = Value::filesize(Filesize::new(1000), Span::test_data());
6411 let b = Value::filesize(Filesize::new(1000), Span::test_data());
6412 let c = Value::filesize(Filesize::new(2000), Span::test_data());
6413 assert_strict_eq_same_hash(&a, &b);
6414 assert_strict_ne(&a, &c);
6415 assert_ne!(hash_value(&a), hash_value(&c));
6416 }
6417
6418 #[test]
6419 fn duration_identity() {
6420 let a = Value::duration(1_000_000, Span::test_data());
6421 let b = Value::duration(1_000_000, Span::test_data());
6422 assert_strict_eq_same_hash(&a, &b);
6423 assert_strict_ne(&a, &Value::duration(1, Span::test_data()));
6424 }
6425
6426 #[test]
6427 fn range_int_identity() {
6428 let a = Value::range(
6429 Range::new_int(1, Some(2), Some(Bound::Included(5))),
6430 Span::test_data(),
6431 );
6432 let b = Value::range(
6433 Range::new_int(1, Some(2), Some(Bound::Included(5))),
6434 Span::test_data(),
6435 );
6436 let c = Value::range(
6437 Range::new_int(1, Some(3), Some(Bound::Included(5))),
6438 Span::test_data(),
6439 );
6440 assert_strict_eq_same_hash(&a, &b);
6441 assert_strict_ne(&a, &c);
6442 assert_ne!(hash_value(&a), hash_value(&c));
6443 }
6444
6445 #[test]
6446 fn range_int_and_float_are_distinct() {
6447 let int = Value::range(
6448 Range::new_int(1, Some(2), Some(Bound::Included(3))),
6449 Span::test_data(),
6450 );
6451 let float = Value::range(
6452 Range::new_float(1.0, Some(2.0), Some(Bound::Included(3.0))),
6453 Span::test_data(),
6454 );
6455 assert_eq!(int, float);
6458 assert_strict_ne(&int, &float);
6459 assert_ne!(hash_value(&int), hash_value(&float));
6460 }
6461
6462 #[test]
6463 fn range_negative_zero_is_equal() {
6464 let a = Value::range(
6465 Range::new_float(0.0, None, Some(Bound::Included(1.0))),
6466 Span::test_data(),
6467 );
6468 let b = Value::range(
6469 Range::new_float(-0.0, None, Some(Bound::Included(1.0))),
6470 Span::test_data(),
6471 );
6472 assert_strict_eq_same_hash(&a, &b);
6473 }
6474
6475 #[test]
6476 fn record_identity() {
6477 let a = Value::test_record(record! {
6478 "x" => Value::test_int(1),
6479 "y" => Value::test_int(2),
6480 });
6481 let b = Value::test_record(record! {
6482 "x" => Value::test_int(1),
6483 "y" => Value::test_int(2),
6484 });
6485 let other_key = Value::test_record(record! {
6486 "x" => Value::test_int(1),
6487 "z" => Value::test_int(2),
6488 });
6489 let other_value = Value::test_record(record! {
6490 "x" => Value::test_int(1),
6491 "y" => Value::test_float(2.0),
6492 });
6493 assert_strict_eq_same_hash(&a, &b);
6494 assert_strict_ne(&a, &other_key);
6495 assert_ne!(hash_value(&a), hash_value(&other_key));
6496 assert_strict_ne(&a, &other_value);
6497 assert_strict_ne(
6498 &a,
6499 &Value::test_record(record! { "x" => Value::test_int(1) }),
6500 );
6501 }
6502
6503 #[test]
6504 fn record_key_order_does_not_affect_identity() {
6505 let a = Value::test_record(record! {
6506 "a" => Value::test_int(1),
6507 "b" => Value::test_int(2),
6508 });
6509 let b = Value::test_record(record! {
6510 "b" => Value::test_int(2),
6511 "a" => Value::test_int(1),
6512 });
6513 assert_strict_eq_same_hash(&a, &b);
6514 }
6515
6516 #[test]
6517 fn wide_record_key_order_does_not_affect_identity() {
6518 let keys = ["g", "f", "e", "d", "c", "b", "a"];
6519 assert!(keys.len() > HASH_ITEM_LIMIT);
6520 let a = wide_record(keys);
6521 let b = wide_record(keys.into_iter().rev());
6522 assert_strict_eq_same_hash(&a, &b);
6523 }
6524
6525 #[test]
6526 fn wide_records_differing_past_hash_limit_collide_but_are_distinct() {
6527 let keys = ["a", "b", "c", "d", "e", "f"];
6529 assert_eq!(keys.len(), HASH_ITEM_LIMIT + 1);
6530 let a = wide_record(keys);
6531 let b = wide_record(["a", "b", "c", "d", "e", "z"]);
6532 assert_strict_ne(&a, &b);
6533 assert_eq!(hash_value(&a), hash_value(&b));
6534 }
6535
6536 #[test]
6537 fn record_with_duplicate_keys_is_reflexive() {
6538 let dup = record_with_duplicates(&[("a", 1), ("a", 2)]);
6539 assert_strict_eq_same_hash(&dup, &dup.clone());
6540 assert_strict_ne(&dup, &record_with_duplicates(&[("a", 2), ("a", 1)]));
6541 assert_strict_ne(
6542 &record_with_duplicates(&[("a", 1), ("a", 1)]),
6543 &record_with_duplicates(&[("a", 1), ("b", 1)]),
6544 );
6545 }
6546
6547 #[test]
6548 fn record_with_duplicate_keys_ignores_key_order() {
6549 let keys = [("a", 1), ("b", 0), ("a", 2), ("c", 0), ("d", 0), ("e", 0)];
6551 let moved = [("e", 0), ("d", 0), ("c", 0), ("a", 1), ("a", 2), ("b", 0)];
6552 assert_strict_eq_same_hash(
6553 &record_with_duplicates(&keys),
6554 &record_with_duplicates(&moved),
6555 );
6556 }
6557
6558 #[test]
6559 fn list_identity() {
6560 let a = Value::test_list(vec![Value::test_int(1), Value::test_int(2)]);
6561 let b = Value::test_list(vec![Value::test_int(1), Value::test_int(2)]);
6562 let c = Value::test_list(vec![Value::test_int(1), Value::test_int(3)]);
6563 let d = Value::test_list(vec![Value::test_int(1), Value::test_float(2.0)]);
6564 assert_strict_eq_same_hash(&a, &b);
6565 assert_strict_ne(&a, &c);
6566 assert_ne!(hash_value(&a), hash_value(&c));
6567 assert_strict_ne(&a, &d);
6568 assert_strict_ne(&a, &Value::test_list(vec![Value::test_int(1)]));
6569 }
6570
6571 #[test]
6572 fn long_lists_hash_both_ends() {
6573 let len = 2 * HASH_ITEM_LIMIT + 1;
6575 let list = |index: usize, changed: i64| {
6576 Value::test_list(
6577 (0..len)
6578 .map(|i| Value::test_int(if i == index { changed } else { i as i64 }))
6579 .collect(),
6580 )
6581 };
6582 let middle = HASH_ITEM_LIMIT;
6583 assert_strict_eq_same_hash(&list(middle, -1), &list(middle, -1));
6584 assert_strict_ne(&list(middle, -1), &list(middle, -2));
6585 assert_eq!(hash_value(&list(middle, -1)), hash_value(&list(middle, -2)));
6586 assert_ne!(
6588 hash_value(&list(len - 1, -1)),
6589 hash_value(&list(len - 1, -2))
6590 );
6591 }
6592
6593 #[test]
6594 fn cell_path_identity() {
6595 let path = |optional: bool, casing: Casing| {
6596 Value::cell_path(
6597 CellPath {
6598 members: vec![PathMember::test_string("a", optional, casing)],
6599 },
6600 Span::test_data(),
6601 )
6602 };
6603 assert_strict_eq_same_hash(
6604 &path(false, Casing::Sensitive),
6605 &path(false, Casing::Sensitive),
6606 );
6607 assert_strict_ne(
6608 &path(false, Casing::Sensitive),
6609 &path(true, Casing::Sensitive),
6610 );
6611 assert_strict_ne(
6613 &path(false, Casing::Sensitive),
6614 &path(false, Casing::Insensitive),
6615 );
6616 assert_ne!(
6617 path(false, Casing::Sensitive),
6618 path(false, Casing::Insensitive)
6619 );
6620 }
6621
6622 #[test]
6623 fn closure_identity() {
6624 let closure = |block: usize, captured: i64| {
6625 Value::closure(
6626 Closure {
6627 block_id: BlockId::new(block),
6628 captures: vec![(VarId::new(0), Value::test_int(captured))],
6629 },
6630 Span::test_data(),
6631 )
6632 };
6633 assert_strict_eq_same_hash(&closure(0, 1), &closure(0, 1));
6634 assert_strict_ne(&closure(0, 1), &closure(1, 1));
6635 assert_ne!(hash_value(&closure(0, 1)), hash_value(&closure(1, 1)));
6636 assert_eq!(closure(0, 1), closure(0, 2));
6638 assert_strict_ne(&closure(0, 1), &closure(0, 2));
6639 assert_ne!(hash_value(&closure(0, 1)), hash_value(&closure(0, 2)));
6640 }
6641
6642 #[test]
6643 fn error_identity() {
6644 let error = |to_type: &str| {
6645 Value::error(
6646 ShellError::CantConvert {
6647 to_type: to_type.into(),
6648 from_type: "string".into(),
6649 span: Span::test_data(),
6650 help: None,
6651 },
6652 Span::test_data(),
6653 )
6654 };
6655 assert_strict_eq_same_hash(&error("int"), &error("int"));
6656 assert_eq!(error("int"), error("float"));
6658 assert_strict_ne(&error("int"), &error("float"));
6659 }
6660
6661 #[test]
6662 fn binary_identity() {
6663 let a = Value::binary(vec![1, 2, 3], Span::test_data());
6664 let b = Value::binary(vec![1, 2, 3], Span::test_data());
6665 let c = Value::binary(vec![1, 2, 4], Span::test_data());
6666 assert_strict_eq_same_hash(&a, &b);
6667 assert_strict_ne(&a, &c);
6668 assert_ne!(hash_value(&a), hash_value(&c));
6669 }
6670
6671 #[test]
6672 fn map_keyed_by_strict_eq_groups_equal_values() {
6673 let int_range = Range::new_int(0, None, Some(Bound::Excluded(5)));
6674 let float_range = Range::new_float(0.0, None, Some(Bound::Excluded(5.0)));
6675 let groups = [
6676 vec![Value::test_int(1), Value::test_int(1)],
6677 vec![Value::test_float(1.0)],
6678 vec![Value::test_float(0.0), Value::test_float(-0.0)],
6679 vec![Value::test_float(f64::NAN), Value::test_float(f64::NAN)],
6680 vec![Value::test_string("x")],
6681 vec![Value::glob("x", false, Span::test_data())],
6682 vec![
6683 Value::range(int_range, Span::test_data()),
6684 Value::range(int_range, Span::test_data()),
6685 ],
6686 vec![Value::range(float_range, Span::test_data())],
6687 vec![
6688 record_with_duplicates(&[("a", 1), ("b", 2)]),
6689 record_with_duplicates(&[("b", 2), ("a", 1)]),
6690 ],
6691 vec![
6692 record_with_duplicates(&[("a", 1), ("a", 2)]),
6693 record_with_duplicates(&[("a", 1), ("a", 2)]),
6694 ],
6695 vec![
6696 wide_record(["a", "b", "c", "d", "e", "f"]),
6697 wide_record(["f", "e", "d", "c", "b", "a"]),
6698 ],
6699 vec![wide_record(["a", "b", "c", "d", "e", "z"])],
6700 ];
6701
6702 let mut map = IndexMap::<Key, usize>::new();
6703 for value in groups.iter().flatten() {
6704 *map.entry(Key(value.clone())).or_default() += 1;
6705 }
6706 let counts: Vec<usize> = map.values().copied().collect();
6707 let expected: Vec<usize> = groups.iter().map(Vec::len).collect();
6708 assert_eq!(counts, expected);
6709 }
6710
6711 struct Rng(u64);
6714
6715 impl Rng {
6716 fn below(&mut self, n: usize) -> usize {
6717 self.0 ^= self.0 << 13;
6718 self.0 ^= self.0 >> 7;
6719 self.0 ^= self.0 << 17;
6720 (self.0 % n as u64) as usize
6721 }
6722
6723 fn pick<T: Copy>(&mut self, items: &[T]) -> T {
6724 items[self.below(items.len())]
6725 }
6726 }
6727
6728 fn random_value(rng: &mut Rng, depth: usize) -> Value {
6731 let kinds = if depth == 0 { 6 } else { 9 };
6732 match rng.below(kinds) {
6733 0 => Value::test_int(rng.below(3) as i64),
6734 1 => Value::test_float(rng.pick(&[0.0, -0.0, 1.0, f64::NAN, f64::INFINITY])),
6735 2 => Value::test_string(rng.pick(&["a", "b"])),
6736 3 => Value::glob(rng.pick(&["a", "b"]), rng.below(2) == 0, Span::test_data()),
6737 4 => {
6738 let date = chrono::DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
6739 .expect("rfc3339");
6740 Value::test_date(date + chrono::Duration::nanoseconds(rng.below(2) as i64))
6741 }
6742 5 => Value::range(
6743 Range::new_float(rng.pick(&[0.0, -0.0]), None, Some(Bound::Included(1.0))),
6744 Span::test_data(),
6745 ),
6746 6 => {
6747 let mut record = Record::new();
6748 for _ in 0..rng.below(2 * HASH_ITEM_LIMIT) {
6749 let key = rng.pick(&["a", "b", "c", "d", "e", "f", "g"]);
6750 record.push(key, random_value(rng, depth - 1));
6751 }
6752 Value::test_record(record)
6753 }
6754 7 => Value::test_list(
6755 (0..rng.below(3 * HASH_ITEM_LIMIT))
6756 .map(|_| random_value(rng, depth - 1))
6757 .collect(),
6758 ),
6759 _ => Value::closure(
6760 Closure {
6761 block_id: BlockId::new(0),
6762 captures: (0..rng.below(2 * HASH_ITEM_LIMIT))
6763 .map(|_| (VarId::new(rng.below(2)), random_value(rng, depth - 1)))
6764 .collect(),
6765 },
6766 Span::test_data(),
6767 ),
6768 }
6769 }
6770
6771 fn other_float_bits(rng: &mut Rng, val: f64) -> f64 {
6774 if val == 0.0 {
6775 rng.pick(&[0.0, -0.0])
6776 } else if val.is_nan() {
6777 rng.pick(&[f64::NAN, -f64::NAN, f64::from_bits(f64::NAN.to_bits() | 1)])
6778 } else {
6779 val
6780 }
6781 }
6782
6783 fn equivalent_rewrite(rng: &mut Rng, value: &Value) -> Value {
6787 let span = Span::new(rng.below(100), 100 + rng.below(100));
6788 match value {
6789 Value::Float { val, .. } => Value::float(other_float_bits(rng, *val), span),
6790 Value::Date { val, .. } => {
6791 let offset = FixedOffset::east_opt(rng.pick(&[3600, -7200])).expect("offset");
6792 Value::date(val.with_timezone(&offset), span)
6793 }
6794 Value::Range { val, .. } => match **val {
6797 Range::FloatRange(range) => Value::range(
6798 Range::new_float(
6799 other_float_bits(rng, range.start()),
6800 None,
6801 Some(range.end()),
6802 ),
6803 span,
6804 ),
6805 Range::IntRange(_) => Value::range(**val, span),
6806 },
6807 Value::Record { val, .. } => {
6808 let mut entries: Vec<(String, Value)> = val
6809 .iter()
6810 .map(|(key, item)| (key.clone(), equivalent_rewrite(rng, item)))
6811 .collect();
6812 let salt = rng.below(usize::MAX);
6815 entries.sort_by_key(|(key, _)| {
6816 let mut hasher = DefaultHasher::new();
6817 (salt, key).hash(&mut hasher);
6818 hasher.finish()
6819 });
6820 Value::record(entries.into_iter().collect(), span)
6821 }
6822 Value::List { vals, .. } => Value::list(
6823 vals.iter()
6824 .map(|item| equivalent_rewrite(rng, item))
6825 .collect(),
6826 span,
6827 ),
6828 Value::Closure { val, .. } => Value::closure(
6829 Closure {
6830 block_id: val.block_id,
6831 captures: val
6832 .captures
6833 .iter()
6834 .map(|(id, item)| (*id, equivalent_rewrite(rng, item)))
6835 .collect(),
6836 },
6837 span,
6838 ),
6839 other => other.clone().with_span(span),
6840 }
6841 }
6842
6843 #[test]
6844 fn random_values_hash_like_their_equivalent_rewrites() {
6845 let mut rng = Rng(0x5EED);
6846 for _ in 0..1_000 {
6847 let value = random_value(&mut rng, 3);
6848 let rewrite = equivalent_rewrite(&mut rng, &value);
6849 assert_strict_eq_same_hash(&value, &rewrite);
6850 }
6851 }
6852 }
6853
6854 mod concat {
6855 use super::*;
6856 use crate::Span;
6857 use pretty_assertions::assert_eq;
6858
6859 #[test]
6860 fn empty_lhs_clones_rhs() {
6861 let empty = Value::test_list(vec![]);
6862 let rhs = Value::test_list(vec![Value::test_int(1), Value::test_int(2)]);
6863 let out = empty
6864 .concat(Span::test_data(), &rhs, Span::test_data())
6865 .expect("concat");
6866 assert_eq!(out, rhs.with_span(Span::test_data()));
6867 }
6868
6869 #[test]
6870 fn empty_rhs_clones_lhs() {
6871 let lhs = Value::test_list(vec![Value::test_int(1), Value::test_int(2)]);
6872 let empty = Value::test_list(vec![]);
6873 let out = lhs
6874 .concat(Span::test_data(), &empty, Span::test_data())
6875 .expect("concat");
6876 assert_eq!(out, lhs.with_span(Span::test_data()));
6877 }
6878
6879 #[test]
6880 fn both_nonempty_appends() {
6881 let lhs = Value::test_list(vec![Value::test_int(1)]);
6882 let rhs = Value::test_list(vec![Value::test_int(2)]);
6883 let out = lhs
6884 .concat(Span::test_data(), &rhs, Span::test_data())
6885 .expect("concat");
6886 assert_eq!(
6887 out,
6888 Value::test_list(vec![Value::test_int(1), Value::test_int(2)])
6889 );
6890 }
6891
6892 #[test]
6893 fn both_empty() {
6894 let empty = Value::test_list(vec![]);
6895 let out = empty
6896 .concat(Span::test_data(), &empty, Span::test_data())
6897 .expect("concat");
6898 assert_eq!(out, Value::test_list(vec![]));
6899 }
6900 }
6901}