use std::fmt;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UnknownValue(String);
impl UnknownValue {
pub(crate) fn new(value: String) -> Self {
Self(value)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for UnknownValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl fmt::Display for UnknownValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
macro_rules! open_enum {
(
$(#[$meta:meta])*
pub enum $name:ident {
$( $(#[$variant_meta:meta])* $variant:ident = $wire:literal, )+
}
) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum $name {
$( #[doc = concat!("`", $wire, "`")] $(#[$variant_meta])* $variant, )+
Unknown($crate::types::UnknownValue),
}
impl $name {
pub fn as_str(&self) -> &str {
match self {
$( Self::$variant => $wire, )+
Self::Unknown(value) => value.as_str(),
}
}
pub fn is_unknown(&self) -> bool {
matches!(self, Self::Unknown(_))
}
fn known(value: &str) -> Option<Self> {
match value {
$( $wire => Some(Self::$variant), )+
_ => None,
}
}
}
impl ::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
f.write_str(self.as_str())
}
}
impl ::std::str::FromStr for $name {
type Err = ::std::convert::Infallible;
fn from_str(value: &str) -> ::std::result::Result<Self, Self::Err> {
Ok(Self::known(value).unwrap_or_else(|| {
Self::Unknown($crate::types::UnknownValue::new(value.to_owned()))
}))
}
}
impl ::serde::Serialize for $name {
fn serialize<S: ::serde::Serializer>(
&self,
serializer: S,
) -> ::std::result::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,
) -> ::std::result::Result<Self, D::Error> {
let value = <::std::string::String as ::serde::Deserialize>::deserialize(deserializer)?;
Ok(Self::known(&value)
.unwrap_or_else(|| Self::Unknown($crate::types::UnknownValue::new(value))))
}
}
#[cfg(test)]
impl $crate::model::conformance::Variant for $name {
fn is_known(&self) -> bool {
!self.is_unknown()
}
}
};
}
pub(crate) use open_enum;