xso: add text conversion traits for AsXml

This commit is contained in:
Jonas Schäfer 2024-07-09 16:57:45 +02:00
commit 4910b01244
2 changed files with 107 additions and 1 deletions

View file

@ -9,7 +9,9 @@
#[cfg(feature = "base64")]
use core::marker::PhantomData;
use crate::{error::Error, FromXmlText, IntoXmlText};
use std::borrow::Cow;
use crate::{error::Error, AsXmlText, FromXmlText, IntoXmlText};
#[cfg(feature = "base64")]
use base64::engine::{general_purpose::STANDARD as StandardBase64Engine, Engine as _};
@ -40,6 +42,16 @@ macro_rules! convert_via_fromstr_and_display {
Ok(self.to_string())
}
}
$(
#[cfg(feature = $feature)]
#[cfg_attr(docsrs, doc(cfg(feature = $feature)))]
)?
impl AsXmlText for $t {
fn as_xml_text(&self) -> Result<Cow<'_, str>, Error> {
Ok(Cow::Owned(self.to_string()))
}
}
)+
}
}
@ -64,6 +76,16 @@ impl IntoXmlText for bool {
}
}
/// This provides an implementation compliant with xsd::bool.
impl AsXmlText for bool {
fn as_xml_text(&self) -> Result<Cow<'_, str>, Error> {
match self {
true => Ok(Cow::Borrowed("true")),
false => Ok(Cow::Borrowed("false")),
}
}
}
convert_via_fromstr_and_display! {
u8,
u16,