Handle inf/nan in delimited data
@@ -202,6 +202,14 @@ impl UntaggedValue {
pub fn decimal_from_float(f: f64, span: Span) -> UntaggedValue {
let dec = BigDecimal::from_f64(f);
+ // BigDecimal doesn't have the concept of inf/NaN so handle manually
+ if f.is_sign_negative() && f.is_infinite() {
+ UntaggedValue::from("-inf")
+ } else if f.is_infinite() {
+ UntaggedValue::from("inf")
+ } else if f.is_nan() {
+ UntaggedValue::from("NaN")
+ } else {
match dec {
Some(dec) => UntaggedValue::Primitive(Primitive::Decimal(dec)),
None => UntaggedValue::Error(ShellError::labeled_error(
@@ -211,6 +219,7 @@ impl UntaggedValue {
)),
}
}
+ }
/// Helper for creating binary (non-text) buffer values
pub fn binary(binary: Vec<u8>) -> UntaggedValue {
@@ -550,3 +559,28 @@ pub fn merge_descriptors(values: &[Value]) -> Vec<String> {
}
ret
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_decimal_from_float() {
+ assert_eq!(
+ UntaggedValue::from("inf"),
+ UntaggedValue::decimal_from_float(f64::INFINITY, Span::default())
+ );
+ assert_eq!(
+ UntaggedValue::from("-inf"),
+ UntaggedValue::decimal_from_float(f64::NEG_INFINITY, Span::default())
+ );
+ assert_eq!(
+ UntaggedValue::from("NaN"),
+ UntaggedValue::decimal_from_float(f64::NAN, Span::default())
+ );
+ assert_eq!(
+ UntaggedValue::from(5.5),
+ UntaggedValue::decimal_from_float(5.5, Span::default())
+ )
+ }
+}
