Skip to main content

xsd_parser/models/
name.rs

1//! Contains the [`Name`] helper type and all related types.
2
3use std::borrow::Cow;
4use std::fmt::{Display, Formatter, Result as FmtResult};
5
6/// Type that represents a name of a XSD element.
7#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
8pub enum Name {
9    /// The name was explicitly set to the given value.
10    Named(Cow<'static, str>),
11
12    /// The name was generated.
13    Generated(Cow<'static, str>),
14}
15
16#[allow(missing_docs)]
17impl Name {
18    pub const ANY: Self = Self::named("any");
19    pub const ANY_TYPE: Self = Self::named("anyType");
20    pub const ANY_SIMPLE_TYPE: Self = Self::named("anySimpleType");
21
22    pub const TYPE: Self = Self::named("type");
23    pub const ANY_ATTRIBUTE: Self = Self::named("any_attribute");
24}
25
26impl Name {
27    /// Create a new [`Name::Named`] using the passed `name`.
28    #[must_use]
29    pub const fn named(name: &'static str) -> Self {
30        Self::Named(Cow::Borrowed(name))
31    }
32
33    /// Create a new [`Name::Generated`] using the passed `name`.
34    #[must_use]
35    pub const fn generated(name: &'static str) -> Self {
36        Self::Generated(Cow::Borrowed(name))
37    }
38
39    /// Create a new [`Name::Named`] using the passed `name`.
40    #[must_use]
41    pub fn new_named<T>(name: T) -> Self
42    where
43        T: Into<Cow<'static, str>>,
44    {
45        Self::Named(name.into())
46    }
47
48    /// Create a new [`Name::Generated`] using the passed `name`.
49    #[must_use]
50    pub fn new_generated<T>(name: T) -> Self
51    where
52        T: Into<Cow<'static, str>>,
53    {
54        Self::Generated(name.into())
55    }
56
57    /// Returns `true` if this is a [`Name::Named`], `false` otherwise.
58    #[must_use]
59    pub fn is_named(&self) -> bool {
60        matches!(self, Self::Named(_))
61    }
62
63    /// Returns `true` if this is a [`Name::Generated`], `false` otherwise.
64    #[must_use]
65    pub fn is_generated(&self) -> bool {
66        matches!(self, Self::Generated(_))
67    }
68
69    /// Returns the value of [`Name::Named`] or [`Name::Generated`].
70    #[must_use]
71    pub fn as_str(&self) -> &str {
72        match self {
73            Self::Named(s) => s,
74            Self::Generated(s) => s,
75        }
76    }
77
78    /// Returns the value of [`Name::Named`] or `None`.
79    #[must_use]
80    pub fn as_named_str(&self) -> Option<&str> {
81        match self {
82            Self::Named(s) => Some(s),
83            Self::Generated(_) => None,
84        }
85    }
86}
87
88impl AsRef<str> for Name {
89    fn as_ref(&self) -> &str {
90        self.as_str()
91    }
92}
93
94impl AsMut<String> for Name {
95    fn as_mut(&mut self) -> &mut String {
96        let x = match self {
97            Self::Named(x) => {
98                *x = Cow::Owned((**x).to_owned());
99
100                x
101            }
102            Self::Generated(x) => {
103                *x = Cow::Owned((**x).to_owned());
104
105                x
106            }
107        };
108
109        let Cow::Owned(x) = x else {
110            unreachable!();
111        };
112
113        x
114    }
115}
116
117impl Display for Name {
118    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
119        match self {
120            Self::Named(x) => write!(f, "{x}"),
121            Self::Generated(x) => write!(f, "{x}"),
122        }
123    }
124}
125
126impl From<String> for Name {
127    fn from(value: String) -> Self {
128        Self::Named(Cow::Owned(value))
129    }
130}
131
132impl From<&'static str> for Name {
133    fn from(value: &'static str) -> Self {
134        Self::Named(Cow::Borrowed(value))
135    }
136}
137
138impl From<Name> for String {
139    fn from(value: Name) -> Self {
140        value.as_str().to_owned()
141    }
142}
143
144impl From<Name> for Cow<'static, str> {
145    fn from(value: Name) -> Self {
146        match value {
147            Name::Named(s) => s,
148            Name::Generated(s) => s,
149        }
150    }
151}