Fix deserialize_char to support multi-byte Unicode characters
Use chars().count() instead of len() (byte length) to check for single
characters. Previously, valid single characters like "é", "中", and "🦀"
were rejected because their UTF-8 encoding is more than one byte.
diff --git a/src/de.rs b/src/de.rs
index f74a38e..1f12e8b 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -988,7 +988,9 @@ impl<'de, 'a> de::Deserializer<'de> for ValueDeserializer<'a> {
fn deserialize_char<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
match self.value {
- KdlValue::String(s) if s.len() == 1 => visitor.visit_char(s.chars().next().unwrap()),
+ KdlValue::String(s) if s.chars().count() == 1 => {
+ visitor.visit_char(s.chars().next().unwrap())
+ }
other => Err(Error::TypeMismatch {
expected: "single character",
got: format!("{other:?}"),
diff --git a/tests/integration.rs b/tests/integration.rs
index 50d6c30..e350271 100644
--- a/tests/integration.rs
+++ b/tests/integration.rs
@@ -718,6 +718,9 @@ roundtrip!(
}
);
roundtrip!(roundtrip_char, WCh, WCh { letter: 'X' });
+roundtrip!(roundtrip_char_multibyte, WCh, WCh { letter: 'é' });
+roundtrip!(roundtrip_char_cjk, WCh, WCh { letter: '中' });
+roundtrip!(roundtrip_char_emoji, WCh, WCh { letter: '🦀' });
roundtrip!(
roundtrip_i128_u128,
WBi,