zvariant/
deserialize_value.rsuse core::str;
use std::marker::PhantomData;
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use static_assertions::assert_impl_all;
use crate::{Signature, Type};
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] = &["signature", "value"];
Ok(DeserializeValue(
deserializer.deserialize_struct(
"Variant",
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("Variant")
}
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 T::SIGNATURE != &sig {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(&sig.to_string()),
&"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> {
const SIGNATURE: &'static Signature = &Signature::Variant;
}