Port everything over to AsXml

This commit is contained in:
Jonas Schäfer 2024-07-09 17:01:42 +02:00
commit ccf38cdf9b
64 changed files with 848 additions and 371 deletions

View file

@ -19,9 +19,9 @@ mod helpers {
// this is to ensure that the macros do not have hidden dependencies on
// any specific names being imported.
use minidom::Element;
use xso::{error::FromElementError, transform, try_from_element, FromXml, IntoXml};
use xso::{error::FromElementError, transform, try_from_element, AsXml, FromXml};
pub(super) fn roundtrip_full<T: IntoXml + FromXml + PartialEq + std::fmt::Debug + Clone>(
pub(super) fn roundtrip_full<T: AsXml + FromXml + PartialEq + std::fmt::Debug + Clone>(
s: &str,
) {
let initial: Element = s.parse().unwrap();
@ -47,7 +47,7 @@ mod helpers {
use self::helpers::{parse_str, roundtrip_full};
use xso::{FromXml, IntoXml};
use xso::{AsXml, FromXml};
// these are adverserial local names in order to trigger any issues with
// unqualified names in the macro expansions.
@ -81,7 +81,7 @@ static BAR_NAME: &::xso::exports::rxml::strings::NcNameStr = {
}
};
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "foo")]
struct Empty;
@ -164,7 +164,7 @@ fn empty_qname_check_has_precedence_over_attr_check() {
}
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = BAR_NAME)]
struct NamePath;
@ -178,7 +178,7 @@ fn name_path_roundtrip() {
roundtrip_full::<NamePath>("<bar xmlns='urn:example:ns1'/>");
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = "urn:example:ns2", name = "baz")]
struct NamespaceLit;
@ -192,7 +192,7 @@ fn namespace_lit_roundtrip() {
roundtrip_full::<NamespaceLit>("<baz xmlns='urn:example:ns2'/>");
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "attr")]
struct RequiredAttribute {
#[xml(attribute)]
@ -237,7 +237,7 @@ fn required_attribute_missing() {
}
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "attr")]
struct RenamedAttribute {
#[xml(attribute = "a1")]
@ -256,7 +256,7 @@ fn renamed_attribute_roundtrip() {
roundtrip_full::<RenamedAttribute>("<attr xmlns='urn:example:ns1' a1='bar' bar='baz'/>");
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "attr")]
struct NamespacedAttribute {
#[xml(attribute(namespace = "urn:example:ns1", name = FOO_NAME))]
@ -297,7 +297,7 @@ fn namespaced_attribute_roundtrip_b() {
);
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "attr")]
struct PrefixedAttribute {
#[xml(attribute = "xml:lang")]
@ -314,7 +314,7 @@ fn prefixed_attribute_roundtrip() {
roundtrip_full::<PrefixedAttribute>("<attr xmlns='urn:example:ns1' xml:lang='foo'/>");
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "attr")]
struct RequiredNonStringAttribute {
#[xml(attribute)]
@ -331,7 +331,7 @@ fn required_non_string_attribute_roundtrip() {
roundtrip_full::<RequiredNonStringAttribute>("<attr xmlns='urn:example:ns1' foo='-16'/>");
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "attr")]
struct DefaultAttribute {
#[xml(attribute(default))]
@ -381,7 +381,7 @@ fn default_attribute_roundtrip_pp() {
roundtrip_full::<DefaultAttribute>("<attr xmlns='urn:example:ns1' foo='xyz' bar='16'/>");
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "text")]
struct TextString {
#[xml(text)]
@ -409,7 +409,7 @@ fn text_string_positive_preserves_whitespace() {
assert_eq!(el.text, " \t\n");
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "text")]
struct TextNonString {
#[xml(text)]
@ -426,7 +426,7 @@ fn text_non_string_roundtrip() {
roundtrip_full::<TextNonString>("<text xmlns='urn:example:ns1'>123456</text>");
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "elem")]
struct IgnoresWhitespaceWithoutTextConsumer;
@ -443,7 +443,7 @@ fn ignores_whitespace_without_text_consumer_positive() {
.unwrap();
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "elem")]
struct FailsTextWithoutTextConsumer;
@ -465,7 +465,7 @@ fn fails_text_without_text_consumer_positive() {
}
}
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "text")]
struct TextWithCodec {
#[xml(text(codec = xso::text::EmptyAsNone))]

View file

@ -94,9 +94,13 @@ macro_rules! generate_attribute {
})
}
}
impl ::xso::IntoXmlText for $elem {
fn into_xml_text(self) -> Result<String, xso::error::Error> {
Ok(self.to_string())
impl ::xso::AsXmlText for $elem {
fn as_xml_text(&self) -> Result<::std::borrow::Cow<'_, str>, xso::error::Error> {
match self {
$(
$elem::$a => Ok(::std::borrow::Cow::Borrowed($b))
),+
}
}
}
impl ::minidom::IntoAttributeValue for $elem {
@ -130,16 +134,16 @@ macro_rules! generate_attribute {
s.parse().map_err(xso::error::Error::text_parse_error)
}
}
impl ::xso::IntoXmlText for $elem {
fn into_xml_text(self) -> Result<String, xso::error::Error> {
Ok(String::from(match self {
impl ::xso::AsXmlText for $elem {
fn as_xml_text(&self) -> Result<std::borrow::Cow<'_, str>, xso::error::Error> {
Ok(std::borrow::Cow::Borrowed(match self {
$($elem::$a => $b),+
}))
}
#[allow(unreachable_patterns)]
fn into_optional_xml_text(self) -> Result<Option<String>, xso::error::Error> {
Ok(Some(String::from(match self {
fn as_optional_xml_text(&self) -> Result<Option<std::borrow::Cow<'_, str>>, xso::error::Error> {
Ok(Some(std::borrow::Cow::Borrowed(match self {
$elem::$default => return Ok(None),
$($elem::$a => $b),+
})))
@ -219,17 +223,17 @@ macro_rules! generate_attribute {
}
}
}
impl ::xso::IntoXmlText for $elem {
fn into_xml_text(self) -> Result<String, xso::error::Error> {
impl ::xso::AsXmlText for $elem {
fn as_xml_text(&self) -> Result<::std::borrow::Cow<'_, str>, xso::error::Error> {
match self {
Self::True => Ok("true".to_owned()),
Self::False => Ok("false".to_owned()),
Self::True => Ok(::std::borrow::Cow::Borrowed("true")),
Self::False => Ok(::std::borrow::Cow::Borrowed("false")),
}
}
fn into_optional_xml_text(self) -> Result<Option<String>, xso::error::Error> {
fn as_optional_xml_text(&self) -> Result<Option<::std::borrow::Cow<'_, str>>, xso::error::Error> {
match self {
Self::True => Ok(Some("true".to_owned())),
Self::True => Ok(Some(::std::borrow::Cow::Borrowed("true"))),
Self::False => Ok(None),
}
}
@ -435,9 +439,9 @@ macro_rules! generate_id {
Ok(Self(s))
}
}
impl ::xso::IntoXmlText for $elem {
fn into_xml_text(self) ->Result<String, xso::error::Error> {
Ok(self.0)
impl ::xso::AsXmlText for $elem {
fn as_xml_text(&self) ->Result<::std::borrow::Cow<'_, str>, xso::error::Error> {
Ok(::std::borrow::Cow::Borrowed(self.0.as_str()))
}
}
impl ::minidom::IntoAttributeValue for $elem {
@ -803,11 +807,11 @@ macro_rules! generate_element {
}
}
impl ::xso::IntoXml for $elem {
type EventIter = ::xso::minidom_compat::IntoEventsViaElement;
impl ::xso::AsXml for $elem {
type ItemIter<'x> = ::xso::minidom_compat::AsItemsViaElement<'x>;
fn into_event_iter(self) -> Result<Self::EventIter, ::xso::error::Error> {
Self::EventIter::new(self)
fn as_xml_iter(&self) -> Result<Self::ItemIter<'_>, ::xso::error::Error> {
::xso::minidom_compat::AsItemsViaElement::new(self.clone())
}
}
);