Skip to main content

use_compound/
compound_name.rs

1use std::fmt;
2
3use crate::CompoundValidationError;
4
5/// A validated primary compound name.
6#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub struct CompoundName(String);
8
9impl CompoundName {
10    /// Creates a compound name.
11    ///
12    /// # Errors
13    ///
14    /// Returns [`CompoundValidationError::EmptyName`] when `name` is empty after trimming.
15    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    /// Returns the compound name text.
25    #[must_use]
26    pub fn as_str(&self) -> &str {
27        &self.0
28    }
29
30    /// Consumes the name and returns the owned text.
31    #[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/// A validated common compound name.
58#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
59pub struct CommonName(String);
60
61impl CommonName {
62    /// Creates a common name.
63    ///
64    /// # Errors
65    ///
66    /// Returns [`CompoundValidationError::EmptyCommonName`] when `name` is empty after trimming.
67    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    /// Returns the common name text.
77    #[must_use]
78    pub fn as_str(&self) -> &str {
79        &self.0
80    }
81
82    /// Consumes the name and returns the owned text.
83    #[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/// A validated systematic compound name.
110#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
111pub struct SystematicName(String);
112
113impl SystematicName {
114    /// Creates a systematic name.
115    ///
116    /// # Errors
117    ///
118    /// Returns [`CompoundValidationError::EmptySystematicName`] when `name` is empty after trimming.
119    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    /// Returns the systematic name text.
129    #[must_use]
130    pub fn as_str(&self) -> &str {
131        &self.0
132    }
133
134    /// Consumes the name and returns the owned text.
135    #[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}