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:
parent
4be297de11
commit
2b333ce579
1 changed files with 21 additions and 0 deletions
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue