2019-05-15 22:58:44 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-05-24 19:35:22 +00:00
|
|
|
use crate::object::DataDescriptor;
|
2019-05-10 16:59:12 +00:00
|
|
|
use crate::object::{Primitive, Value};
|
2019-06-01 05:50:16 +00:00
|
|
|
use derive_new::new;
|
2019-05-15 18:14:51 +00:00
|
|
|
use indexmap::IndexMap;
|
2019-06-07 07:50:26 +00:00
|
|
|
use serde::ser::{Serialize, SerializeMap, Serializer};
|
2019-06-01 18:26:04 +00:00
|
|
|
use serde_derive::Deserialize;
|
2019-05-17 15:55:50 +00:00
|
|
|
use std::cmp::{Ordering, PartialOrd};
|
2019-06-22 03:43:37 +00:00
|
|
|
use std::fmt;
|
2019-05-10 16:59:12 +00:00
|
|
|
|
2019-06-01 18:26:04 +00:00
|
|
|
#[derive(Debug, Default, Eq, PartialEq, Deserialize, Clone, new)]
|
2019-05-10 16:59:12 +00:00
|
|
|
pub struct Dictionary {
|
2019-06-07 07:50:26 +00:00
|
|
|
pub entries: IndexMap<DataDescriptor, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 18:26:04 +00: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 07:50:26 +00:00
|
|
|
Value::Object(_) => {}
|
|
|
|
_ => map.serialize_entry(k, v)?,
|
2019-06-01 18:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (k, v) in self.entries.iter() {
|
|
|
|
match v {
|
|
|
|
Value::Object(_) => map.serialize_entry(k, v)?,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
map.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 05:50:16 +00:00
|
|
|
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 15:55:50 +00: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 16:59:12 +00:00
|
|
|
impl Dictionary {
|
2019-05-24 18:48:33 +00:00
|
|
|
crate fn add(&mut self, name: impl Into<DataDescriptor>, value: Value) {
|
2019-05-10 16:59:12 +00:00
|
|
|
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() {
|
2019-05-24 18:48:33 +00:00
|
|
|
out.add(key.copy(), 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-24 18:48:33 +00:00
|
|
|
self.entries.iter().map(|(name, _)| name.copy()).collect()
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 22:23:36 +00:00
|
|
|
crate fn get_data(&'a self, desc: &DataDescriptor) -> MaybeOwned<'a, Value> {
|
2019-05-24 18:48:33 +00:00
|
|
|
match self.entries.get(desc) {
|
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
|
|
|
|
2019-05-28 06:45:18 +00:00
|
|
|
crate fn get_data_by_key(&self, name: &str) -> Option<&Value> {
|
2019-05-24 18:48:33 +00:00
|
|
|
match self
|
|
|
|
.entries
|
|
|
|
.iter()
|
|
|
|
.find(|(desc_name, _)| desc_name.name.is_string(name))
|
|
|
|
{
|
2019-05-28 06:45:18 +00:00
|
|
|
Some((_, v)) => Some(v),
|
|
|
|
None => None,
|
2019-05-17 15:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 03:43:37 +00:00
|
|
|
|
|
|
|
crate fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let mut debug = f.debug_struct("Dictionary");
|
|
|
|
|
|
|
|
for (desc, value) in self.entries.iter() {
|
|
|
|
debug.field(desc.name.debug(), &value.debug());
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.finish()
|
|
|
|
}
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|