#[macro_export]
macro_rules! archive_struct {
($name:ident $(< $($generic:ident $(: $bound:path)?),+ >)? { $($field:ident),* $(,)? }
$(skip { $($skipped:ident),* $(,)? })? $(check $check:path)?) => {
impl $(< $($generic: $crate::archive::Archive $(+ $bound)?),+ >)? $crate::archive::Archive
for $name $(< $($generic),+ >)?
{
fn encode(
&self,
encoder: &mut $crate::archive::Encoder<'_>,
) -> ::std::result::Result<(), $crate::archive::ArchiveError> {
let Self { $($field,)* $($($skipped: _,)*)? } = self;
$($crate::archive::Archive::encode($field, encoder)?;)*
::std::result::Result::Ok(())
}
fn decode(
decoder: &mut $crate::archive::Decoder<'_>,
) -> ::std::result::Result<Self, $crate::archive::ArchiveError> {
$(let $field = $crate::archive::Archive::decode(decoder)?;)*
let value = Self {
$($field,)*
$($($skipped: ::std::default::Default::default(),)*)?
};
$($check(&value)?;)?
::std::result::Result::Ok(value)
}
}
};
($name:ident $(< $($generic:ident $(: $bound:path)?),+ >)? ( $($field:ident),* $(,)? )) => {
impl $(< $($generic: $crate::archive::Archive $(+ $bound)?),+ >)? $crate::archive::Archive
for $name $(< $($generic),+ >)?
{
fn encode(
&self,
encoder: &mut $crate::archive::Encoder<'_>,
) -> ::std::result::Result<(), $crate::archive::ArchiveError> {
let Self($($field),*) = self;
$($crate::archive::Archive::encode($field, encoder)?;)*
::std::result::Result::Ok(())
}
fn decode(
decoder: &mut $crate::archive::Decoder<'_>,
) -> ::std::result::Result<Self, $crate::archive::ArchiveError> {
$(let $field = $crate::archive::Archive::decode(decoder)?;)*
::std::result::Result::Ok(Self($($field),*))
}
}
};
($name:ident) => {
impl $crate::archive::Archive for $name {
fn encode(
&self,
_encoder: &mut $crate::archive::Encoder<'_>,
) -> ::std::result::Result<(), $crate::archive::ArchiveError> {
::std::result::Result::Ok(())
}
fn decode(
_decoder: &mut $crate::archive::Decoder<'_>,
) -> ::std::result::Result<Self, $crate::archive::ArchiveError> {
::std::result::Result::Ok(Self)
}
}
};
}
#[macro_export]
macro_rules! archive_enum {
($name:ident $(< $($generic:ident $(: $bound:path)?),+ >)? {
$($variant:ident $(( $($tuple:ident),* $(,)? ))? $({ $($field:ident),* $(,)? })?),*
$(,)?
}) => {
impl $(< $($generic: $crate::archive::Archive $(+ $bound)?),+ >)? $crate::archive::Archive
for $name $(< $($generic),+ >)?
{
fn encode(
&self,
encoder: &mut $crate::archive::Encoder<'_>,
) -> ::std::result::Result<(), $crate::archive::ArchiveError> {
const NAMES: &[&str] = &[$(stringify!($variant)),*];
match self {
$(Self::$variant $(( $($tuple),* ))? $({ $($field),* })? => {
encoder.write_variant(NAMES, stringify!($variant));
$($($crate::archive::Archive::encode($tuple, encoder)?;)*)?
$($($crate::archive::Archive::encode($field, encoder)?;)*)?
})*
}
::std::result::Result::Ok(())
}
fn decode(
decoder: &mut $crate::archive::Decoder<'_>,
) -> ::std::result::Result<Self, $crate::archive::ArchiveError> {
const NAMES: &[&str] = &[$(stringify!($variant)),*];
let variant = decoder.read_variant(NAMES)?;
$(if variant == stringify!($variant) {
$($(let $tuple = $crate::archive::Archive::decode(decoder)?;)*)?
$($(let $field = $crate::archive::Archive::decode(decoder)?;)*)?
return ::std::result::Result::Ok(
Self::$variant $(( $($tuple),* ))? $({ $($field),* })?
);
})*
::std::result::Result::Err($crate::archive::ArchiveError::invalid(
concat!("unknown variant of ", stringify!($name)),
))
}
}
};
}
#[macro_export]
macro_rules! archive_kinds {
(dyn $trait:ident { $($tag:literal => $kind:ty),* $(,)? }) => {
impl $crate::archive::Archive for ::std::boxed::Box<dyn $trait> {
fn encode(
&self,
encoder: &mut $crate::archive::Encoder<'_>,
) -> ::std::result::Result<(), $crate::archive::ArchiveError> {
$crate::archive::ArchiveRc::encode_rc(&**self, encoder)
}
fn decode(
decoder: &mut $crate::archive::Decoder<'_>,
) -> ::std::result::Result<Self, $crate::archive::ArchiveError> {
let tag = decoder.read_str()?;
$(if tag == $tag {
let value: $kind = decoder.nested($crate::archive::Archive::decode)?;
return ::std::result::Result::Ok(::std::boxed::Box::new(value));
})*
if tag.is_empty() {
return decoder.read_program_kind::<Self>(
$crate::archive::KindOf::of::<dyn $trait>(stringify!($trait)),
);
}
::std::result::Result::Err($crate::archive::ArchiveError::invalid(format!(
"unknown kind {tag:?} of {}",
stringify!($trait),
)))
}
}
impl $crate::archive::ArchiveRc for dyn $trait {
fn encode_rc(
&self,
encoder: &mut $crate::archive::Encoder<'_>,
) -> ::std::result::Result<(), $crate::archive::ArchiveError> {
let any: &dyn ::std::any::Any = self;
$(if let ::std::option::Option::Some(value) = any.downcast_ref::<$kind>() {
encoder.write_str($tag);
return $crate::archive::Archive::encode(value, encoder);
})*
encoder.write_str("");
encoder.write_program_kind(
$crate::archive::KindOf::of::<dyn $trait>(stringify!($trait)),
any,
)
}
fn decode_rc(
decoder: &mut $crate::archive::Decoder<'_>,
) -> ::std::result::Result<::std::rc::Rc<Self>, $crate::archive::ArchiveError> {
<::std::boxed::Box<dyn $trait> as $crate::archive::Archive>::decode(decoder)
.map(::std::rc::Rc::from)
}
}
};
}