//! # Generic builder type implementations //! //! This module contains [`FromEventsBuilder`] implementations for types from //! foreign libraries (such as the standard library). //! //! In order to not clutter the `xso` crate's main namespace, they are //! stashed away in a separate module. // Copyright (c) 2024 Jonas Schäfer // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. use crate::error::{Error, FromEventsError}; use crate::{FromEventsBuilder, FromXml}; /// Helper struct to construct an `Option` from XML events. pub struct OptionBuilder(T); impl FromEventsBuilder for OptionBuilder { type Output = Option; fn feed(&mut self, ev: rxml::Event) -> Result, Error> { self.0.feed(ev).map(|ok| ok.map(|value| Some(value))) } } impl FromXml for Option { type Builder = OptionBuilder; fn from_events( name: rxml::QName, attrs: rxml::AttrMap, ) -> Result { Ok(OptionBuilder(T::from_events(name, attrs)?)) } } /// Helper struct to construct an `Box` from XML events. pub struct BoxBuilder(Box); impl FromEventsBuilder for BoxBuilder { type Output = Box; fn feed(&mut self, ev: rxml::Event) -> Result, Error> { self.0.feed(ev).map(|ok| ok.map(|value| Box::new(value))) } } impl FromXml for Box { type Builder = BoxBuilder; fn from_events( name: rxml::QName, attrs: rxml::AttrMap, ) -> Result { Ok(BoxBuilder(Box::new(T::from_events(name, attrs)?))) } }