2019-05-15 22:58:44 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
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-07-03 17:37:09 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
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-07-03 17:37:09 +00:00
|
|
|
#[derive(Debug, Default, Eq, PartialEq, Serialize, Deserialize, Clone, new)]
|
2019-05-10 16:59:12 +00:00
|
|
|
pub struct Dictionary {
|
2019-07-08 16:44:53 +00:00
|
|
|
pub entries: IndexMap<String, Spanned<Value>>,
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 15:55:50 +00:00
|
|
|
impl PartialOrd for Dictionary {
|
2019-06-24 00:55:31 +00:00
|
|
|
fn partial_cmp(&self, other: &Dictionary) -> Option<Ordering> {
|
2019-07-03 17:37:09 +00:00
|
|
|
let this: Vec<&String> = self.entries.keys().collect();
|
|
|
|
let that: Vec<&String> = other.entries.keys().collect();
|
2019-06-24 00:55:31 +00:00
|
|
|
|
|
|
|
if this != that {
|
|
|
|
return this.partial_cmp(&that);
|
|
|
|
}
|
|
|
|
|
|
|
|
let this: Vec<&Value> = self.entries.values().collect();
|
|
|
|
let that: Vec<&Value> = self.entries.values().collect();
|
|
|
|
|
|
|
|
this.partial_cmp(&that)
|
2019-05-17 15:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:44:53 +00:00
|
|
|
impl From<IndexMap<String, Spanned<Value>>> for Dictionary {
|
|
|
|
fn from(input: IndexMap<String, Spanned<Value>>) -> Dictionary {
|
2019-06-01 05:50:16 +00:00
|
|
|
let mut out = IndexMap::default();
|
|
|
|
|
|
|
|
for (key, value) in input {
|
2019-07-03 17:37:09 +00:00
|
|
|
out.insert(key, value);
|
2019-06-01 05:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::new(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:55:50 +00:00
|
|
|
impl Ord for Dictionary {
|
2019-06-24 00:55:31 +00:00
|
|
|
fn cmp(&self, other: &Dictionary) -> Ordering {
|
2019-07-03 17:37:09 +00:00
|
|
|
let this: Vec<&String> = self.entries.keys().collect();
|
|
|
|
let that: Vec<&String> = other.entries.keys().collect();
|
2019-06-24 00:55:31 +00:00
|
|
|
|
|
|
|
if this != that {
|
|
|
|
return this.cmp(&that);
|
|
|
|
}
|
|
|
|
|
|
|
|
let this: Vec<&Value> = self.entries.values().collect();
|
|
|
|
let that: Vec<&Value> = self.entries.values().collect();
|
|
|
|
|
|
|
|
this.cmp(&that)
|
2019-05-17 15:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd<Value> for Dictionary {
|
|
|
|
fn partial_cmp(&self, _other: &Value) -> Option<Ordering> {
|
|
|
|
Some(Ordering::Less)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<Value> for Dictionary {
|
|
|
|
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-07-03 17:37:09 +00:00
|
|
|
crate fn add(&mut self, name: impl Into<String>, 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-07-03 17:37:09 +00:00
|
|
|
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-07-04 05:11:56 +00:00
|
|
|
pub fn get_data(&'a self, desc: &String) -> 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-07-08 16:44:53 +00:00
|
|
|
crate fn get_data_by_key(&self, name: &str) -> Option<&Spanned<Value>> {
|
2019-05-24 18:48:33 +00:00
|
|
|
match self
|
|
|
|
.entries
|
|
|
|
.iter()
|
2019-07-03 17:37:09 +00:00
|
|
|
.find(|(desc_name, _)| *desc_name == name)
|
2019-05-24 18:48:33 +00:00
|
|
|
{
|
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() {
|
2019-07-03 17:37:09 +00:00
|
|
|
debug.field(desc, &value.debug());
|
2019-06-22 03:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug.finish()
|
|
|
|
}
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
2019-07-08 16:44:53 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SpannedDictBuilder {
|
|
|
|
span: Span,
|
|
|
|
dict: IndexMap<DataDescriptor, Spanned<Value>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpannedDictBuilder {
|
|
|
|
pub fn new(span: impl Into<Span>) -> SpannedDictBuilder {
|
|
|
|
SpannedDictBuilder {
|
|
|
|
span: span.into(),
|
|
|
|
dict: IndexMap::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&mut self, key: impl Into<DataDescriptor>, value: impl Into<Value>) {
|
|
|
|
self.dict
|
|
|
|
.insert(key.into(), value.into().spanned(self.span));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_spanned(
|
|
|
|
&mut self,
|
|
|
|
key: impl Into<DataDescriptor>,
|
|
|
|
value: impl Into<Spanned<Value>>,
|
|
|
|
) {
|
|
|
|
self.dict.insert(key.into(), value.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_spanned_value(self) -> Spanned<Value> {
|
|
|
|
Value::Object(Dictionary { entries: self.dict }).spanned(self.span)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SpannedDictBuilder> for Spanned<Value> {
|
|
|
|
fn from(input: SpannedDictBuilder) -> Spanned<Value> {
|
|
|
|
input.into_spanned_value()
|
|
|
|
}
|
|
|
|
}
|