use_compound/
compound_name.rs1use std::fmt;
2
3use crate::CompoundValidationError;
4
5#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub struct CompoundName(String);
8
9impl CompoundName {
10 pub fn new(name: &str) -> Result<Self, CompoundValidationError> {
16 let trimmed = name.trim();
17 if trimmed.is_empty() {
18 Err(CompoundValidationError::EmptyName)
19 } else {
20 Ok(Self(trimmed.to_owned()))
21 }
22 }
23
24 #[must_use]
26 pub fn as_str(&self) -> &str {
27 &self.0
28 }
29
30 #[must_use]
32 pub fn into_string(self) -> String {
33 self.0
34 }
35}
36
37impl AsRef<str> for CompoundName {
38 fn as_ref(&self) -> &str {
39 self.as_str()
40 }
41}
42
43impl TryFrom<&str> for CompoundName {
44 type Error = CompoundValidationError;
45
46 fn try_from(value: &str) -> Result<Self, Self::Error> {
47 Self::new(value)
48 }
49}
50
51impl fmt::Display for CompoundName {
52 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
53 formatter.write_str(self.as_str())
54 }
55}
56
57#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
59pub struct CommonName(String);
60
61impl CommonName {
62 pub fn new(name: &str) -> Result<Self, CompoundValidationError> {
68 let trimmed = name.trim();
69 if trimmed.is_empty() {
70 Err(CompoundValidationError::EmptyCommonName)
71 } else {
72 Ok(Self(trimmed.to_owned()))
73 }
74 }
75
76 #[must_use]
78 pub fn as_str(&self) -> &str {
79 &self.0
80 }
81
82 #[must_use]
84 pub fn into_string(self) -> String {
85 self.0
86 }
87}
88
89impl AsRef<str> for CommonName {
90 fn as_ref(&self) -> &str {
91 self.as_str()
92 }
93}
94
95impl TryFrom<&str> for CommonName {
96 type Error = CompoundValidationError;
97
98 fn try_from(value: &str) -> Result<Self, Self::Error> {
99 Self::new(value)
100 }
101}
102
103impl fmt::Display for CommonName {
104 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
105 formatter.write_str(self.as_str())
106 }
107}
108
109#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
111pub struct SystematicName(String);
112
113impl SystematicName {
114 pub fn new(name: &str) -> Result<Self, CompoundValidationError> {
120 let trimmed = name.trim();
121 if trimmed.is_empty() {
122 Err(CompoundValidationError::EmptySystematicName)
123 } else {
124 Ok(Self(trimmed.to_owned()))
125 }
126 }
127
128 #[must_use]
130 pub fn as_str(&self) -> &str {
131 &self.0
132 }
133
134 #[must_use]
136 pub fn into_string(self) -> String {
137 self.0
138 }
139}
140
141impl AsRef<str> for SystematicName {
142 fn as_ref(&self) -> &str {
143 self.as_str()
144 }
145}
146
147impl TryFrom<&str> for SystematicName {
148 type Error = CompoundValidationError;
149
150 fn try_from(value: &str) -> Result<Self, Self::Error> {
151 Self::new(value)
152 }
153}
154
155impl fmt::Display for SystematicName {
156 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
157 formatter.write_str(self.as_str())
158 }
159}