xsd_parser/schema/
qname.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::borrow::Cow;

use quick_xml::name::{QName as QuickXmlQName, ResolveResult};

use crate::quick_xml::{DeserializeBytes, Error, XmlReader};

use super::Namespace;

/// Type that represents a a resolved [`QName`].
///
/// The namespace of this [`QName`] was resolved during deserialization.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct QName {
    raw: Vec<u8>,
    index: Option<usize>,
    ns: Option<Namespace>,
}

impl QName {
    /// Create a new [`QName`] instance from the passed `reader` and `raw` data.
    pub fn from_reader<R>(reader: &R, raw: &[u8]) -> Self
    where
        R: XmlReader,
    {
        let index = raw.iter().position(|x| *x == b':');
        let ns = match reader.resolve(QuickXmlQName(raw), true).0 {
            ResolveResult::Unbound | ResolveResult::Unknown(_) => None,
            ResolveResult::Bound(ns) => Some(Namespace(Cow::Owned(ns.0.to_owned()))),
        };
        let raw = raw.to_owned();

        Self { raw, index, ns }
    }

    /// Get the namespace of the [`QName`].
    #[must_use]
    pub fn namespace(&self) -> Option<&Namespace> {
        self.ns.as_ref()
    }

    /// Get the prefix of the [`QName`].
    #[must_use]
    pub fn prefix(&self) -> Option<&[u8]> {
        let index = self.index?;

        Some(&self.raw[0..index])
    }

    /// Get the local name of the [`QName`].
    #[must_use]
    pub fn local_name(&self) -> &[u8] {
        let index = self.index.map(|index| index + 1).unwrap_or_default();

        &self.raw[index..]
    }
}

impl DeserializeBytes for QName {
    fn deserialize_bytes<R: XmlReader>(reader: &R, bytes: &[u8]) -> Result<Self, Error> {
        Ok(Self::from_reader(reader, bytes))
    }
}