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
16impl Name {
17    /// Create a new [`Name::Named`] using the passed `name`.
18    #[must_use]
19    pub const fn named(name: &'static str) -> Self {
20        Self::Named(Cow::Borrowed(name))
21    }
22
23    /// Create a new [`Name::Generated`] using the passed `name`.
24    #[must_use]
25    pub const fn generated(name: &'static str) -> Self {
26        Self::Generated(Cow::Borrowed(name))
27    }
28
29    /// Create a new [`Name::Named`] using the passed `name`.
30    #[must_use]
31    pub fn new_named<T>(name: T) -> Self
32    where
33        T: Into<Cow<'static, str>>,
34    {
35        Self::Named(name.into())
36    }
37
38    /// Create a new [`Name::Generated`] using the passed `name`.
39    #[must_use]
40    pub fn new_generated<T>(name: T) -> Self
41    where
42        T: Into<Cow<'static, str>>,
43    {
44        Self::Generated(name.into())
45    }
46
47    /// Returns `true` if this is a [`Name::Named`], `false` otherwise.
48    #[must_use]
49    pub fn is_named(&self) -> bool {
50        matches!(self, Self::Named(_))
51    }
52
53    /// Returns `true` if this is a [`Name::Generated`], `false` otherwise.
54    #[must_use]
55    pub fn is_generated(&self) -> bool {
56        matches!(self, Self::Generated(_))
57    }
58
59    /// Returns the value of [`Name::Named`] or [`Name::Generated`].
60    #[must_use]
61    pub fn as_str(&self) -> &str {
62        match self {
63            Self::Named(s) => s,
64            Self::Generated(s) => s,
65        }
66    }
67
68    /// Returns the value of [`Name::Named`] or `None`.
69    #[must_use]
70    pub fn as_named_str(&self) -> Option<&str> {
71        match self {
72            Self::Named(s) => Some(s),
73            Self::Generated(_) => None,
74        }
75    }
76}
77
78impl AsRef<str> for Name {
79    fn as_ref(&self) -> &str {
80        self.as_str()
81    }
82}
83
84impl AsMut<String> for Name {
85    fn as_mut(&mut self) -> &mut String {
86        let x = match self {
87            Self::Named(x) => {
88                *x = Cow::Owned((**x).to_owned());
89
90                x
91            }
92            Self::Generated(x) => {
93                *x = Cow::Owned((**x).to_owned());
94
95                x
96            }
97        };
98
99        let Cow::Owned(x) = x else {
100            unreachable!();
101        };
102
103        x
104    }
105}
106
107impl Display for Name {
108    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
109        match self {
110            Self::Named(x) => write!(f, "{x}"),
111            Self::Generated(x) => write!(f, "{x}"),
112        }
113    }
114}
115
116impl From<String> for Name {
117    fn from(value: String) -> Self {
118        Self::Named(Cow::Owned(value))
119    }
120}
121
122impl From<&'static str> for Name {
123    fn from(value: &'static str) -> Self {
124        Self::Named(Cow::Borrowed(value))
125    }
126}