1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, strum :: EnumIter)]
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4pub enum SodiumIsotope {
6 Na18,
8 Na19,
10 Na20,
12 Na21,
14 Na22,
16 Na23,
18 Na24,
20 Na25,
22 Na26,
24 Na27,
26 Na28,
28 Na29,
30 Na30,
32 Na31,
34 Na32,
36 Na33,
38 Na34,
40 Na35,
42 Na36,
44 Na37,
46}
47impl super::RelativeAtomicMass for SodiumIsotope {
48 #[inline]
49 fn relative_atomic_mass(&self) -> f64 {
50 match self {
51 Self::Na18 => 18.02688f64,
52 Self::Na19 => 19.01388f64,
53 Self::Na20 => 20.0073544f64,
54 Self::Na21 => 20.99765469f64,
55 Self::Na22 => 21.99443741f64,
56 Self::Na23 => 22.989769282f64,
57 Self::Na24 => 23.99096295f64,
58 Self::Na25 => 24.989954f64,
59 Self::Na26 => 25.9926346f64,
60 Self::Na27 => 26.9940765f64,
61 Self::Na28 => 27.998939f64,
62 Self::Na29 => 29.0028771f64,
63 Self::Na30 => 30.0090979f64,
64 Self::Na31 => 31.013163f64,
65 Self::Na32 => 32.02019f64,
66 Self::Na33 => 33.02573f64,
67 Self::Na34 => 34.03359f64,
68 Self::Na35 => 35.04062f64,
69 Self::Na36 => 36.04929f64,
70 Self::Na37 => 37.05705f64,
71 }
72 }
73}
74impl super::ElementVariant for SodiumIsotope {
75 #[inline]
76 fn element(&self) -> crate::Element {
77 crate::Element::Na
78 }
79}
80impl super::MassNumber for SodiumIsotope {
81 #[inline]
82 fn mass_number(&self) -> u16 {
83 match self {
84 Self::Na18 => 18u16,
85 Self::Na19 => 19u16,
86 Self::Na20 => 20u16,
87 Self::Na21 => 21u16,
88 Self::Na22 => 22u16,
89 Self::Na23 => 23u16,
90 Self::Na24 => 24u16,
91 Self::Na25 => 25u16,
92 Self::Na26 => 26u16,
93 Self::Na27 => 27u16,
94 Self::Na28 => 28u16,
95 Self::Na29 => 29u16,
96 Self::Na30 => 30u16,
97 Self::Na31 => 31u16,
98 Self::Na32 => 32u16,
99 Self::Na33 => 33u16,
100 Self::Na34 => 34u16,
101 Self::Na35 => 35u16,
102 Self::Na36 => 36u16,
103 Self::Na37 => 37u16,
104 }
105 }
106}
107impl super::IsotopicComposition for SodiumIsotope {
108 #[inline]
109 fn isotopic_composition(&self) -> Option<f64> {
110 match self {
111 Self::Na23 => Some(1f64),
112 _ => None,
113 }
114 }
115}
116impl super::MostAbundantIsotope for SodiumIsotope {
117 fn most_abundant_isotope() -> Self {
118 Self::Na23
119 }
120}
121impl From<SodiumIsotope> for crate::Isotope {
122 fn from(isotope: SodiumIsotope) -> Self {
123 crate::Isotope::Na(isotope)
124 }
125}
126impl From<SodiumIsotope> for crate::Element {
127 fn from(_isotope: SodiumIsotope) -> Self {
128 crate::Element::Na
129 }
130}
131impl TryFrom<u64> for SodiumIsotope {
132 type Error = crate::errors::Error;
133 fn try_from(value: u64) -> Result<Self, Self::Error> {
134 match value {
135 18u64 => Ok(Self::Na18),
136 19u64 => Ok(Self::Na19),
137 20u64 => Ok(Self::Na20),
138 21u64 => Ok(Self::Na21),
139 22u64 => Ok(Self::Na22),
140 23u64 => Ok(Self::Na23),
141 24u64 => Ok(Self::Na24),
142 25u64 => Ok(Self::Na25),
143 26u64 => Ok(Self::Na26),
144 27u64 => Ok(Self::Na27),
145 28u64 => Ok(Self::Na28),
146 29u64 => Ok(Self::Na29),
147 30u64 => Ok(Self::Na30),
148 31u64 => Ok(Self::Na31),
149 32u64 => Ok(Self::Na32),
150 33u64 => Ok(Self::Na33),
151 34u64 => Ok(Self::Na34),
152 35u64 => Ok(Self::Na35),
153 36u64 => Ok(Self::Na36),
154 37u64 => Ok(Self::Na37),
155 _ => Err(crate::errors::Error::Isotope(crate::Element::Na, value)),
156 }
157 }
158}
159impl TryFrom<u8> for SodiumIsotope {
160 type Error = crate::errors::Error;
161 fn try_from(value: u8) -> Result<Self, Self::Error> {
162 Self::try_from(u64::from(value))
163 }
164}
165impl TryFrom<u16> for SodiumIsotope {
166 type Error = crate::errors::Error;
167 fn try_from(value: u16) -> Result<Self, Self::Error> {
168 Self::try_from(u64::from(value))
169 }
170}
171impl TryFrom<u32> for SodiumIsotope {
172 type Error = crate::errors::Error;
173 fn try_from(value: u32) -> Result<Self, Self::Error> {
174 Self::try_from(u64::from(value))
175 }
176}
177impl core::fmt::Display for SodiumIsotope {
178 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
179 match self {
180 Self::Na18 => write!(f, "Na18"),
181 Self::Na19 => write!(f, "Na19"),
182 Self::Na20 => write!(f, "Na20"),
183 Self::Na21 => write!(f, "Na21"),
184 Self::Na22 => write!(f, "Na22"),
185 Self::Na23 => write!(f, "Na23"),
186 Self::Na24 => write!(f, "Na24"),
187 Self::Na25 => write!(f, "Na25"),
188 Self::Na26 => write!(f, "Na26"),
189 Self::Na27 => write!(f, "Na27"),
190 Self::Na28 => write!(f, "Na28"),
191 Self::Na29 => write!(f, "Na29"),
192 Self::Na30 => write!(f, "Na30"),
193 Self::Na31 => write!(f, "Na31"),
194 Self::Na32 => write!(f, "Na32"),
195 Self::Na33 => write!(f, "Na33"),
196 Self::Na34 => write!(f, "Na34"),
197 Self::Na35 => write!(f, "Na35"),
198 Self::Na36 => write!(f, "Na36"),
199 Self::Na37 => write!(f, "Na37"),
200 }
201 }
202}
203#[cfg(test)]
204mod tests {
205 use strum::IntoEnumIterator;
206
207 use super::*;
208 use crate::isotopes::{
209 ElementVariant, IsotopicComposition, MassNumber, MostAbundantIsotope, RelativeAtomicMass,
210 };
211 #[test]
212 fn test_relative_atomic_mass() {
213 for isotope in SodiumIsotope::iter() {
214 let mass = isotope.relative_atomic_mass();
215 assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
216 }
217 }
218 #[test]
219 fn test_element() {
220 for isotope in SodiumIsotope::iter() {
221 let element = isotope.element();
222 assert_eq!(element, crate::Element::Na, "Element should be correct for {isotope:?}");
223 }
224 }
225 #[test]
226 fn test_mass_number() {
227 for isotope in SodiumIsotope::iter() {
228 let mass_number = isotope.mass_number();
229 assert!(
230 mass_number > 0 && mass_number < 300,
231 "Mass number should be reasonable for {isotope:?}"
232 );
233 }
234 }
235 #[test]
236 fn test_isotopic_composition() {
237 for isotope in SodiumIsotope::iter() {
238 let comp = isotope.isotopic_composition();
239 if let Some(c) = comp {
240 assert!(
241 (0.0..=1.0).contains(&c),
242 "Composition should be between 0 and 1 for {isotope:?}"
243 );
244 }
245 }
246 }
247 #[test]
248 fn test_most_abundant() {
249 let most_abundant = SodiumIsotope::most_abundant_isotope();
250 let _ = most_abundant.relative_atomic_mass();
251 }
252 #[test]
253 fn test_from_isotope() {
254 for isotope in SodiumIsotope::iter() {
255 let iso: crate::Isotope = isotope.into();
256 match iso {
257 crate::Isotope::Na(i) => assert_eq!(i, isotope),
258 _ => panic!("Wrong isotope type"),
259 }
260 }
261 }
262 #[test]
263 fn test_from_element() {
264 for isotope in SodiumIsotope::iter() {
265 let elem: crate::Element = isotope.into();
266 assert_eq!(elem, crate::Element::Na);
267 }
268 }
269 #[test]
270 fn test_try_from_mass_number() {
271 for isotope in SodiumIsotope::iter() {
272 let mass = isotope.mass_number();
273 let iso = SodiumIsotope::try_from(mass).unwrap();
274 assert_eq!(iso, isotope);
275 let iso_u32 = SodiumIsotope::try_from(u32::from(mass)).unwrap();
276 assert_eq!(iso_u32, isotope);
277 if let Ok(mass_u8) = u8::try_from(mass) {
278 let iso_u8 = SodiumIsotope::try_from(mass_u8).unwrap();
279 assert_eq!(iso_u8, isotope);
280 }
281 }
282 assert!(SodiumIsotope::try_from(0_u16).is_err());
283 assert!(SodiumIsotope::try_from(1000_u16).is_err());
284 assert!(SodiumIsotope::try_from(0_u32).is_err());
285 assert!(SodiumIsotope::try_from(1000_u32).is_err());
286 assert!(SodiumIsotope::try_from(0_u8).is_err());
287 }
288 #[test]
289 fn test_display() {
290 for isotope in SodiumIsotope::iter() {
291 let s = alloc::format!("{isotope}");
292 assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
293 }
294 }
295}