use alloc::borrow::Cow;
use alloc::string::String;
use core::fmt;
use deser_core::State;
use deser_core::de::{Deserialize, OwnedSink, Sink, SinkHandle};
use deser_core::ser::{Describe, Emit, Serialize};
use deser_core::{Atom, ContainerShape, Error};
#[derive(Debug, Default, Clone)]
pub(crate) struct ClassName(pub(crate) Option<String>);
#[derive(Debug, Default, Clone)]
pub(crate) struct PropertyVisibility(pub(crate) Option<Visibility>);
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum Visibility {
#[default]
Public,
Protected,
Private(String),
}
pub fn take_class(state: &mut State) -> Option<String> {
state.take_event::<ClassName>().and_then(|class| class.0)
}
pub fn set_class<S: Into<String>>(state: &mut State, class: S) {
state.event_mut::<ClassName>().0 = Some(class.into());
}
pub fn take_visibility(state: &mut State) -> Option<Visibility> {
state
.take_event::<PropertyVisibility>()
.and_then(|visibility| visibility.0)
}
pub fn set_visibility(state: &mut State, visibility: Visibility) {
state.event_mut::<PropertyVisibility>().0 = Some(visibility);
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Object<T> {
pub class: Option<String>,
pub value: T,
}
impl<T> Object<T> {
pub fn new<S: Into<String>>(class: S, value: T) -> Object<T> {
Object {
class: Some(class.into()),
value,
}
}
pub fn into_inner(self) -> T {
self.value
}
}
impl<T: fmt::Debug> fmt::Debug for Object<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.class {
Some(ref class) => {
write!(f, "{} ", class)?;
fmt::Debug::fmt(&self.value, f)
}
None => fmt::Debug::fmt(&self.value, f),
}
}
}
impl<T: Serialize> Serialize for Object<T> {
fn serialize<'a>(this: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
let emit = T::serialize(&this.value, state)?;
if let Some(ref class) = this.class {
set_class(state, class.as_str());
}
Ok(emit)
}
fn finish(this: &Self, state: &mut State) -> Result<(), Error> {
T::finish(&this.value, state)
}
fn is_optional(this: &Self) -> bool {
T::is_optional(&this.value)
}
fn container_shape(this: &Self) -> ContainerShape {
T::container_shape(&this.value)
}
fn describe(this: &Self, d: &mut dyn Describe) {
T::describe(&this.value, d)
}
}
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Object<T> {
fn deserialize_into<'out>(
out: &'out mut Option<Self>,
state: &mut State,
) -> SinkHandle<'out, 'de> {
SinkHandle::arena(
ObjectSink {
out,
slot: None,
compound: None,
class: None,
},
state,
)
}
fn expecting() -> Cow<'static, str> {
T::expecting()
}
fn describe_type(d: &mut dyn Describe) {
T::describe_type(d)
}
}
struct ObjectSink<'a, 'de, T> {
out: &'a mut Option<Object<T>>,
slot: Option<T>,
compound: Option<OwnedSink<'de, T>>,
class: Option<String>,
}
impl<'a, 'de, T: Deserialize<'de>> ObjectSink<'a, 'de, T> {
fn compound(&mut self, state: &mut State) -> &mut dyn Sink<'de> {
self.compound
.get_or_insert_with(|| OwnedSink::deserialize(state))
.get_mut()
}
}
impl<'a, 'de, T: Deserialize<'de>> Sink<'de> for ObjectSink<'a, 'de, T> {
fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
self.class = take_class(state);
let mut sink = T::deserialize_into(&mut self.slot, state);
sink.atom(atom, state)?;
sink.finish(state)
}
fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
self.class = take_class(state);
let mut sink = T::deserialize_into(&mut self.slot, state);
sink.borrowed_atom(atom, state)?;
sink.finish(state)
}
fn map(&mut self, state: &mut State) -> Result<(), Error> {
self.class = take_class(state);
self.compound(state).map(state)
}
fn seq(&mut self, state: &mut State) -> Result<(), Error> {
self.class = take_class(state);
self.compound(state).seq(state)
}
fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
self.compound(state).next_key(state)
}
fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
self.compound(state).next_value(state)
}
fn value_for_key(
&mut self,
key: &str,
state: &mut State,
) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
self.compound(state).value_for_key(key, state)
}
fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
match self.compound {
Some(ref mut compound) => compound.get_mut().recover(err, state),
None => Err(err),
}
}
fn finish(&mut self, state: &mut State) -> Result<(), Error> {
let value = match self.compound {
Some(ref mut compound) => {
compound.get_mut().finish(state)?;
compound.take()
}
None => self.slot.take(),
};
let class = self.class.take();
*self.out = value.map(|value| Object { class, value });
Ok(())
}
fn expecting(&self) -> Cow<'_, str> {
if let Some(ref compound) = self.compound {
return compound.get().expecting();
}
T::expecting()
}
}