From f91b3b6a6c406b821db8b799de8138fe6aca5809 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 19 Dec 2025 20:57:40 +0000 Subject: [PATCH] Add string util for `#[serde(deserialize_with = "deserialize::to_lowercase")]` Signed-off-by: Jason Volk --- src/core/utils/string.rs | 1 + src/core/utils/string/de.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/core/utils/string/de.rs diff --git a/src/core/utils/string.rs b/src/core/utils/string.rs index 12b1ef2e..854d5d4b 100644 --- a/src/core/utils/string.rs +++ b/src/core/utils/string.rs @@ -1,4 +1,5 @@ mod between; +pub mod de; mod split; mod tests; mod unquote; diff --git a/src/core/utils/string/de.rs b/src/core/utils/string/de.rs new file mode 100644 index 00000000..08eb403c --- /dev/null +++ b/src/core/utils/string/de.rs @@ -0,0 +1,22 @@ +use std::fmt; + +use serde::de::{Deserializer, Error, Visitor}; + +struct ToLowercase; + +#[inline] +pub fn to_lowercase<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + deserializer.deserialize_string(ToLowercase) +} + +impl Visitor<'_> for ToLowercase { + type Value = String; + + #[inline] + fn visit_str(self, v: &str) -> Result { Ok(v.to_lowercase()) } + + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("String") } +}