use alloc::borrow::Cow;
use deser_core::State;
use deser_core::de::{Deserialize, Slot, default_atom};
use deser_core::ext::{ExtValue, Extension};
use deser_core::ser::{Emit, Serialize};
use deser_core::{Atom, Error};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Uid(u64);
impl Uid {
pub const fn new(value: u64) -> Uid {
Uid(value)
}
pub const fn get(self) -> u64 {
self.0
}
}
impl From<u64> for Uid {
fn from(value: u64) -> Uid {
Uid(value)
}
}
impl From<Uid> for u64 {
fn from(value: Uid) -> u64 {
value.0
}
}
impl Extension for Uid {
fn name(&self) -> &str {
"plist uid"
}
fn fallback(&self) -> Atom<'_> {
Atom::U64(self.0)
}
}
impl Serialize for Uid {
fn serialize<'a>(value: &'a Self, _state: &mut State) -> Result<Emit<'a>, Error> {
Ok(Emit::Atom(Atom::Ext(ExtValue::borrowed(value))))
}
}
impl<'de> Deserialize<'de> for Uid {
fn deserialize_atom(slot: &mut Slot<Self>, atom: Atom, state: &mut State) -> Result<(), Error> {
let value = match atom {
Atom::Ext(ref ext) => match ext.downcast_ref::<Uid>() {
Some(&value) => value,
None => return default_atom(slot, atom, state),
},
Atom::U64(value) => Uid(value),
other => return default_atom(slot, other, state),
};
slot.set(value);
Ok(())
}
fn expecting() -> Cow<'static, str> {
Cow::Borrowed("plist uid")
}
}