xsd_parser/models/schema/
namespace.rs

1use std::borrow::Cow;
2use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
3use std::ops::Deref;
4
5use crate::models::format_utf8_slice;
6
7/// Represents a XML namespace.
8#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
9pub struct Namespace(pub Cow<'static, [u8]>);
10
11impl Namespace {
12    /// The XML schema namespace
13    pub const XS: Self = Self(Cow::Borrowed(b"http://www.w3.org/2001/XMLSchema"));
14
15    /// The XML schema instance namespace
16    pub const XSI: Self = Self(Cow::Borrowed(b"http://www.w3.org/2001/XMLSchema-instance"));
17
18    /// The XML namespace.
19    pub const XML: Self = Self(Cow::Borrowed(b"http://www.w3.org/XML/1998/namespace"));
20}
21
22impl Namespace {
23    /// Create a new [`Namespace`] instance from the passed `value`.
24    #[must_use]
25    pub fn new<X>(value: X) -> Self
26    where
27        X: Into<Cow<'static, [u8]>>,
28    {
29        Self(value.into())
30    }
31
32    /// Create a new [`Namespace`] instance from the passed `value`.
33    ///
34    /// In contrast to [`new`](Self::new) this is a const function
35    /// and can be used during compile time.
36    #[must_use]
37    pub const fn new_const(value: &'static [u8]) -> Self {
38        Self(Cow::Borrowed(value))
39    }
40}
41
42impl AsRef<[u8]> for Namespace {
43    fn as_ref(&self) -> &[u8] {
44        &self.0
45    }
46}
47
48impl Deref for Namespace {
49    type Target = Cow<'static, [u8]>;
50
51    fn deref(&self) -> &Self::Target {
52        &self.0
53    }
54}
55
56impl<X> From<X> for Namespace
57where
58    X: Into<Cow<'static, [u8]>>,
59{
60    fn from(value: X) -> Self {
61        Self(value.into())
62    }
63}
64
65impl<'x> From<&'x Self> for Namespace {
66    fn from(value: &'x Self) -> Self {
67        value.clone()
68    }
69}
70
71impl Debug for Namespace {
72    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
73        write!(f, "Namespace(b\"")?;
74
75        format_utf8_slice(&self.0, f)?;
76
77        write!(f, "\")")?;
78
79        Ok(())
80    }
81}
82
83impl Display for Namespace {
84    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
85        format_utf8_slice(&self.0, f)
86    }
87}