xso: Add an EmptyAsError text codec

Emmanuel Gil Peyrot created

As its name implies, this codec emits an error when the parsed string is
empty.

Change summary

xso/src/text.rs | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)

Detailed changes

xso/src/text.rs 🔗

@@ -171,6 +171,27 @@ impl TextCodec<Option<String>> for EmptyAsNone {
     }
 }
 
+/// Text codec which returns None instead of the empty string.
+pub struct EmptyAsError;
+
+impl TextCodec<String> for EmptyAsError {
+    fn decode(s: String) -> Result<String, Error> {
+        if s.is_empty() {
+            Err(Error::Other("Empty text node."))
+        } else {
+            Ok(s)
+        }
+    }
+
+    fn encode(value: &String) -> Result<Option<Cow<'_, str>>, Error> {
+        if value.is_empty() {
+            Err(Error::Other("Empty text node."))
+        } else {
+            Ok(Some(Cow::Borrowed(value.as_str())))
+        }
+    }
+}
+
 /// Trait for preprocessing text data from XML.
 ///
 /// This may be used by codecs to allow to customize some of their behaviour.