macro_rules! wire_enum {
(
$(#[$attr:meta])*
pub enum $name:ident {
$($(#[$variant_attr:meta])* $variant:ident = $value:literal,)+
}
) => {
$(#[$attr])*
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum $name {
$($(#[$variant_attr])* $variant,)+
Unknown(::std::string::String),
}
impl $name {
#[cfg(test)]
pub(crate) const VALUES: &[&str] = &[$($value),+];
pub fn as_str(&self) -> &str {
match self {
$(Self::$variant => $value,)+
Self::Unknown(value) => value,
}
}
}
impl ::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
f.pad(self.as_str())
}
}
impl ::serde::Serialize for $name {
fn serialize<S: ::serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> ::serde::Deserialize<'de> for $name {
fn deserialize<D: ::serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = <::std::string::String as ::serde::Deserialize>::deserialize(deserializer)?;
Ok(match value.as_str() {
$($value => Self::$variant,)+
_ => Self::Unknown(value),
})
}
}
};
}