// Copyright (c) Facebook, Inc. and its affiliates. // // This source code is licensed under both the MIT license found in the // LICENSE-MIT file in the root directory of this source tree and the Apache // License, Version 2.0 found in the LICENSE-APACHE file in the root directory // of this source tree. /// Macro used for deriving `serde`'s `Serialize` and `Deserialize` traits. macro_rules! impl_serialize_and_deserialize_for { ($item:ident$( where $($path:ty: $bound1:path $(| $bound2:path)*),+$(,)?)?$(; $error:expr)?) => { #[cfg(feature = "serde")] impl serde_::Serialize for $item $(where $($path: $bound1 $(+ $bound2)*),+ )? { fn serialize(&self, serializer: S) -> Result where S: serde_::Serializer, { serializer.serialize_bytes(&self.serialize()$(.map_err($error)?)?) } } #[cfg(feature = "serde")] impl<'de, CS: CipherSuite> serde_::Deserialize<'de> for $item { fn deserialize(deserializer: D) -> Result where D: serde_::Deserializer<'de>, { use serde_::de::Error; struct ByteVisitor(core::marker::PhantomData); impl<'de, CS: CipherSuite> serde_::de::Visitor<'de> for ByteVisitor { type Value = $item; fn expecting( &self, formatter: &mut core::fmt::Formatter, ) -> core::fmt::Result { formatter.write_str(core::concat!( "the byte representation of a ", core::stringify!($t) )) } fn visit_bytes(self, value: &[u8]) -> Result where E: Error, { $item::::deserialize(value).map_err(|_| { Error::invalid_value( serde_::de::Unexpected::Bytes(value), &core::concat!( "invalid byte sequence for ", core::stringify!($t) ), ) }) } } deserializer .deserialize_bytes(ByteVisitor::(core::marker::PhantomData)) .map_err(Error::custom) } } }; }