nushell/crates/nu-cli/src/data/dict.rs

104 lines
2.8 KiB
Rust
Raw Normal View History

2019-05-16 00:58:44 +02:00
use crate::prelude::*;
use derive_new::new;
use nu_protocol::{Dictionary, Primitive, UntaggedValue, Value};
use nu_source::{b, PrettyDebug, Spanned};
2019-05-10 18:59:12 +02:00
2019-11-04 16:47:03 +01:00
#[derive(Debug, new)]
struct DebugEntry<'a> {
key: &'a str,
value: &'a Value,
2019-11-04 16:47:03 +01:00
}
impl<'a> PrettyDebug for DebugEntry<'a> {
fn pretty(&self) -> DebugDocBuilder {
(b::key(self.key.to_string()) + b::equals() + self.value.pretty().into_value()).group()
2019-11-04 16:47:03 +01:00
}
}
pub trait DictionaryExt {
fn get_data(&self, desc: &str) -> MaybeOwned<'_, Value>;
2019-06-24 02:55:31 +02:00
fn keys(&self) -> indexmap::map::Keys<String, Value>;
fn get_data_by_key(&self, name: Spanned<&str>) -> Option<Value>;
fn get_mut_data_by_key(&mut self, name: &str) -> Option<&mut Value>;
fn insert_data_at_key(&mut self, name: &str, value: Value);
2019-05-17 17:55:50 +02:00
}
impl DictionaryExt for Dictionary {
fn get_data(&self, desc: &str) -> MaybeOwned<'_, Value> {
match self.entries.get(desc) {
Some(v) => MaybeOwned::Borrowed(v),
None => MaybeOwned::Owned(
UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value(),
),
2019-05-10 18:59:12 +02:00
}
}
2019-05-17 17:55:50 +02:00
fn keys(&self) -> indexmap::map::Keys<String, Value> {
2019-11-04 16:47:03 +01:00
self.entries.keys()
}
fn get_data_by_key(&self, name: Spanned<&str>) -> Option<Value> {
2019-11-04 16:47:03 +01:00
let result = self
.entries
.iter()
2019-11-04 16:47:03 +01:00
.find(|(desc_name, _)| *desc_name == name.item)?
.1;
Some(
result
.value
2019-11-04 16:47:03 +01:00
.clone()
.into_value(Tag::new(result.anchor(), name.span)),
2019-11-04 16:47:03 +01:00
)
2019-05-17 17:55:50 +02:00
}
2019-06-22 05:43:37 +02:00
fn get_mut_data_by_key(&mut self, name: &str) -> Option<&mut Value> {
self.entries
.iter_mut()
.find(|(desc_name, _)| *desc_name == name)
.map_or_else(|| None, |x| Some(x.1))
}
fn insert_data_at_key(&mut self, name: &str, value: Value) {
2019-11-04 16:47:03 +01:00
self.entries.insert(name.to_string(), value);
2019-06-22 05:43:37 +02:00
}
2019-05-10 18:59:12 +02:00
}
2019-07-08 18:44:53 +02:00
2019-09-02 07:37:13 +02:00
#[derive(Debug)]
2019-08-01 03:58:42 +02:00
pub struct TaggedListBuilder {
tag: Tag,
pub list: Vec<Value>,
2019-07-09 06:31:26 +02:00
}
2019-08-01 03:58:42 +02:00
impl TaggedListBuilder {
pub fn new(tag: impl Into<Tag>) -> TaggedListBuilder {
2019-08-01 03:58:42 +02:00
TaggedListBuilder {
tag: tag.into(),
2019-07-09 06:31:26 +02:00
list: vec![],
}
}
pub fn push_value(&mut self, value: impl Into<Value>) {
self.list.push(value.into());
2019-07-09 06:31:26 +02:00
}
pub fn push_untagged(&mut self, value: impl Into<UntaggedValue>) {
self.list.push(value.into().into_value(self.tag.clone()));
}
pub fn into_value(self) -> Value {
UntaggedValue::Table(self.list).into_value(self.tag)
2019-07-09 06:31:26 +02:00
}
pub fn into_untagged_value(self) -> UntaggedValue {
UntaggedValue::Table(self.list).into_value(self.tag).value
2019-07-09 06:31:26 +02:00
}
}
impl From<TaggedListBuilder> for Value {
fn from(input: TaggedListBuilder) -> Value {
input.into_value()
2019-07-09 06:31:26 +02:00
}
}