use std::borrow::{Borrow, Cow}; use std::collections::BTreeMap; use std::{fmt, ops}; use serde::{Deserialize, Serialize}; use serde_json::value::RawValue; use serde_json::{Map, Value}; #[derive(Deserialize, Serialize)] pub struct TopLevelMap<'p>(#[serde(borrow)] pub BTreeMap, &'p RawValue>); impl TryFrom<&'_ TopLevelMap<'_>> for Map { type Error = serde_json::Error; fn try_from(tlmap: &TopLevelMap<'_>) -> Result { let mut object = Map::new(); for (k, v) in &tlmap.0 { let value = serde_json::from_str(v.get())?; object.insert(k.to_string(), value); } Ok(object) } } impl<'p> ops::Deref for TopLevelMap<'p> { type Target = BTreeMap, &'p RawValue>; fn deref(&self) -> &Self::Target { &self.0 } } impl ops::DerefMut for TopLevelMap<'_> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } #[derive(Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)] pub struct CowStr<'p>(#[serde(borrow)] pub Cow<'p, str>); impl fmt::Display for CowStr<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) } } impl AsRef for CowStr<'_> { fn as_ref(&self) -> &str { self.0.as_ref() } } impl<'doc> Borrow for CowStr<'doc> { fn borrow(&self) -> &str { self.0.borrow() } }