xmlity_quick_xml/
lib.rs

1#![warn(missing_docs)]
2//! # XMLity Quick XML
3//!
4//! This crate contains a reference implementation of the `xmlity` crate using the `quick-xml` crate. It is the intention to keep this crate up to date with the latest version of `quick-xml` and `xmlity`.
5#[cfg(doctest)]
6#[doc = include_str!("../../README.md")]
7struct _RootReadMeDocTests;
8
9#[cfg(doctest)]
10#[doc = include_str!("../README.md")]
11struct _ReadMeDocTests;
12
13use ::quick_xml::name::ResolveResult;
14use core::str;
15
16use xmlity::{LocalName, Prefix, QName, XmlNamespace};
17
18/// Includes the deserializer for the `quick-xml` crate.
19pub mod de;
20/// Includes the serializer for the `quick-xml` crate.
21pub mod ser;
22
23pub use de::{from_str, Deserializer};
24use quick_xml::name::{LocalName as QuickLocalName, Prefix as QuickPrefix, QName as QuickName};
25pub use ser::{to_string, to_string_pretty, Serializer};
26
27trait HasQuickXmlAlternative {
28    type QuickXmlAlternative;
29
30    fn from_quick_xml(quick_xml: Self::QuickXmlAlternative) -> Self;
31}
32
33impl<'a> HasQuickXmlAlternative for QName<'a> {
34    type QuickXmlAlternative = QuickName<'a>;
35
36    fn from_quick_xml(quick_xml: Self::QuickXmlAlternative) -> Self {
37        QName::new(
38            quick_xml.prefix().map(Prefix::from_quick_xml),
39            LocalName::from_quick_xml(quick_xml.local_name()),
40        )
41    }
42}
43
44impl<'a> HasQuickXmlAlternative for Prefix<'a> {
45    type QuickXmlAlternative = QuickPrefix<'a>;
46    fn from_quick_xml(quick_xml: Self::QuickXmlAlternative) -> Self {
47        Self::new(str::from_utf8(quick_xml.into_inner()).expect("prefix should be valid utf8"))
48            .expect("A quick xml prefix should be valid")
49    }
50}
51
52impl<'a> HasQuickXmlAlternative for LocalName<'a> {
53    type QuickXmlAlternative = QuickLocalName<'a>;
54    fn from_quick_xml(quick_xml: Self::QuickXmlAlternative) -> Self {
55        Self::new(str::from_utf8(quick_xml.into_inner()).expect("local name should be valid utf8"))
56            .expect("A quick xml local name should be valid")
57    }
58}
59
60struct OwnedQuickName(Vec<u8>);
61
62impl OwnedQuickName {
63    pub fn new(name: &QName<'_>) -> Self {
64        Self(name.to_string().into_bytes())
65    }
66
67    pub fn as_ref(&self) -> QuickName<'_> {
68        QuickName(&self.0[..])
69    }
70}
71
72fn xml_namespace_from_resolve_result(value: ResolveResult<'_>) -> Option<XmlNamespace<'_>> {
73    match value {
74        ResolveResult::Bound(namespace) => Some(
75            XmlNamespace::new(str::from_utf8(namespace.0).expect("namespace should be valid utf8"))
76                .unwrap(),
77        ),
78        _ => None,
79    }
80}
81
82/// An XML namespace declaration/singular mapping from a prefix to a namespace.
83#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
84struct XmlnsDeclaration<'a> {
85    pub prefix: Prefix<'a>,
86    pub namespace: XmlNamespace<'a>,
87}
88
89impl<'a> XmlnsDeclaration<'a> {
90    pub fn new(prefix: Prefix<'a>, namespace: XmlNamespace<'a>) -> Self {
91        Self { prefix, namespace }
92    }
93
94    /// Returns the QName for the XML namespace declaration.
95    pub fn xmlns_qname(prefix: Prefix<'_>) -> QName<'_> {
96        if prefix.is_default() {
97            QName::new(
98                None,
99                LocalName::new("xmlns").expect("xmlns is a valid local name"),
100            )
101        } else {
102            QName::new(
103                Some(Prefix::new("xmlns").expect("xmlns is a valid prefix")),
104                LocalName::from(prefix),
105            )
106        }
107    }
108}