use core::{
any::Any,
num::NonZero,
ops::{Deref, DerefMut},
};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::{Error, Keys, Traversal, TreeAny, TreeDeserialize, TreeKey, TreeSerialize, Walk};
#[derive(
Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize,
)]
#[serde(transparent)]
#[repr(transparent)]
pub struct Leaf<T: ?Sized>(pub T);
impl<T: ?Sized> Deref for Leaf<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: ?Sized> DerefMut for Leaf<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> Leaf<T> {
#[inline]
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> From<T> for Leaf<T> {
#[inline]
fn from(value: T) -> Self {
Self(value)
}
}
impl<T: ?Sized> TreeKey for Leaf<T> {
#[inline]
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
Ok(W::leaf())
}
#[inline]
fn traverse_by_key<K, F, E>(mut keys: K, _func: F) -> Result<usize, Error<E>>
where
K: Keys,
F: FnMut(usize, Option<&'static str>, NonZero<usize>) -> Result<(), E>,
{
keys.finalize()?;
Ok(0)
}
}
impl<T: Serialize + ?Sized> TreeSerialize for Leaf<T> {
#[inline]
fn serialize_by_key<K, S>(&self, mut keys: K, ser: S) -> Result<usize, Error<S::Error>>
where
K: Keys,
S: Serializer,
{
keys.finalize()?;
self.0.serialize(ser).map_err(|err| Error::Inner(0, err))?;
Ok(0)
}
}
impl<'de, T: Deserialize<'de>> TreeDeserialize<'de> for Leaf<T> {
#[inline]
fn deserialize_by_key<K, D>(&mut self, mut keys: K, de: D) -> Result<usize, Error<D::Error>>
where
K: Keys,
D: Deserializer<'de>,
{
keys.finalize()?;
self.0 = T::deserialize(de).map_err(|err| Error::Inner(0, err))?;
Ok(0)
}
}
impl<T: Any> TreeAny for Leaf<T> {
#[inline]
fn ref_any_by_key<K>(&self, mut keys: K) -> Result<&dyn Any, Traversal>
where
K: Keys,
{
keys.finalize()?;
Ok(&self.0)
}
#[inline]
fn mut_any_by_key<K>(&mut self, mut keys: K) -> Result<&mut dyn Any, Traversal>
where
K: Keys,
{
keys.finalize()?;
Ok(&mut self.0)
}
}
#[derive(
Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize,
)]
#[serde(transparent)]
#[repr(transparent)]
pub struct StrLeaf<T: ?Sized>(pub T);
impl<T: ?Sized> Deref for StrLeaf<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: ?Sized> DerefMut for StrLeaf<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> StrLeaf<T> {
#[inline]
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> From<T> for StrLeaf<T> {
#[inline]
fn from(value: T) -> Self {
Self(value)
}
}
impl<T: ?Sized> TreeKey for StrLeaf<T> {
#[inline]
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
Ok(W::leaf())
}
#[inline]
fn traverse_by_key<K, F, E>(mut keys: K, _func: F) -> Result<usize, Error<E>>
where
K: Keys,
F: FnMut(usize, Option<&'static str>, NonZero<usize>) -> Result<(), E>,
{
keys.finalize()?;
Ok(0)
}
}
impl<T: AsRef<str> + ?Sized> TreeSerialize for StrLeaf<T> {
#[inline]
fn serialize_by_key<K, S>(&self, mut keys: K, ser: S) -> Result<usize, Error<S::Error>>
where
K: Keys,
S: Serializer,
{
keys.finalize()?;
let name = self.0.as_ref();
name.serialize(ser).map_err(|err| Error::Inner(0, err))?;
Ok(0)
}
}
impl<'de, T: TryFrom<&'de str>> TreeDeserialize<'de> for StrLeaf<T> {
#[inline]
fn deserialize_by_key<K, D>(&mut self, mut keys: K, de: D) -> Result<usize, Error<D::Error>>
where
K: Keys,
D: Deserializer<'de>,
{
keys.finalize()?;
let name = Deserialize::deserialize(de).map_err(|err| Error::Inner(0, err))?;
self.0 = T::try_from(name).or(Err(Traversal::Invalid(0, "Could not convert")))?;
Ok(0)
}
}
impl<T> TreeAny for StrLeaf<T> {
#[inline]
fn ref_any_by_key<K>(&self, mut keys: K) -> Result<&dyn Any, Traversal>
where
K: Keys,
{
keys.finalize()?;
Err(Traversal::Access(1, "No Any access for StrLeaf"))
}
#[inline]
fn mut_any_by_key<K>(&mut self, mut keys: K) -> Result<&mut dyn Any, Traversal>
where
K: Keys,
{
keys.finalize()?;
Err(Traversal::Access(1, "No Any access for StrLeaf"))
}
}