diff --git a/xso/Cargo.toml b/xso/Cargo.toml index 17083b257534f7b16c64705d4e62e5d79855cb3c..7096307ea5f837afc7ed190456f06ad347ed9de4 100644 --- a/xso/Cargo.toml +++ b/xso/Cargo.toml @@ -25,9 +25,11 @@ uuid = { version = "1", optional = true } base64 = { version = "0.22", optional = true } [features] +default = [ "std" ] macros = [ "dep:xso_proc", "rxml/macros" ] minidom = [ "xso_proc/minidom"] panicking-into-impl = ["xso_proc/panicking-into-impl"] +std = [] [package.metadata.docs.rs] all-features = true diff --git a/xso/src/asxml.rs b/xso/src/asxml.rs index a3d17996c598046a343aa967c5dbf4cddc1fcfa9..2fe670414b667f4e50b14ece7ea902e76dda1a59 100644 --- a/xso/src/asxml.rs +++ b/xso/src/asxml.rs @@ -12,6 +12,8 @@ // 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 alloc::boxed::Box; + use crate::error::Error; use crate::rxml_util::Item; use crate::AsXml; @@ -99,11 +101,11 @@ where mod tests { use super::*; - use std::borrow::Cow; + use alloc::{borrow::Cow, vec}; #[test] fn option_as_xml_terminates_immediately_for_none() { - let mut iter = OptionAsXml::>(None); + let mut iter = OptionAsXml::>(None); match iter.next() { None => (), other => panic!("unexpected item: {:?}", other), diff --git a/xso/src/error.rs b/xso/src/error.rs index f7dd9b98f950549d5eecbeda56eaf2f1922706d9..b6304261032015ace37d8170a52b7a30d898054c 100644 --- a/xso/src/error.rs +++ b/xso/src/error.rs @@ -9,6 +9,11 @@ This module contains the error types used throughout the `xso` crate. // 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 alloc::{ + boxed::Box, + string::{String, ToString}, +}; use core::fmt; use rxml::Error as XmlError; @@ -31,7 +36,7 @@ impl fmt::Display for OpaqueError { } } -impl std::error::Error for OpaqueError {} +impl core::error::Error for OpaqueError {} /// Error variants generated while parsing or serialising XML data. #[derive(Debug)] @@ -40,7 +45,7 @@ pub enum Error { XmlError(XmlError), /// Attempt to parse text data failed with the provided nested error. - TextParseError(Box), + TextParseError(Box), /// Generic, unspecified other error. Other(&'static str), @@ -58,7 +63,7 @@ impl Error { /// /// This includes the `Box::new(.)` call, making it directly usable as /// argument to [`Result::map_err`]. - pub fn text_parse_error(e: T) -> Self { + pub fn text_parse_error(e: T) -> Self { Self::TextParseError(Box::new(e)) } } @@ -90,8 +95,8 @@ impl fmt::Display for Error { } } -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { +impl core::error::Error for Error { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { match self { Self::XmlError(ref e) => Some(e), Self::TextParseError(ref e) => Some(&**e), @@ -159,8 +164,8 @@ impl fmt::Display for FromEventsError { } } -impl std::error::Error for FromEventsError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { +impl core::error::Error for FromEventsError { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { match self { Self::Mismatch { .. } => None, Self::Invalid(ref e) => Some(e), @@ -201,8 +206,8 @@ impl fmt::Display for FromElementError { } } -impl std::error::Error for FromElementError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { +impl core::error::Error for FromElementError { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { match self { Self::Mismatch(_) => None, Self::Invalid(ref e) => Some(e), diff --git a/xso/src/fromxml.rs b/xso/src/fromxml.rs index 366bb1c82d201dd49f24cb396fa981df4eff8ee2..431a2355e096e0a532e65e3d6630f111ef2ed26e 100644 --- a/xso/src/fromxml.rs +++ b/xso/src/fromxml.rs @@ -12,6 +12,8 @@ // 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 alloc::boxed::Box; + use crate::error::{Error, FromEventsError}; use crate::{FromEventsBuilder, FromXml}; @@ -271,6 +273,7 @@ impl FromEventsBuilder for Discard { mod tests { use super::*; + use alloc::borrow::ToOwned; use rxml::{parser::EventMetrics, Event, Namespace, NcName}; macro_rules! null_builder { diff --git a/xso/src/lib.rs b/xso/src/lib.rs index 5939b6cde014c8e1afd65fd722471d409345518b..9df1c5548d6638387f6639f82a5950808d152d37 100644 --- a/xso/src/lib.rs +++ b/xso/src/lib.rs @@ -22,7 +22,11 @@ use of this library in parsing XML streams like specified in RFC 6120. // 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 std::io; +#![no_std] + +extern crate alloc; +#[cfg(feature = "std")] +extern crate std; pub mod asxml; pub mod error; @@ -39,7 +43,12 @@ pub mod exports { pub use rxml; } -use std::borrow::Cow; +use alloc::{ + borrow::{Cow, ToOwned}, + boxed::Box, + string::String, + vec::Vec, +}; #[doc(inline)] pub use text::TextCodec; @@ -444,7 +453,8 @@ pub fn try_from_element( unreachable!("minidom::Element did not produce enough events to complete element") } -fn map_nonio_error(r: Result) -> Result { +#[cfg(feature = "std")] +fn map_nonio_error(r: Result) -> Result { match r { Ok(v) => Ok(v), Err(e) => match e.downcast::() { @@ -454,6 +464,7 @@ fn map_nonio_error(r: Result) -> Result } } +#[cfg(feature = "std")] fn read_start_event( r: &mut rxml::Reader, ) -> Result<(rxml::QName, rxml::AttrMap), self::error::Error> { @@ -475,6 +486,7 @@ fn read_start_event( /// Attempt to parse a type implementing [`FromXml`] from a byte buffer /// containing XML data. +#[cfg(feature = "std")] pub fn from_bytes(mut buf: &[u8]) -> Result { let mut reader = rxml::Reader::new(&mut buf); let (name, attrs) = read_start_event(&mut reader)?; @@ -493,23 +505,24 @@ pub fn from_bytes(mut buf: &[u8]) -> Result { Err(self::error::Error::XmlError(rxml::Error::InvalidEof(None))) } +#[cfg(feature = "std")] fn read_start_event_io( r: &mut rxml::Reader, -) -> io::Result<(rxml::QName, rxml::AttrMap)> { +) -> std::io::Result<(rxml::QName, rxml::AttrMap)> { for ev in r { match ev? { rxml::Event::XmlDeclaration(_, rxml::XmlVersion::V1_0) => (), rxml::Event::StartElement(_, name, attrs) => return Ok((name, attrs)), _ => { - return Err(io::Error::new( - io::ErrorKind::InvalidData, + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, self::error::Error::Other("Unexpected event at start of document"), )) } } } - Err(io::Error::new( - io::ErrorKind::InvalidData, + Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, self::error::Error::XmlError(rxml::Error::InvalidEof(Some( rxml::error::ErrorContext::DocumentBegin, ))), @@ -517,29 +530,30 @@ fn read_start_event_io( } /// Attempt to parse a type implementing [`FromXml`] from a reader. -pub fn from_reader(r: R) -> io::Result { +#[cfg(feature = "std")] +pub fn from_reader(r: R) -> std::io::Result { let mut reader = rxml::Reader::new(r); let (name, attrs) = read_start_event_io(&mut reader)?; let mut builder = match T::from_events(name, attrs) { Ok(v) => v, Err(self::error::FromEventsError::Mismatch { .. }) => { return Err(self::error::Error::TypeMismatch) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) } Err(self::error::FromEventsError::Invalid(e)) => { - return Err(e).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) + return Err(e).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) } }; for ev in reader { if let Some(v) = builder .feed(ev?) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))? + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? { return Ok(v); } } - Err(io::Error::new( - io::ErrorKind::UnexpectedEof, + Err(std::io::Error::new( + std::io::ErrorKind::UnexpectedEof, self::error::Error::XmlError(rxml::Error::InvalidEof(None)), )) } diff --git a/xso/src/minidom_compat.rs b/xso/src/minidom_compat.rs index 0c521d0e98a4cfcbe3747d10c9dafec215559221..e2148fd6ea301fb1915268a7adec0a06f6d970e3 100644 --- a/xso/src/minidom_compat.rs +++ b/xso/src/minidom_compat.rs @@ -5,9 +5,13 @@ // 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 alloc::{ + borrow::{Cow, ToOwned}, + boxed::Box, + vec::IntoIter, +}; use core::marker::PhantomData; -use std::borrow::Cow; -use std::vec::IntoIter; use minidom::{Element, Node}; diff --git a/xso/src/rxml_util.rs b/xso/src/rxml_util.rs index f74bd2a745b02d8b1ec96f976c00995a48b72eaa..37500fcf3eb39b7147325a30592d3602a5003da2 100644 --- a/xso/src/rxml_util.rs +++ b/xso/src/rxml_util.rs @@ -6,7 +6,10 @@ //! Utilities which may eventually move upstream to the `rxml` crate. -use std::borrow::Cow; +use alloc::{ + borrow::{Cow, ToOwned}, + string::String, +}; use rxml::{parser::EventMetrics, AttrMap, Event, Namespace, NcName, NcNameStr, XmlVersion}; @@ -283,6 +286,8 @@ impl<'x, I: Iterator, crate::error::Error>>> Iterator for mod tests_minidom { use super::*; + use alloc::{string::ToString, vec, vec::Vec}; + fn events_to_items>(events: I) -> Vec> { let iter = EventToItem { inner: events.map(|ev| Ok(ev)), @@ -423,6 +428,8 @@ mod tests_minidom { mod tests { use super::*; + use alloc::{vec, vec::Vec}; + fn items_to_events<'x, I: IntoIterator>>( items: I, ) -> Result, crate::error::Error> { diff --git a/xso/src/text.rs b/xso/src/text.rs index 0fe4eef14648e8bd73848e9225f857676733a548..871be16e15e080423ea09946de69c70017be484b 100644 --- a/xso/src/text.rs +++ b/xso/src/text.rs @@ -8,7 +8,12 @@ use core::marker::PhantomData; -use std::borrow::Cow; +use alloc::{ + borrow::Cow, + format, + string::{String, ToString}, + vec::Vec, +}; use crate::{error::Error, AsXmlText, FromXmlText};