diff --git a/xso/src/lib.rs b/xso/src/lib.rs index 9d540748..9daaa2d0 100644 --- a/xso/src/lib.rs +++ b/xso/src/lib.rs @@ -58,27 +58,6 @@ pub use xso_proc::FromXml; #[cfg(feature = "macros")] pub use xso_proc::AsXml; -/// Trait allowing to consume a struct and iterate its contents as -/// serialisable [`rxml::Event`] items. -/// -/// **Important:** Changing the [`EventIter`][`Self::EventIter`] associated -/// type is considered a non-breaking change for any given implementation of -/// this trait. Always refer to a type's iterator type using fully-qualified -/// notation, for example: `::EventIter`. -pub trait IntoXml { - /// The iterator type. - /// - /// **Important:** Changing this type is considered a non-breaking change - /// for any given implementation of this trait. Always refer to a type's - /// iterator type using fully-qualified notation, for example: - /// `::EventIter`. - type EventIter: Iterator>; - - /// Return an iterator which emits the contents of the struct or enum as - /// serialisable [`rxml::Event`] items. - fn into_event_iter(self) -> Result; -} - /// Trait allowing to iterate a struct's contents as serialisable /// [`Item`]s. /// @@ -208,83 +187,6 @@ impl FromXmlText for Box { } } -/// Trait to convert a value to an XML text string. -/// -/// This trait is implemented for many standard library types implementing -/// [`std::fmt::Display`]. In addition, the following feature flags can enable -/// more implementations: -/// -/// - `jid`: `jid::Jid`, `jid::BareJid`, `jid::FullJid` -/// - `uuid`: `uuid::Uuid` -/// -/// Because of the unfortunate situation as described in [`FromXmlText`], we -/// are **extremely liberal** with accepting optional dependencies for this -/// purpose. You are very welcome to make merge requests against this crate -/// adding support for parsing third-party crates. -pub trait IntoXmlText: Sized { - /// Convert the value to an XML string in a context where an absent value - /// cannot be represented. - fn into_xml_text(self) -> Result; - - /// Convert the value to an XML string in a context where an absent value - /// can be represented. - /// - /// The provided implementation will always return the result of - /// [`Self::into_xml_text`] wrapped into `Some(.)`. By re-implementing - /// this method, implementors can customize the behaviour for certain - /// values. - fn into_optional_xml_text(self) -> Result, self::error::Error> { - Ok(Some(self.into_xml_text()?)) - } -} - -impl IntoXmlText for String { - fn into_xml_text(self) -> Result { - Ok(self) - } -} - -impl IntoXmlText for Box { - fn into_xml_text(self) -> Result { - T::into_xml_text(*self) - } -} - -impl> IntoXmlText for Cow<'_, B> { - fn into_xml_text(self) -> Result { - T::into_xml_text(self.into_owned()) - } -} - -/// Specialized variant of [`IntoXmlText`]. -/// -/// Do **not** implement this unless you cannot implement [`IntoXmlText`]: -/// implementing [`IntoXmlText`] is more versatile and an -/// [`IntoOptionalXmlText`] implementation is automatically provided. -/// -/// If you need to customize the behaviour of the [`IntoOptionalXmlText`] -/// blanket implementation, implement a custom -/// [`IntoXmlText::into_optional_xml_text`] instead. -pub trait IntoOptionalXmlText { - /// Convert the value to an XML string in a context where an absent value - /// can be represented. - fn into_optional_xml_text(self) -> Result, self::error::Error>; -} - -impl IntoOptionalXmlText for T { - fn into_optional_xml_text(self) -> Result, self::error::Error> { - ::into_optional_xml_text(self) - } -} - -impl IntoOptionalXmlText for Option { - fn into_optional_xml_text(self) -> Result, self::error::Error> { - self.map(T::into_optional_xml_text) - .transpose() - .map(Option::flatten) - } -} - /// Trait to convert a value to an XML text string. /// /// This trait is implemented for many standard library types implementing diff --git a/xso/src/minidom_compat.rs b/xso/src/minidom_compat.rs index 5ac68adb..b7f74ce4 100644 --- a/xso/src/minidom_compat.rs +++ b/xso/src/minidom_compat.rs @@ -20,7 +20,7 @@ use rxml::{ use crate::{ error::{Error, FromEventsError}, rxml_util::{EventToItem, Item}, - AsXml, FromEventsBuilder, FromXml, IntoXml, + AsXml, FromEventsBuilder, FromXml, }; /// State machine for converting a minidom Element into rxml events. @@ -115,7 +115,7 @@ impl IntoEventsInner { return Ok(Some(Event::Text(EventMetrics::zero(), text))); } Some(Node::Element(el)) => { - *nested = Some(Box::new(el.into_event_iter()?)); + *nested = Some(Box::new(IntoEvents::new(el))); // fallthrough to next loop iteration } None => { @@ -136,7 +136,13 @@ impl IntoEventsInner { /// This can be constructed from the /// [`IntoXml::into_event_iter`][`crate::IntoXml::into_event_iter`] /// implementation on [`minidom::Element`]. -pub struct IntoEvents(IntoEventsInner); +struct IntoEvents(IntoEventsInner); + +impl IntoEvents { + fn new(el: Element) -> Self { + IntoEvents(IntoEventsInner::Header(el)) + } +} impl Iterator for IntoEvents { type Item = Result; @@ -146,14 +152,6 @@ impl Iterator for IntoEvents { } } -impl IntoXml for Element { - type EventIter = IntoEvents; - - fn into_event_iter(self) -> Result { - Ok(IntoEvents(IntoEventsInner::Header(self))) - } -} - enum AsXmlState<'a> { /// Element header: we need to generate the /// [`Item::ElementHeadStart`] item from the namespace and name. @@ -425,38 +423,10 @@ where } } -/// Helper struct to stream a struct which implements conversion -/// to [`minidom::Element`]. -pub struct IntoEventsViaElement { - inner: IntoEvents, -} - -impl IntoEventsViaElement { - /// Create a new streaming parser for `T`. - pub fn new(value: T) -> Result - where - Error: From, - minidom::Element: TryFrom, - { - let element: minidom::Element = value.try_into()?; - Ok(Self { - inner: element.into_event_iter()?, - }) - } -} - -impl Iterator for IntoEventsViaElement { - type Item = Result; - - fn next(&mut self) -> Option { - self.inner.next() - } -} - /// Helper struct to stream a struct which implements conversion /// to [`minidom::Element`]. pub struct AsItemsViaElement<'x> { - iter: EventToItem, + iter: EventToItem, lifetime_binding: PhantomData>, } @@ -467,8 +437,9 @@ impl<'x> AsItemsViaElement<'x> { Error: From, minidom::Element: TryFrom, { + let element: minidom::Element = value.try_into()?; Ok(Self { - iter: EventToItem::new(IntoEventsViaElement::new(value)?), + iter: EventToItem::new(IntoEvents::new(element)), lifetime_binding: PhantomData, }) } diff --git a/xso/src/text.rs b/xso/src/text.rs index 767062e5..9ad3a8ea 100644 --- a/xso/src/text.rs +++ b/xso/src/text.rs @@ -11,7 +11,7 @@ use core::marker::PhantomData; use std::borrow::Cow; -use crate::{error::Error, AsXmlText, FromXmlText, IntoXmlText}; +use crate::{error::Error, AsXmlText, FromXmlText}; #[cfg(feature = "base64")] use base64::engine::{general_purpose::STANDARD as StandardBase64Engine, Engine as _}; @@ -33,16 +33,6 @@ macro_rules! convert_via_fromstr_and_display { } } - $( - #[cfg(feature = $feature)] - #[cfg_attr(docsrs, doc(cfg(feature = $feature)))] - )? - impl IntoXmlText for $t { - fn into_xml_text(self) -> Result { - Ok(self.to_string()) - } - } - $( #[cfg(feature = $feature)] #[cfg_attr(docsrs, doc(cfg(feature = $feature)))] @@ -69,13 +59,6 @@ impl FromXmlText for bool { } } -/// This provides an implementation compliant with xsd::bool. -impl IntoXmlText for bool { - fn into_xml_text(self) -> Result { - Ok(self.to_string()) - } -} - /// This provides an implementation compliant with xsd::bool. impl AsXmlText for bool { fn as_xml_text(&self) -> Result, Error> {