From 8fee0b32e722a977e24ec446b8b5e8edeaa3f852 Mon Sep 17 00:00:00 2001 From: xiuxiu62 Date: Tue, 12 Oct 2021 14:54:28 -0700 Subject: [PATCH] impl Value::Record from HashMap --- crates/nu-protocol/src/value/mod.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 8eb7dd566f..e792e253fc 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -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>> for Value { + fn from(input: Spanned>) -> 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 {