xso: Add an EmptyAsError text codec

As its name implies, this codec emits an error when the parsed string is
empty.
This commit is contained in:
Emmanuel Gil Peyrot 2024-07-24 17:47:35 +02:00 committed by Maxime “pep” Buquet
parent 4be297de11
commit 2b333ce579

View file

@ -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.