xsd_parser/models/
name.rs1use std::borrow::Cow;
4use std::fmt::{Display, Formatter, Result as FmtResult};
5
6#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
8pub enum Name {
9 Named(Cow<'static, str>),
11
12 Generated(Cow<'static, str>),
14}
15
16impl Name {
17 #[must_use]
19 pub const fn named(name: &'static str) -> Self {
20 Self::Named(Cow::Borrowed(name))
21 }
22
23 #[must_use]
25 pub const fn generated(name: &'static str) -> Self {
26 Self::Generated(Cow::Borrowed(name))
27 }
28
29 #[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 #[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 #[must_use]
49 pub fn is_named(&self) -> bool {
50 matches!(self, Self::Named(_))
51 }
52
53 #[must_use]
55 pub fn is_generated(&self) -> bool {
56 matches!(self, Self::Generated(_))
57 }
58
59 #[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 #[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}