impl Value::Record from HashMap<String, Value>

This commit is contained in:
xiuxiu62 2021-10-12 14:54:28 -07:00
parent 1ea124a65b
commit 8fee0b32e7

View File

@ -11,10 +11,11 @@ use serde::{Deserialize, Serialize};
pub use stream::*;
pub use unit::*;
use std::collections::HashMap;
use std::{cmp::Ordering, fmt::Debug};
use crate::ast::{CellPath, PathMember};
use crate::{span, BlockId, Span, Type};
use crate::{span, BlockId, Span, Spanned, Type};
use crate::ShellError;
@ -1032,6 +1033,23 @@ impl Value {
}
}
/// Create a Value::Record from a spanned hashmap
impl From<Spanned<HashMap<String, Value>>> for Value {
fn from(input: Spanned<HashMap<String, Value>>) -> Self {
let span = input.span;
let (cols, vals) = input
.item
.into_iter()
.fold((vec![], vec![]), |mut acc, (k, v)| {
acc.0.push(k);
acc.1.push(v);
acc
});
Value::Record { cols, vals, span }
}
}
/// Format a duration in nanoseconds into a string
pub fn format_duration(duration: i64) -> String {
let (sign, duration) = if duration >= 0 {