use core::fmt::{self, Debug};
use std::{borrow::Cow, marker::PhantomData, str::FromStr};
use crate::{
de::{
self, Visitor, XmlCData, XmlComment, XmlDeclaration, XmlDoctype, XmlProcessingInstruction,
XmlText,
},
value::{self, XmlDecl},
Deserialize, Deserializer, Serialize, Serializer,
};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct XmlRoot<T> {
pub decl: Option<XmlDecl>,
pub elements: Vec<XmlRootTop<T>>,
}
impl<T: Serialize> crate::Serialize for XmlRoot<T> {
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as crate::Serializer>::Ok, <S as crate::Serializer>::Error>
where
S: crate::Serializer,
{
let mut __elements = crate::Serializer::serialize_seq(serializer)?;
crate::ser::SerializeSeq::serialize_element(&mut __elements, &self.decl)?;
crate::ser::SerializeSeq::serialize_element(&mut __elements, &self.elements)?;
crate::ser::SerializeSeq::end(__elements)
}
}
impl<'__deserialize, T: Deserialize<'__deserialize> + Debug> Deserialize<'__deserialize>
for XmlRoot<T>
{
fn deserialize<D>(__deserializer: D) -> Result<Self, <D as Deserializer<'__deserialize>>::Error>
where
D: Deserializer<'__deserialize>,
{
struct __XmlRootVisitor<'__visitor, T> {
marker: ::core::marker::PhantomData<XmlRoot<T>>,
lifetime: ::core::marker::PhantomData<&'__visitor ()>,
}
impl<'__visitor, T: Deserialize<'__visitor> + Debug> crate::de::Visitor<'__visitor>
for __XmlRootVisitor<'__visitor, T>
{
type Value = XmlRoot<T>;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(formatter, "struct XmlRoot")
}
fn visit_seq<S>(self, mut sequence: S) -> Result<Self::Value, S::Error>
where
S: de::SeqAccess<'__visitor>,
{
Ok(Self::Value {
decl: crate::de::SeqAccess::next_element::<XmlDecl>(&mut sequence)
.ok()
.flatten(),
elements: crate::de::SeqAccess::next_element_seq::<Vec<XmlRootTop<T>>>(
&mut sequence,
)?
.unwrap_or_default(),
})
}
}
Deserializer::deserialize_seq(
__deserializer,
__XmlRootVisitor {
lifetime: ::core::marker::PhantomData,
marker: ::core::marker::PhantomData,
},
)
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum XmlRootTop<T> {
Value(T),
Comment(value::XmlComment),
PI(value::XmlProcessingInstruction),
Doctype(value::XmlDoctype),
}
impl<T> From<value::XmlComment> for XmlRootTop<T> {
fn from(value: value::XmlComment) -> Self {
XmlRootTop::Comment(value)
}
}
impl<T> From<value::XmlProcessingInstruction> for XmlRootTop<T> {
fn from(value: value::XmlProcessingInstruction) -> Self {
XmlRootTop::PI(value)
}
}
impl<T> From<value::XmlDoctype> for XmlRootTop<T> {
fn from(value: value::XmlDoctype) -> Self {
XmlRootTop::Doctype(value)
}
}
impl<T: Serialize> crate::Serialize for XmlRootTop<T> {
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as crate::Serializer>::Ok, <S as crate::Serializer>::Error>
where
S: crate::Serializer,
{
match self {
XmlRootTop::Value(__v) => crate::Serialize::serialize(&__v, serializer),
XmlRootTop::Comment(__v) => crate::Serialize::serialize(&__v, serializer),
XmlRootTop::PI(__v) => crate::Serialize::serialize(&__v, serializer),
XmlRootTop::Doctype(__v) => crate::Serialize::serialize(&__v, serializer),
}
}
}
impl<'__deserialize, T: Deserialize<'__deserialize>> Deserialize<'__deserialize> for XmlRootTop<T> {
fn deserialize<D>(__deserializer: D) -> Result<Self, <D as Deserializer<'__deserialize>>::Error>
where
D: Deserializer<'__deserialize>,
{
struct __XmlRootTopVisitor<'__visitor, T> {
marker: ::core::marker::PhantomData<XmlRootTop<T>>,
lifetime: ::core::marker::PhantomData<&'__visitor ()>,
}
impl<'__visitor, T: Deserialize<'__visitor>> crate::de::Visitor<'__visitor>
for __XmlRootTopVisitor<'__visitor, T>
{
type Value = XmlRootTop<T>;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(formatter, "enum XmlRootTop")
}
fn visit_seq<S>(
self,
mut __sequence: S,
) -> Result<Self::Value, <S as crate::de::SeqAccess<'__visitor>>::Error>
where
S: crate::de::SeqAccess<'__visitor>,
{
if let ::core::result::Result::Ok(::core::option::Option::Some(_v)) =
crate::de::SeqAccess::next_element::<T>(&mut __sequence)
{
return ::core::result::Result::Ok(XmlRootTop::Value(_v));
}
if let ::core::result::Result::Ok(::core::option::Option::Some(_v)) =
crate::de::SeqAccess::next_element::<value::XmlComment>(&mut __sequence)
{
return ::core::result::Result::Ok(XmlRootTop::Comment(_v));
}
if let ::core::result::Result::Ok(::core::option::Option::Some(_v)) =
crate::de::SeqAccess::next_element::<value::XmlProcessingInstruction>(
&mut __sequence,
)
{
return ::core::result::Result::Ok(XmlRootTop::PI(_v));
}
if let ::core::result::Result::Ok(::core::option::Option::Some(_v)) =
crate::de::SeqAccess::next_element::<value::XmlDoctype>(&mut __sequence)
{
return ::core::result::Result::Ok(XmlRootTop::Doctype(_v));
}
::core::result::Result::Err(crate::de::Error::no_possible_variant("XmlRootTop"))
}
}
Deserializer::deserialize_seq(
__deserializer,
__XmlRootTopVisitor {
lifetime: ::core::marker::PhantomData,
marker: ::core::marker::PhantomData,
},
)
}
}
impl<T> XmlRoot<T> {
pub fn new() -> Self {
Self {
decl: None,
elements: Vec::new(),
}
}
pub fn with_decl<U: Into<XmlDecl>>(mut self, decl: U) -> Self {
let decl: XmlDecl = decl.into();
self.decl = Some(decl);
self
}
pub fn with_element<U: Into<T>>(mut self, element: U) -> Self {
let element: T = element.into();
self.elements.push(XmlRootTop::Value(element));
self
}
pub fn with_elements<U: Into<T>, I: IntoIterator<Item = U>>(mut self, elements: I) -> Self {
self.elements.extend(
elements
.into_iter()
.map(Into::<T>::into)
.map(XmlRootTop::Value),
);
self
}
pub fn with_comment<U: Into<value::XmlComment>>(mut self, comment: U) -> Self {
let comment: value::XmlComment = comment.into();
self.elements.push(comment.into());
self
}
pub fn with_comments<U: Into<value::XmlComment>, I: IntoIterator<Item = U>>(
mut self,
comments: I,
) -> Self {
self.elements.extend(
comments
.into_iter()
.map(Into::<value::XmlComment>::into)
.map(Into::into),
);
self
}
pub fn with_pi<U: Into<value::XmlProcessingInstruction>>(mut self, pi: U) -> Self {
let pi: value::XmlProcessingInstruction = pi.into();
self.elements.push(pi.into());
self
}
pub fn with_pis<U: Into<value::XmlProcessingInstruction>, I: IntoIterator<Item = U>>(
mut self,
pis: I,
) -> Self {
self.elements.extend(
pis.into_iter()
.map(Into::<value::XmlProcessingInstruction>::into)
.map(Into::into),
);
self
}
pub fn with_doctype<U: Into<value::XmlDoctype>>(mut self, doctype: U) -> Self {
let doctype: value::XmlDoctype = doctype.into();
self.elements.push(doctype.into());
self
}
pub fn with_doctypes<U: Into<value::XmlDoctype>, I: IntoIterator<Item = U>>(
mut self,
doctypes: I,
) -> Self {
self.elements.extend(
doctypes
.into_iter()
.map(Into::<value::XmlDoctype>::into)
.map(Into::into),
);
self
}
}
impl<T> Default for XmlRoot<T> {
fn default() -> Self {
Self::new()
}
}
pub struct FromCDataVisitor<T> {
_marker: PhantomData<fn() -> T>,
}
impl<T> Default for FromCDataVisitor<T> {
fn default() -> Self {
Self {
_marker: PhantomData,
}
}
}
impl<'de, T: Deserialize<'de>> Visitor<'de> for FromCDataVisitor<T>
where
T: FromStr,
{
type Value = T;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a string")
}
fn visit_cdata<E, V: XmlCData<'de>>(self, v: V) -> Result<Self::Value, E>
where
E: de::Error,
{
v.as_str().parse().map_err(|_| E::custom("invalid value"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CData<S>(pub S);
impl<'de, S: FromStr + Deserialize<'de>> Deserialize<'de> for CData<S> {
fn deserialize<D: Deserializer<'de>>(reader: D) -> Result<Self, D::Error> {
reader
.deserialize_any(FromCDataVisitor::default())
.map(CData)
}
}
impl<S: AsRef<str>> Serialize for CData<S> {
fn serialize<Ser: Serializer>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error> {
serializer.serialize_cdata(self.0.as_ref())
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Whitespace<'a>(pub std::borrow::Cow<'a, str>);
impl<'de> Deserialize<'de> for Whitespace<'de> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct __Visitor<'v> {
lifetime: ::core::marker::PhantomData<&'v ()>,
}
impl<'v> crate::de::Visitor<'v> for __Visitor<'v> {
type Value = Whitespace<'v>;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
formatter.write_str("ignored any value")
}
fn visit_text<E, V: XmlText<'v>>(self, text: V) -> Result<Self::Value, E>
where
E: de::Error,
{
let text = text.into_string();
if text.trim().is_empty() {
Ok(Whitespace(text))
} else {
Err(E::custom("expected whitespace"))
}
}
}
deserializer.deserialize_any(__Visitor {
lifetime: ::core::marker::PhantomData,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ValueOrWhitespace<'a, T> {
Whitespace(Cow<'a, str>),
Value(T),
}
impl<'de, T: Deserialize<'de>> Deserialize<'de> for ValueOrWhitespace<'de, T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct __Visitor<'v, T> {
marker: ::core::marker::PhantomData<T>,
lifetime: ::core::marker::PhantomData<&'v ()>,
}
impl<'v, T: Deserialize<'v>> crate::de::Visitor<'v> for __Visitor<'v, T> {
type Value = ValueOrWhitespace<'v, T>;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
formatter.write_str("ignored any value")
}
fn visit_seq<S>(self, mut sequence: S) -> Result<Self::Value, S::Error>
where
S: de::SeqAccess<'v>,
{
if let Ok(Some(text)) = sequence.next_element::<Whitespace>() {
Ok(ValueOrWhitespace::Whitespace(text.0))
} else {
sequence
.next_element_seq::<T>()?
.ok_or_else(de::Error::missing_data)
.map(ValueOrWhitespace::Value)
}
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
T::deserialize_seq(crate::types::utils::NoneDeserializer::new())
.map(ValueOrWhitespace::Value)
}
}
deserializer.deserialize_seq(__Visitor {
lifetime: ::core::marker::PhantomData,
marker: ::core::marker::PhantomData,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IgnoredAny;
impl<'de> Deserialize<'de> for IgnoredAny {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct __Visitor<'v> {
marker: ::core::marker::PhantomData<IgnoredAny>,
lifetime: ::core::marker::PhantomData<&'v ()>,
}
impl<'v> crate::de::Visitor<'v> for __Visitor<'v> {
type Value = IgnoredAny;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
formatter.write_str("ignored any value")
}
fn visit_seq<S>(self, _sequence: S) -> Result<Self::Value, S::Error>
where
S: crate::de::SeqAccess<'v>,
{
Ok(IgnoredAny)
}
fn visit_text<E, V: XmlText<'v>>(self, _value: V) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(IgnoredAny)
}
fn visit_cdata<E, V: XmlCData<'v>>(self, _value: V) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(IgnoredAny)
}
fn visit_element<A>(self, _element: A) -> Result<Self::Value, A::Error>
where
A: de::ElementAccess<'v>,
{
Ok(IgnoredAny)
}
fn visit_attribute<A>(self, _attribute: A) -> Result<Self::Value, A::Error>
where
A: de::AttributeAccess<'v>,
{
Ok(IgnoredAny)
}
fn visit_pi<E, V: XmlProcessingInstruction>(self, _pi: V) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(IgnoredAny)
}
fn visit_decl<E, V: XmlDeclaration>(self, _declaration: V) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(IgnoredAny)
}
fn visit_comment<E, V: XmlComment<'v>>(self, _comment: V) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(IgnoredAny)
}
fn visit_doctype<E, V: XmlDoctype<'v>>(self, _doctype: V) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(IgnoredAny)
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(IgnoredAny)
}
}
deserializer.deserialize_any(__Visitor {
lifetime: ::core::marker::PhantomData,
marker: ::core::marker::PhantomData,
})
}
}
pub struct NoneDeserializer<E: de::Error> {
_marker: PhantomData<E>,
}
impl<E: de::Error> NoneDeserializer<E> {
pub fn new() -> Self {
Self {
_marker: PhantomData,
}
}
}
impl<E: de::Error> Default for NoneDeserializer<E> {
fn default() -> Self {
Self::new()
}
}
impl<'de, E: de::Error> Deserializer<'de> for NoneDeserializer<E> {
type Error = E;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_none()
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_none()
}
}