xso: add support for boxed children

This allows building recursive tree structures.
This commit is contained in:
Jonas Schäfer 2024-06-30 09:27:48 +02:00
parent 01336802b4
commit cd9f2033f3
3 changed files with 80 additions and 2 deletions

View file

@ -550,3 +550,40 @@ fn optional_child_roundtrip_absent() {
};
roundtrip_full::<OptionalChild>("<parent xmlns='urn:example:ns1'/>")
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "elem")]
struct BoxedChild {
#[xml(child(default))]
child: std::option::Option<Box<BoxedChild>>,
}
#[test]
fn boxed_child_roundtrip_absent() {
#[allow(unused_imports)]
use std::{
option::Option::{None, Some},
result::Result::{Err, Ok},
};
roundtrip_full::<BoxedChild>("<elem xmlns='urn:example:ns1'/>")
}
#[test]
fn boxed_child_roundtrip_nested_1() {
#[allow(unused_imports)]
use std::{
option::Option::{None, Some},
result::Result::{Err, Ok},
};
roundtrip_full::<BoxedChild>("<elem xmlns='urn:example:ns1'><elem/></elem>")
}
#[test]
fn boxed_child_roundtrip_nested_2() {
#[allow(unused_imports)]
use std::{
option::Option::{None, Some},
result::Result::{Err, Ok},
};
roundtrip_full::<BoxedChild>("<elem xmlns='urn:example:ns1'><elem><elem/></elem></elem>")
}

View file

@ -14,8 +14,8 @@ Version NEXT:
All this is to avoid triggering the camel case lint on the types we
generate.
* Added
- Support for child elements in derive macros. Child elements may be
wrapped in Option.
- Support for child elements in derive macros. Child elements may also
be wrapped in Option or Box.
Version 0.1.2:
2024-07-26 Jonas Schäfer <jonas@zombofant.net>

View file

@ -94,6 +94,17 @@ impl<'x, T: Iterator<Item = Result<Item<'x>, self::error::Error>>> Iterator for
}
}
/// Helper iterator to convert an `Box<T>` to XML.
pub struct BoxAsXml<T: Iterator>(Box<T>);
impl<'x, T: Iterator<Item = Result<Item<'x>, self::error::Error>>> Iterator for BoxAsXml<T> {
type Item = Result<Item<'x>, self::error::Error>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl<T: AsXml> AsXml for Option<T> {
type ItemIter<'x> = OptionAsXml<T::ItemIter<'x>> where T: 'x;
@ -105,6 +116,14 @@ impl<T: AsXml> AsXml for Option<T> {
}
}
impl<T: AsXml> AsXml for Box<T> {
type ItemIter<'x> = BoxAsXml<T::ItemIter<'x>> where T: 'x;
fn as_xml_iter(&self) -> Result<Self::ItemIter<'_>, self::error::Error> {
Ok(BoxAsXml(Box::new(T::as_xml_iter(&self)?)))
}
}
/// Trait for a temporary object allowing to construct a struct from
/// [`rxml::Event`] items.
///
@ -134,6 +153,9 @@ pub trait FromEventsBuilder {
/// Helper struct to construct an `Option<T>` from XML events.
pub struct OptionBuilder<T: FromEventsBuilder>(T);
/// Helper struct to construct an `Box<T>` from XML events.
pub struct BoxBuilder<T: FromEventsBuilder>(Box<T>);
impl<T: FromEventsBuilder> FromEventsBuilder for OptionBuilder<T> {
type Output = Option<T::Output>;
@ -142,6 +164,14 @@ impl<T: FromEventsBuilder> FromEventsBuilder for OptionBuilder<T> {
}
}
impl<T: FromEventsBuilder> FromEventsBuilder for BoxBuilder<T> {
type Output = Box<T::Output>;
fn feed(&mut self, ev: rxml::Event) -> Result<Option<Self::Output>, self::error::Error> {
self.0.feed(ev).map(|ok| ok.map(|value| Box::new(value)))
}
}
/// Trait allowing to construct a struct from a stream of
/// [`rxml::Event`] items.
///
@ -190,6 +220,17 @@ impl<T: FromXml> FromXml for Option<T> {
}
}
impl<T: FromXml> FromXml for Box<T> {
type Builder = BoxBuilder<T::Builder>;
fn from_events(
name: rxml::QName,
attrs: rxml::AttrMap,
) -> Result<Self::Builder, self::error::FromEventsError> {
Ok(BoxBuilder(Box::new(T::from_events(name, attrs)?)))
}
}
/// Trait allowing to convert XML text to a value.
///
/// This trait is similar to [`std::str::FromStr`], however, due to