From 4961874386903045666547d80743f6e3ee11445e Mon Sep 17 00:00:00 2001 From: RobWalt Date: Sun, 22 Oct 2023 12:25:43 +0200 Subject: [PATCH] feat: improve deserialization capabilities of bool enums --- src/macros.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 84bccc8..1afeca4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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(self, v: &str) -> Result + 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) } } }