use crate::ast::Aggregate;
use crate::decode::{self, JsonCodable, Reader, Result};
use crate::json::{Object, Value};
use crate::strings;
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Format {
pub singular: bool,
pub relationships: Vec<(String, Format)>,
pub aggregate: Option<Aggregate>,
}
impl Format {
pub fn set_relationship(&mut self, name: &str, format: Format) {
match self
.relationships
.binary_search_by(|(k, _)| strings::utf16(k, name))
{
Ok(i) => self.relationships[i].1 = format,
Err(i) => self.relationships.insert(i, (name.to_owned(), format)),
}
}
}
impl JsonCodable for Format {
fn from_json(v: &Value) -> Result<Self> {
let r = Reader::new(v, "object")?;
let mut relationships = r
.optional("relationships", |v| decode::record(v, Format::from_json))?
.unwrap_or_default();
relationships.sort_by(|a, b| strings::utf16(&a.0, &b.0));
Ok(Format {
singular: r.optional_bool("singular")?.unwrap_or(false),
relationships,
aggregate: r.optional("aggregate", Aggregate::from_json)?,
})
}
fn to_json(&self) -> Value {
let rels: Object = self
.relationships
.iter()
.map(|(k, f)| (k.clone(), f.to_json()))
.collect();
let mut o = Object::new();
o.insert("singular", Value::Bool(self.singular));
o.insert("relationships", Value::Object(rels));
if let Some(a) = &self.aggregate {
o.insert("aggregate", a.to_json());
}
Value::Object(o)
}
}