2021-07-01 01:50:08 +00:00
|
|
|
#![macro_use]
|
|
|
|
|
2022-06-20 17:04:02 +00:00
|
|
|
/// Macro for deriving `Serialize` and `Deserialize` for string enums with an
|
|
|
|
/// `UNKNOWN(String)` variant.
|
|
|
|
///
|
2022-06-21 03:43:15 +00:00
|
|
|
/// Enum should have `#[derive(EnumString, EnumVariantNames, IntoStaticStr)]` included.
|
2022-06-20 17:04:02 +00:00
|
|
|
///
|
|
|
|
/// Also implements `AsRef<str>`, `Display`, and `From<&str>`.
|
|
|
|
macro_rules! serde_strum_unknown {
|
2022-06-20 16:40:28 +00:00
|
|
|
( $name:ident ) => {
|
2022-06-20 17:04:02 +00:00
|
|
|
impl AsRef<str> for $name {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Self::UNKNOWN(string) => &*string,
|
|
|
|
known => known.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::fmt::Display for $name {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
|
|
self.as_ref().fmt(f)
|
2022-06-20 16:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl serde::ser::Serialize for $name {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::ser::Serializer,
|
|
|
|
{
|
|
|
|
serializer.serialize_str(self.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 17:04:02 +00:00
|
|
|
impl From<&str> for $name {
|
|
|
|
fn from(item: &str) -> Self {
|
|
|
|
item.parse().unwrap()
|
2021-07-01 02:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 17:04:02 +00:00
|
|
|
impl<'de> serde::de::Deserialize<'de> for $name {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::de::Deserializer<'de>
|
|
|
|
{
|
2022-06-21 03:43:15 +00:00
|
|
|
#[cfg(not(feature = "deny-unknown-enum-variants-strings"))]
|
|
|
|
{
|
|
|
|
<&str>::deserialize(deserializer).map(Into::into)
|
|
|
|
}
|
|
|
|
#[cfg(feature = "deny-unknown-enum-variants-strings")]
|
|
|
|
{
|
|
|
|
<&str>::deserialize(deserializer).map(Into::into)
|
|
|
|
.and_then(|item| {
|
|
|
|
match item {
|
|
|
|
Self::UNKNOWN(unknown) => Err(serde::de::Error::unknown_variant(
|
|
|
|
&*unknown,
|
|
|
|
<Self as strum::VariantNames>::VARIANTS,
|
|
|
|
)),
|
|
|
|
other => Ok(other),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-07-01 02:35:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 01:50:08 +00:00
|
|
|
macro_rules! arr {
|
|
|
|
(
|
|
|
|
$( #[$attr:meta] )*
|
|
|
|
$v:vis $id:ident $name:ident: [$ty:ty; _] = $value:expr
|
|
|
|
) => {
|
|
|
|
$( #[$attr] )*
|
|
|
|
$v $id $name: [$ty; $value.len()] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 03:43:15 +00:00
|
|
|
/// Macro for newtype "enums" with integer values.
|
|
|
|
///
|
|
|
|
/// For serde, use the following:
|
|
|
|
/// ```ignore
|
|
|
|
/// #[derive(Serialize, Deserialize)]
|
|
|
|
/// #[serde(from = "$repr", into = "$repr")]
|
|
|
|
/// ```
|
2021-07-01 01:50:08 +00:00
|
|
|
macro_rules! newtype_enum {
|
|
|
|
{
|
|
|
|
$( #[$attr:meta] )*
|
2021-07-01 02:35:08 +00:00
|
|
|
$v:vis newtype_enum $name:ident($repr:ty) {
|
2021-07-01 01:50:08 +00:00
|
|
|
$(
|
|
|
|
$( #[$var_attr:meta] )*
|
|
|
|
$var_name:ident = $var_val:expr,
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
} => {
|
|
|
|
$( #[$attr] )*
|
|
|
|
#[derive(Copy, Clone)]
|
2021-07-01 02:40:48 +00:00
|
|
|
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
|
2021-07-01 01:50:08 +00:00
|
|
|
#[repr(transparent)]
|
|
|
|
$v struct $name($v $repr);
|
|
|
|
impl $name {
|
|
|
|
$(
|
|
|
|
$( #[$var_attr] )*
|
2021-07-01 02:35:08 +00:00
|
|
|
$v const $var_name: Self = Self($var_val);
|
2021-07-01 01:50:08 +00:00
|
|
|
)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
arr!{
|
2021-07-01 03:09:24 +00:00
|
|
|
#[doc = "Array containing all known variants."]
|
2021-07-01 01:50:08 +00:00
|
|
|
pub const ALL_KNOWN: [Self; _] = [
|
|
|
|
$( Self::$var_name, )*
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "If this is one of the known variants."]
|
2021-07-01 03:09:24 +00:00
|
|
|
$v const fn is_known(self) -> bool {
|
2021-07-01 01:50:08 +00:00
|
|
|
match self {
|
|
|
|
$(
|
|
|
|
Self::$var_name => true,
|
|
|
|
)*
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 03:43:15 +00:00
|
|
|
impl std::convert::From<$name> for $repr {
|
|
|
|
fn from(value: $name ) -> Self {
|
|
|
|
value.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl serde::ser::Serialize for $name {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::ser::Serializer,
|
|
|
|
{
|
|
|
|
<$repr>::serialize(&self.0, serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 01:50:08 +00:00
|
|
|
impl std::convert::From<$repr> for $name {
|
|
|
|
fn from(value: $repr ) -> Self {
|
|
|
|
Self(value)
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 03:43:15 +00:00
|
|
|
impl<'de> serde::de::Deserialize<'de> for $name {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::de::Deserializer<'de>
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "deny-unknown-enum-variants-integers"))]
|
|
|
|
{
|
|
|
|
<$repr>::deserialize(deserializer).map(Into::into)
|
|
|
|
}
|
|
|
|
#[cfg(feature = "deny-unknown-enum-variants-integers")]
|
|
|
|
{
|
|
|
|
<$repr>::deserialize(deserializer).map(Into::into)
|
|
|
|
.and_then(|item: Self| {
|
|
|
|
if !item.is_known() {
|
|
|
|
Err(serde::de::Error::custom(format!(
|
|
|
|
"Unknown integer enum variant: {} (\"deny-unknown-enum-variants-integers\" feature is enabled).\nExpected one of the following: {:?}",
|
|
|
|
item, Self::ALL_KNOWN
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Ok(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-07-01 01:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for $name {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::fmt::Debug for $name {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}({}{})", stringify!($name), self.0, if self.is_known() { "" } else { "?" })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|