feat: improve deserialization capabilities of bool enums
This commit is contained in:
parent
d1e5fd27ca
commit
4961874386
1 changed files with 11 additions and 1 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue