2019-05-15 22:58:44 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-05-10 16:59:12 +00:00
|
|
|
use crate::object::desc::DataDescriptor;
|
|
|
|
use crate::object::{Primitive, Value};
|
2019-05-15 18:14:51 +00:00
|
|
|
use indexmap::IndexMap;
|
2019-05-17 15:55:50 +00:00
|
|
|
use std::cmp::{Ordering, PartialOrd};
|
2019-05-10 16:59:12 +00:00
|
|
|
|
2019-05-17 15:55:50 +00:00
|
|
|
#[derive(Debug, Default, Eq, PartialEq)]
|
2019-05-10 16:59:12 +00:00
|
|
|
pub struct Dictionary {
|
2019-05-15 18:14:51 +00:00
|
|
|
entries: IndexMap<String, Value>,
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 15:55:50 +00:00
|
|
|
impl PartialOrd for Dictionary {
|
|
|
|
// TODO: FIXME
|
|
|
|
fn partial_cmp(&self, _other: &Dictionary) -> Option<Ordering> {
|
|
|
|
Some(Ordering::Less)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:59:12 +00:00
|
|
|
impl Dictionary {
|
|
|
|
crate fn add(&mut self, name: impl Into<String>, value: Value) {
|
|
|
|
self.entries.insert(name.into(), value);
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:14:51 +00:00
|
|
|
crate fn copy_dict(&self) -> Dictionary {
|
|
|
|
let mut out = Dictionary::default();
|
2019-05-10 16:59:12 +00:00
|
|
|
|
2019-05-15 18:14:51 +00:00
|
|
|
for (key, value) in self.entries.iter() {
|
|
|
|
out.add(key.clone(), value.copy());
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 18:14:51 +00:00
|
|
|
out
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 22:23:36 +00:00
|
|
|
crate fn data_descriptors(&self) -> Vec<DataDescriptor> {
|
2019-05-10 16:59:12 +00:00
|
|
|
self.entries
|
|
|
|
.iter()
|
2019-05-15 22:58:44 +00:00
|
|
|
.map(|(name, _)| {
|
2019-05-15 16:12:38 +00:00
|
|
|
DataDescriptor::new(name.clone(), true, Box::new(crate::object::types::AnyShell))
|
2019-05-10 16:59:12 +00:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:23:36 +00:00
|
|
|
crate fn get_data(&'a self, desc: &DataDescriptor) -> MaybeOwned<'a, Value> {
|
2019-05-10 16:59:12 +00:00
|
|
|
match self.entries.get(&desc.name) {
|
2019-05-15 18:14:51 +00:00
|
|
|
Some(v) => MaybeOwned::Borrowed(v),
|
2019-05-10 16:59:12 +00:00
|
|
|
None => MaybeOwned::Owned(Value::Primitive(Primitive::Nothing)),
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 15:55:50 +00:00
|
|
|
|
|
|
|
crate fn get_data_by_key(&self, name: &str) -> MaybeOwned<'_, Value> {
|
|
|
|
match self.entries.get(name) {
|
|
|
|
Some(v) => MaybeOwned::Borrowed(v),
|
|
|
|
None => MaybeOwned::Owned(Value::Primitive(Primitive::Nothing)),
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|