feat: improve deserialization capabilities of bool enums

This commit is contained in:
RobWalt 2023-10-22 12:25:43 +02:00
parent d1e5fd27ca
commit 4961874386
No known key found for this signature in database
GPG key ID: 333C6AC0CEF0CE68

View file

@ -20,6 +20,7 @@ macro_rules! implement_boolean_enum {
mod serde {
use serde::de::Visitor;
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;
use super::$enum_name;
@ -51,6 +52,15 @@ macro_rules! implement_boolean_enum {
Ok($enum_name::$no_variant)
}
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
$enum_name::iter()
.find(|s| s.to_string().to_lowercase() == v.to_lowercase())
.ok_or(E::custom("not string"))
}
}
impl<'de> Deserialize<'de> for $enum_name {
@ -58,7 +68,7 @@ macro_rules! implement_boolean_enum {
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_bool(PrivateVisitor)
deserializer.deserialize_any(PrivateVisitor)
}
}
}