xso: provide adapter for AsXml implementation based on Into<Element>

Analogous to the already existing `IntoXml` implementation helpers
based on an `Into<Element>` implementation on a type, this provides
the utilities for `AsXml`.

This is of course exceptionally inefficient, but it is only needed
transitionally until we have everything migrated to derive macros or
otherwise rewritten in terms of AsXml/FromXml.
This commit is contained in:
Jonas Schäfer 2024-07-09 16:54:08 +02:00
commit 569b6e327d
2 changed files with 247 additions and 1 deletions

View file

@ -19,7 +19,7 @@ use rxml::{
use crate::{
error::{Error, FromEventsError},
rxml_util::Item,
rxml_util::{EventToItem, Item},
AsXml, FromEventsBuilder, FromXml, IntoXml,
};
@ -453,6 +453,35 @@ impl Iterator for IntoEventsViaElement {
}
}
/// Helper struct to stream a struct which implements conversion
/// to [`minidom::Element`].
pub struct AsItemsViaElement<'x> {
iter: EventToItem<IntoEventsViaElement>,
lifetime_binding: PhantomData<Item<'x>>,
}
impl<'x> AsItemsViaElement<'x> {
/// Create a new streaming parser for `T`.
pub fn new<E, T>(value: T) -> Result<Self, crate::error::Error>
where
Error: From<E>,
minidom::Element: TryFrom<T, Error = E>,
{
Ok(Self {
iter: EventToItem::new(IntoEventsViaElement::new(value)?),
lifetime_binding: PhantomData,
})
}
}
impl<'x> Iterator for AsItemsViaElement<'x> {
type Item = Result<Item<'x>, Error>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|x| x.map(Item::into_owned))
}
}
#[cfg(test)]
mod tests {
use super::*;