mod inner;
use crate::{BTreeMap, Error, Json, Map, MapKind, ObjectIndexes, ObjectKey, Result};
#[cfg(feature="std")]
use crate::HashMap;
impl Json {
pub fn insert<K, V>(&mut self, key: K, value: V) -> Result<Option<Self>> where K: Into<ObjectKey>, V: Into<Self> {
match self {
Json::Object(map) => Ok(map.insert(key, value)),
_ => Err(e!("Json is not an Object")),
}
}
pub fn by<'a, K, const N: usize>(&self, keys: K) -> Result<&Self> where K: Into<ObjectIndexes<'a, N>> {
let (result, keys) = inner::maybe_by(self, keys)?;
result.ok_or_else(|| err!("There is no value at: {keys:?}"))
}
pub fn by_or_null<'a, K, const N: usize>(&self, keys: K) -> Result<&Self> where K: Into<ObjectIndexes<'a, N>> {
let (result, _) = inner::maybe_by(self, keys)?;
Ok(result.unwrap_or(&Self::Null))
}
pub fn maybe_by<'a, K, const N: usize>(&self, keys: K) -> Result<Option<&Self>> where K: Into<ObjectIndexes<'a, N>> {
inner::maybe_by(self, keys).map(|(result, _)| result)
}
pub fn mut_by<'a, K, const N: usize>(&mut self, keys: K) -> Result<&mut Self> where K: Into<ObjectIndexes<'a, N>> {
let (result, keys) = inner::maybe_mut_by(self, keys)?;
result.ok_or_else(|| err!("There is no value at: {keys:?}"))
}
pub fn maybe_mut_by<'a, K, const N: usize>(&mut self, keys: K) -> Result<Option<&mut Self>> where K: Into<ObjectIndexes<'a, N>> {
inner::maybe_mut_by(self, keys).map(|(result, _)| result)
}
pub fn take_by<'a, K, const N: usize>(&mut self, keys: K) -> Result<Self> where K: Into<ObjectIndexes<'a, N>> {
let (result, keys) = inner::maybe_take_by(self, keys)?;
result.ok_or_else(|| err!("There is no value at: {keys:?}"))
}
pub fn take_by_or_null<'a, K, const N: usize>(&mut self, keys: K) -> Result<Self> where K: Into<ObjectIndexes<'a, N>> {
let (result, _) = inner::maybe_take_by(self, keys)?;
Ok(result.unwrap_or(Self::Null))
}
pub fn maybe_take_by<'a, K, const N: usize>(&mut self, keys: K) -> Result<Option<Self>> where K: Into<ObjectIndexes<'a, N>> {
inner::maybe_take_by(self, keys).map(|(result, _)| result)
}
pub fn as_object(&self) -> Result<&Map> {
match self {
Json::Object(object) => Ok(object),
_ => Err(e!("Json is not an Object")),
}
}
pub fn as_mut_object(&mut self) -> Result<&mut Map> {
match self {
Json::Object(object) => Ok(object),
_ => Err(e!("Json is not an Object")),
}
}
}
impl From<Map> for Json {
fn from(map: Map) -> Self {
Self::Object(map)
}
}
impl From<BTreeMap> for Json {
fn from(map: BTreeMap) -> Self {
Self::Object(Map::BTreeMap(map))
}
}
#[cfg(feature="std")]
impl From<HashMap> for Json {
fn from(map: HashMap) -> Self {
Self::Object(Map::HashMap(map))
}
}
impl TryFrom<Json> for Map {
type Error = Error;
fn try_from(j: Json) -> Result<Self> {
match j {
Json::Object(map) => Ok(map),
_ => Err(e!("Not a Map")),
}
}
}
impl TryFrom<Json> for BTreeMap {
type Error = Error;
fn try_from(j: Json) -> Result<Self> {
match j {
Json::Object(map) => Ok(map.into()),
_ => Err(e!("Not a Map")),
}
}
}
#[cfg(feature="std")]
impl TryFrom<Json> for HashMap {
type Error = Error;
fn try_from(j: Json) -> Result<Self> {
match j {
Json::Object(map) => Ok(map.into()),
_ => Err(e!("Not a Map")),
}
}
}
impl<I, K, V> From<(MapKind, I)> for Json where I: IntoIterator<Item=(K, V)>, K: Into<ObjectKey>, V: Into<Self> {
fn from((map_kind, iter): (MapKind, I)) -> Self {
let iter = iter.into_iter().map(|(k, v)| (k.into(), v.into()));
Self::Object(match map_kind {
MapKind::BTreeMap => Map::BTreeMap(BTreeMap::from_iter(iter)),
#[cfg(feature="std")]
MapKind::HashMap => Map::HashMap(HashMap::from_iter(iter)),
})
}
}