use serde_json::{Map, Value};
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Document(Map<String, Value>);
impl Document {
pub fn new() -> Self {
Self(Map::new())
}
pub fn field(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.0.insert(key.into(), value.into());
self
}
pub fn set(&mut self, key: impl Into<String>, value: impl Into<Value>) {
self.0.insert(key.into(), value.into());
}
pub fn get(&self, key: &str) -> Option<&Value> {
self.0.get(key)
}
pub fn iter(&self) -> impl Iterator<Item = (&String, &Value)> {
self.0.iter()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn as_map(&self) -> &Map<String, Value> {
&self.0
}
}
impl From<Map<String, Value>> for Document {
fn from(map: Map<String, Value>) -> Self {
Self(map)
}
}
impl From<Value> for Document {
fn from(value: Value) -> Self {
match value {
Value::Object(map) => Self(map),
_ => Self::new(),
}
}
}
impl serde::Serialize for Document {
fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
self.0.serialize(s)
}
}