use std::{
fmt::Display,
ops::{Deref, DerefMut},
};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use crate::Type;
pub trait NoneValue {
type NoneType;
fn null_value() -> Self::NoneType;
}
impl<T> NoneValue for T
where
T: Default,
{
type NoneType = Self;
fn null_value() -> Self {
Default::default()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Optional<T>(Option<T>);
impl<T> Type for Optional<T>
where
T: Type,
{
const SIGNATURE: &'static crate::Signature = T::SIGNATURE;
}
impl<T> Serialize for Optional<T>
where
T: Type + NoneValue + Serialize,
<T as NoneValue>::NoneType: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if T::SIGNATURE == bool::SIGNATURE {
panic!("`Optional<bool>` type is not supported");
}
match &self.0 {
Some(value) => value.serialize(serializer),
None => T::null_value().serialize(serializer),
}
}
}
impl<'de, T, E> Deserialize<'de> for Optional<T>
where
T: Type + NoneValue + Deserialize<'de>,
<T as NoneValue>::NoneType: Deserialize<'de> + TryInto<T, Error = E> + PartialEq,
E: Display,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
if T::SIGNATURE == bool::SIGNATURE {
panic!("`Optional<bool>` type is not supported");
}
let value = <<T as NoneValue>::NoneType>::deserialize(deserializer)?;
if value == T::null_value() {
Ok(Optional(None))
} else {
Ok(Optional(Some(value.try_into().map_err(de::Error::custom)?)))
}
}
}
impl<T> From<Option<T>> for Optional<T> {
fn from(value: Option<T>) -> Self {
Optional(value)
}
}
impl<T> From<Optional<T>> for Option<T> {
fn from(value: Optional<T>) -> Self {
value.0
}
}
impl<T> Deref for Optional<T> {
type Target = Option<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Optional<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> Default for Optional<T> {
fn default() -> Self {
Self(None)
}
}
#[cfg(test)]
mod tests {
use std::panic::catch_unwind;
#[test]
fn bool_in_optional() {
use crate::{LE, Optional, to_bytes};
let ctxt = crate::serialized::Context::new_dbus(LE, 0);
let res = catch_unwind(|| to_bytes(ctxt, &Optional::<bool>::default()));
assert!(res.is_err());
let data = crate::serialized::Data::new([0, 0, 0, 0].as_slice(), ctxt);
let res = catch_unwind(|| data.deserialize::<Optional<bool>>());
assert!(res.is_err());
}
}