use core::str;
use std::marker::PhantomData;
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use static_assertions::assert_impl_all;
use crate::{Signature, Type, Value};
pub struct DeserializeValue<'de, T: Type + Deserialize<'de>>(
pub T,
std::marker::PhantomData<&'de T>,
);
assert_impl_all!(DeserializeValue<'_, i32>: Send, Sync, Unpin);
impl<'de, T: Type + Deserialize<'de>> Deserialize<'de> for DeserializeValue<'de, T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
const FIELDS: &[&str] = &["zvariant::Value::Signature", "zvariant::Value::Value"];
Ok(DeserializeValue(
deserializer.deserialize_struct(
"zvariant::Value",
FIELDS,
DeserializeValueVisitor(PhantomData),
)?,
PhantomData,
))
}
}
struct DeserializeValueVisitor<T>(PhantomData<T>);
impl<'de, T: Type + Deserialize<'de>> Visitor<'de> for DeserializeValueVisitor<T> {
type Value = T;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("zvariant::Value")
}
fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
where
V: SeqAccess<'de>,
{
let sig: Signature<'_> = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
if sig != T::signature() {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(&sig),
&"the value signature",
));
}
seq.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(1, &self))
}
}
impl<'de, T: Type + Deserialize<'de>> Type for DeserializeValue<'de, T> {
fn signature() -> Signature<'static> {
Value::signature()
}
}