nushell/src/object/dict.rs

117 lines
2.9 KiB
Rust
Raw Normal View History

2019-05-16 00:58:44 +02:00
use crate::prelude::*;
2019-05-24 21:35:22 +02:00
use crate::object::DataDescriptor;
2019-05-10 18:59:12 +02:00
use crate::object::{Primitive, Value};
use derive_new::new;
use indexmap::IndexMap;
2019-06-07 09:50:26 +02:00
use serde::ser::{Serialize, SerializeMap, Serializer};
2019-06-01 20:26:04 +02:00
use serde_derive::Deserialize;
2019-05-17 17:55:50 +02:00
use std::cmp::{Ordering, PartialOrd};
2019-05-10 18:59:12 +02:00
2019-06-01 20:26:04 +02:00
#[derive(Debug, Default, Eq, PartialEq, Deserialize, Clone, new)]
2019-05-10 18:59:12 +02:00
pub struct Dictionary {
2019-06-07 09:50:26 +02:00
pub entries: IndexMap<DataDescriptor, Value>,
2019-05-10 18:59:12 +02:00
}
2019-05-17 17:55:50 +02:00
impl PartialOrd for Dictionary {
// TODO: FIXME
fn partial_cmp(&self, _other: &Dictionary) -> Option<Ordering> {
Some(Ordering::Less)
}
}
2019-06-01 20:26:04 +02:00
impl Serialize for Dictionary {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(self.entries.len()))?;
for (k, v) in self.entries.iter() {
match v {
2019-06-07 09:50:26 +02:00
Value::Object(_) => {}
_ => map.serialize_entry(k, v)?,
2019-06-01 20:26:04 +02:00
}
}
for (k, v) in self.entries.iter() {
match v {
Value::Object(_) => map.serialize_entry(k, v)?,
_ => {}
}
}
map.end()
}
}
impl From<IndexMap<String, Value>> for Dictionary {
fn from(input: IndexMap<String, Value>) -> Dictionary {
let mut out = IndexMap::default();
for (key, value) in input {
out.insert(DataDescriptor::for_string_name(key), value);
}
Dictionary::new(out)
}
}
2019-05-17 17:55:50 +02:00
impl Ord for Dictionary {
// TODO: FIXME
fn cmp(&self, _other: &Dictionary) -> Ordering {
Ordering::Less
}
}
impl PartialOrd<Value> for Dictionary {
fn partial_cmp(&self, _other: &Value) -> Option<Ordering> {
Some(Ordering::Less)
}
}
impl PartialEq<Value> for Dictionary {
// TODO: FIXME
fn eq(&self, other: &Value) -> bool {
match other {
Value::Object(d) => self == d,
_ => false,
}
}
}
2019-05-10 18:59:12 +02:00
impl Dictionary {
crate fn add(&mut self, name: impl Into<DataDescriptor>, value: Value) {
2019-05-10 18:59:12 +02:00
self.entries.insert(name.into(), value);
}
crate fn copy_dict(&self) -> Dictionary {
let mut out = Dictionary::default();
2019-05-10 18:59:12 +02:00
for (key, value) in self.entries.iter() {
out.add(key.copy(), value.copy());
2019-05-10 18:59:12 +02:00
}
out
2019-05-10 18:59:12 +02:00
}
2019-05-16 00:23:36 +02:00
crate fn data_descriptors(&self) -> Vec<DataDescriptor> {
self.entries.iter().map(|(name, _)| name.copy()).collect()
2019-05-10 18:59:12 +02:00
}
2019-05-16 00:23:36 +02:00
crate fn get_data(&'a self, desc: &DataDescriptor) -> MaybeOwned<'a, Value> {
match self.entries.get(desc) {
Some(v) => MaybeOwned::Borrowed(v),
2019-05-10 18:59:12 +02:00
None => MaybeOwned::Owned(Value::Primitive(Primitive::Nothing)),
}
}
2019-05-17 17:55:50 +02:00
2019-05-28 08:45:18 +02:00
crate fn get_data_by_key(&self, name: &str) -> Option<&Value> {
match self
.entries
.iter()
.find(|(desc_name, _)| desc_name.name.is_string(name))
{
2019-05-28 08:45:18 +02:00
Some((_, v)) => Some(v),
None => None,
2019-05-17 17:55:50 +02:00
}
}
2019-05-10 18:59:12 +02:00
}