use std::fmt::{self, Formatter};
use serde_core::de::{
self, Deserialize, DeserializeSeed, Deserializer, Error as _, IgnoredAny, SeqAccess,
Unexpected, Visitor,
};
use serde_core::ser::{Serialize, Serializer};
pub(crate) struct Identifier(pub(crate) String);
pub(crate) struct IdentifierSeed(pub(crate) &'static [&'static str]);
impl<'de> DeserializeSeed<'de> for IdentifierSeed {
type Value = Identifier;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_identifier(IdentifierVisitor(self.0))
}
}
struct IdentifierVisitor(&'static [&'static str]);
impl Visitor<'_> for IdentifierVisitor {
type Value = Identifier;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("a field or variant name")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Identifier(value.to_owned()))
}
fn visit_u64<E>(self, index: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
match usize::try_from(index)
.ok()
.and_then(|index| self.0.get(index))
{
Some(name) => Ok(Identifier((*name).to_owned())),
None => Err(E::invalid_value(
Unexpected::Unsigned(index),
&"a declared field or variant position",
)),
}
}
}
pub(crate) fn accept<T, E>(slot: &mut Option<T>, value: T, field: &'static str) -> Result<(), E>
where
E: de::Error,
{
match slot {
Some(_) => Err(E::duplicate_field(field)),
None => {
*slot = Some(value);
Ok(())
}
}
}
pub(crate) fn require<T, E>(slot: Option<T>, field: &'static str) -> Result<T, E>
where
E: de::Error,
{
match slot {
Some(value) => Ok(value),
None => Err(E::missing_field(field)),
}
}
pub(crate) fn element<'de, T, A>(sequence: &mut A, index: usize) -> Result<T, A::Error>
where
T: Deserialize<'de>,
A: SeqAccess<'de>,
{
match sequence.next_element()? {
Some(value) => Ok(value),
None => Err(A::Error::invalid_length(index, &"every struct field")),
}
}
pub(crate) fn end<'de, A>(sequence: &mut A, fields: usize) -> Result<(), A::Error>
where
A: SeqAccess<'de>,
{
match sequence.next_element::<IgnoredAny>()? {
Some(_) => Err(A::Error::invalid_length(
fields.saturating_add(1),
&"exactly the declared struct fields",
)),
None => Ok(()),
}
}
pub(crate) struct Bytes<'a>(pub(crate) &'a [u8]);
impl Serialize for Bytes<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(self.0)
}
}
pub(crate) struct ByteBuf(pub(crate) Box<[u8]>);
impl<'de> Deserialize<'de> for ByteBuf {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_byte_buf(ByteBufVisitor)
}
}
struct ByteBufVisitor;
impl<'de> Visitor<'de> for ByteBufVisitor {
type Value = ByteBuf;
fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("a byte string")
}
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(ByteBuf(Box::from(value)))
}
fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut bytes = Vec::new();
while let Some(byte) = sequence.next_element::<u8>()? {
bytes.push(byte);
}
Ok(ByteBuf(bytes.into_boxed_slice()))
}
}
impl ByteBuf {
pub(crate) fn into_optional(value: Option<Self>) -> Option<Box<[u8]>> {
value.map(|buffer| buffer.0)
}
}
#[cfg(test)]
#[path = "wire_tests.rs"]
pub(crate) mod wire_tests;