elements_rs 0.2.6

A comprehensive library for chemical elements and their isotopes with rich metadata
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Isotopes of the element Caesium
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, strum :: EnumIter)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "mem_size", derive(mem_dbg::MemSize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemDbg))]
#[cfg_attr(feature = "mem_size", mem_size(flat))]
/// Isotopes of the element Caesium
pub enum CaesiumIsotope {
    /// Isotope Cs112 of Caesium
    Cs112,
    /// Isotope Cs113 of Caesium
    Cs113,
    /// Isotope Cs114 of Caesium
    Cs114,
    /// Isotope Cs115 of Caesium
    Cs115,
    /// Isotope Cs116 of Caesium
    Cs116,
    /// Isotope Cs117 of Caesium
    Cs117,
    /// Isotope Cs118 of Caesium
    Cs118,
    /// Isotope Cs119 of Caesium
    Cs119,
    /// Isotope Cs120 of Caesium
    Cs120,
    /// Isotope Cs121 of Caesium
    Cs121,
    /// Isotope Cs122 of Caesium
    Cs122,
    /// Isotope Cs123 of Caesium
    Cs123,
    /// Isotope Cs124 of Caesium
    Cs124,
    /// Isotope Cs125 of Caesium
    Cs125,
    /// Isotope Cs126 of Caesium
    Cs126,
    /// Isotope Cs127 of Caesium
    Cs127,
    /// Isotope Cs128 of Caesium
    Cs128,
    /// Isotope Cs129 of Caesium
    Cs129,
    /// Isotope Cs130 of Caesium
    Cs130,
    /// Isotope Cs131 of Caesium
    Cs131,
    /// Isotope Cs132 of Caesium
    Cs132,
    /// Isotope Cs133 of Caesium
    Cs133,
    /// Isotope Cs134 of Caesium
    Cs134,
    /// Isotope Cs135 of Caesium
    Cs135,
    /// Isotope Cs136 of Caesium
    Cs136,
    /// Isotope Cs137 of Caesium
    Cs137,
    /// Isotope Cs138 of Caesium
    Cs138,
    /// Isotope Cs139 of Caesium
    Cs139,
    /// Isotope Cs140 of Caesium
    Cs140,
    /// Isotope Cs141 of Caesium
    Cs141,
    /// Isotope Cs142 of Caesium
    Cs142,
    /// Isotope Cs143 of Caesium
    Cs143,
    /// Isotope Cs144 of Caesium
    Cs144,
    /// Isotope Cs145 of Caesium
    Cs145,
    /// Isotope Cs146 of Caesium
    Cs146,
    /// Isotope Cs147 of Caesium
    Cs147,
    /// Isotope Cs148 of Caesium
    Cs148,
    /// Isotope Cs149 of Caesium
    Cs149,
    /// Isotope Cs150 of Caesium
    Cs150,
    /// Isotope Cs151 of Caesium
    Cs151,
    /// Isotope Cs152 of Caesium
    Cs152,
}
impl super::RelativeAtomicMass for CaesiumIsotope {
    #[inline]
    fn relative_atomic_mass(&self) -> f64 {
        match self {
            Self::Cs112 => 111.950309f64,
            Self::Cs113 => 112.9444291f64,
            Self::Cs114 => 113.941296f64,
            Self::Cs115 => 114.93591f64,
            Self::Cs116 => 115.93337f64,
            Self::Cs117 => 116.928617f64,
            Self::Cs118 => 117.92656f64,
            Self::Cs119 => 118.922377f64,
            Self::Cs120 => 119.920677f64,
            Self::Cs121 => 120.917227f64,
            Self::Cs122 => 121.916108f64,
            Self::Cs123 => 122.912996f64,
            Self::Cs124 => 123.9122578f64,
            Self::Cs125 => 124.909728f64,
            Self::Cs126 => 125.909446f64,
            Self::Cs127 => 126.9074174f64,
            Self::Cs128 => 127.9077487f64,
            Self::Cs129 => 128.9060657f64,
            Self::Cs130 => 129.9067093f64,
            Self::Cs131 => 130.9054649f64,
            Self::Cs132 => 131.9064339f64,
            Self::Cs133 => 132.905451961f64,
            Self::Cs134 => 133.906718503f64,
            Self::Cs135 => 134.905977f64,
            Self::Cs136 => 135.9073114f64,
            Self::Cs137 => 136.90708923f64,
            Self::Cs138 => 137.9110171f64,
            Self::Cs139 => 138.9133638f64,
            Self::Cs140 => 139.9172831f64,
            Self::Cs141 => 140.9200455f64,
            Self::Cs142 => 141.924296f64,
            Self::Cs143 => 142.927349f64,
            Self::Cs144 => 143.932076f64,
            Self::Cs145 => 144.935527f64,
            Self::Cs146 => 145.940344f64,
            Self::Cs147 => 146.944156f64,
            Self::Cs148 => 147.94923f64,
            Self::Cs149 => 148.95302f64,
            Self::Cs150 => 149.95833f64,
            Self::Cs151 => 150.96258f64,
            Self::Cs152 => 151.968728f64,
        }
    }
}
impl super::ElementVariant for CaesiumIsotope {
    #[inline]
    fn element(&self) -> crate::Element {
        crate::Element::Cs
    }
}
impl super::MassNumber for CaesiumIsotope {
    #[inline]
    fn mass_number(&self) -> u16 {
        match self {
            Self::Cs112 => 112u16,
            Self::Cs113 => 113u16,
            Self::Cs114 => 114u16,
            Self::Cs115 => 115u16,
            Self::Cs116 => 116u16,
            Self::Cs117 => 117u16,
            Self::Cs118 => 118u16,
            Self::Cs119 => 119u16,
            Self::Cs120 => 120u16,
            Self::Cs121 => 121u16,
            Self::Cs122 => 122u16,
            Self::Cs123 => 123u16,
            Self::Cs124 => 124u16,
            Self::Cs125 => 125u16,
            Self::Cs126 => 126u16,
            Self::Cs127 => 127u16,
            Self::Cs128 => 128u16,
            Self::Cs129 => 129u16,
            Self::Cs130 => 130u16,
            Self::Cs131 => 131u16,
            Self::Cs132 => 132u16,
            Self::Cs133 => 133u16,
            Self::Cs134 => 134u16,
            Self::Cs135 => 135u16,
            Self::Cs136 => 136u16,
            Self::Cs137 => 137u16,
            Self::Cs138 => 138u16,
            Self::Cs139 => 139u16,
            Self::Cs140 => 140u16,
            Self::Cs141 => 141u16,
            Self::Cs142 => 142u16,
            Self::Cs143 => 143u16,
            Self::Cs144 => 144u16,
            Self::Cs145 => 145u16,
            Self::Cs146 => 146u16,
            Self::Cs147 => 147u16,
            Self::Cs148 => 148u16,
            Self::Cs149 => 149u16,
            Self::Cs150 => 150u16,
            Self::Cs151 => 151u16,
            Self::Cs152 => 152u16,
        }
    }
}
impl super::IsotopicComposition for CaesiumIsotope {
    #[inline]
    fn isotopic_composition(&self) -> Option<f64> {
        match self {
            Self::Cs133 => Some(1f64),
            _ => None,
        }
    }
}
impl super::MostAbundantIsotope for CaesiumIsotope {
    fn most_abundant_isotope() -> Self {
        Self::Cs133
    }
}
impl From<CaesiumIsotope> for crate::Isotope {
    fn from(isotope: CaesiumIsotope) -> Self {
        crate::Isotope::Cs(isotope)
    }
}
impl From<CaesiumIsotope> for crate::Element {
    fn from(_isotope: CaesiumIsotope) -> Self {
        crate::Element::Cs
    }
}
impl TryFrom<u64> for CaesiumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        match value {
            112u64 => Ok(Self::Cs112),
            113u64 => Ok(Self::Cs113),
            114u64 => Ok(Self::Cs114),
            115u64 => Ok(Self::Cs115),
            116u64 => Ok(Self::Cs116),
            117u64 => Ok(Self::Cs117),
            118u64 => Ok(Self::Cs118),
            119u64 => Ok(Self::Cs119),
            120u64 => Ok(Self::Cs120),
            121u64 => Ok(Self::Cs121),
            122u64 => Ok(Self::Cs122),
            123u64 => Ok(Self::Cs123),
            124u64 => Ok(Self::Cs124),
            125u64 => Ok(Self::Cs125),
            126u64 => Ok(Self::Cs126),
            127u64 => Ok(Self::Cs127),
            128u64 => Ok(Self::Cs128),
            129u64 => Ok(Self::Cs129),
            130u64 => Ok(Self::Cs130),
            131u64 => Ok(Self::Cs131),
            132u64 => Ok(Self::Cs132),
            133u64 => Ok(Self::Cs133),
            134u64 => Ok(Self::Cs134),
            135u64 => Ok(Self::Cs135),
            136u64 => Ok(Self::Cs136),
            137u64 => Ok(Self::Cs137),
            138u64 => Ok(Self::Cs138),
            139u64 => Ok(Self::Cs139),
            140u64 => Ok(Self::Cs140),
            141u64 => Ok(Self::Cs141),
            142u64 => Ok(Self::Cs142),
            143u64 => Ok(Self::Cs143),
            144u64 => Ok(Self::Cs144),
            145u64 => Ok(Self::Cs145),
            146u64 => Ok(Self::Cs146),
            147u64 => Ok(Self::Cs147),
            148u64 => Ok(Self::Cs148),
            149u64 => Ok(Self::Cs149),
            150u64 => Ok(Self::Cs150),
            151u64 => Ok(Self::Cs151),
            152u64 => Ok(Self::Cs152),
            _ => Err(crate::errors::Error::Isotope(crate::Element::Cs, value)),
        }
    }
}
impl TryFrom<u8> for CaesiumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u16> for CaesiumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u16) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u32> for CaesiumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u32) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl core::fmt::Display for CaesiumIsotope {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Cs112 => write!(f, "Cs112"),
            Self::Cs113 => write!(f, "Cs113"),
            Self::Cs114 => write!(f, "Cs114"),
            Self::Cs115 => write!(f, "Cs115"),
            Self::Cs116 => write!(f, "Cs116"),
            Self::Cs117 => write!(f, "Cs117"),
            Self::Cs118 => write!(f, "Cs118"),
            Self::Cs119 => write!(f, "Cs119"),
            Self::Cs120 => write!(f, "Cs120"),
            Self::Cs121 => write!(f, "Cs121"),
            Self::Cs122 => write!(f, "Cs122"),
            Self::Cs123 => write!(f, "Cs123"),
            Self::Cs124 => write!(f, "Cs124"),
            Self::Cs125 => write!(f, "Cs125"),
            Self::Cs126 => write!(f, "Cs126"),
            Self::Cs127 => write!(f, "Cs127"),
            Self::Cs128 => write!(f, "Cs128"),
            Self::Cs129 => write!(f, "Cs129"),
            Self::Cs130 => write!(f, "Cs130"),
            Self::Cs131 => write!(f, "Cs131"),
            Self::Cs132 => write!(f, "Cs132"),
            Self::Cs133 => write!(f, "Cs133"),
            Self::Cs134 => write!(f, "Cs134"),
            Self::Cs135 => write!(f, "Cs135"),
            Self::Cs136 => write!(f, "Cs136"),
            Self::Cs137 => write!(f, "Cs137"),
            Self::Cs138 => write!(f, "Cs138"),
            Self::Cs139 => write!(f, "Cs139"),
            Self::Cs140 => write!(f, "Cs140"),
            Self::Cs141 => write!(f, "Cs141"),
            Self::Cs142 => write!(f, "Cs142"),
            Self::Cs143 => write!(f, "Cs143"),
            Self::Cs144 => write!(f, "Cs144"),
            Self::Cs145 => write!(f, "Cs145"),
            Self::Cs146 => write!(f, "Cs146"),
            Self::Cs147 => write!(f, "Cs147"),
            Self::Cs148 => write!(f, "Cs148"),
            Self::Cs149 => write!(f, "Cs149"),
            Self::Cs150 => write!(f, "Cs150"),
            Self::Cs151 => write!(f, "Cs151"),
            Self::Cs152 => write!(f, "Cs152"),
        }
    }
}
#[cfg(test)]
mod tests {
    use strum::IntoEnumIterator;

    use super::*;
    use crate::isotopes::{
        ElementVariant, IsotopicComposition, MassNumber, MostAbundantIsotope, RelativeAtomicMass,
    };
    #[test]
    fn test_relative_atomic_mass() {
        for isotope in CaesiumIsotope::iter() {
            let mass = isotope.relative_atomic_mass();
            assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
        }
    }
    #[test]
    fn test_element() {
        for isotope in CaesiumIsotope::iter() {
            let element = isotope.element();
            assert_eq!(element, crate::Element::Cs, "Element should be correct for {isotope:?}");
        }
    }
    #[test]
    fn test_mass_number() {
        for isotope in CaesiumIsotope::iter() {
            let mass_number = isotope.mass_number();
            assert!(
                mass_number > 0 && mass_number < 300,
                "Mass number should be reasonable for {isotope:?}"
            );
        }
    }
    #[test]
    fn test_isotopic_composition() {
        for isotope in CaesiumIsotope::iter() {
            let comp = isotope.isotopic_composition();
            if let Some(c) = comp {
                assert!(
                    (0.0..=1.0).contains(&c),
                    "Composition should be between 0 and 1 for {isotope:?}"
                );
            }
        }
    }
    #[test]
    fn test_most_abundant() {
        let most_abundant = CaesiumIsotope::most_abundant_isotope();
        let _ = most_abundant.relative_atomic_mass();
    }
    #[test]
    fn test_from_isotope() {
        for isotope in CaesiumIsotope::iter() {
            let iso: crate::Isotope = isotope.into();
            match iso {
                crate::Isotope::Cs(i) => assert_eq!(i, isotope),
                _ => panic!("Wrong isotope type"),
            }
        }
    }
    #[test]
    fn test_from_element() {
        for isotope in CaesiumIsotope::iter() {
            let elem: crate::Element = isotope.into();
            assert_eq!(elem, crate::Element::Cs);
        }
    }
    #[test]
    fn test_try_from_mass_number() {
        for isotope in CaesiumIsotope::iter() {
            let mass = isotope.mass_number();
            let iso = CaesiumIsotope::try_from(mass).unwrap();
            assert_eq!(iso, isotope);
            let iso_u32 = CaesiumIsotope::try_from(u32::from(mass)).unwrap();
            assert_eq!(iso_u32, isotope);
            if let Ok(mass_u8) = u8::try_from(mass) {
                let iso_u8 = CaesiumIsotope::try_from(mass_u8).unwrap();
                assert_eq!(iso_u8, isotope);
            }
        }
        assert!(CaesiumIsotope::try_from(0_u16).is_err());
        assert!(CaesiumIsotope::try_from(1000_u16).is_err());
        assert!(CaesiumIsotope::try_from(0_u32).is_err());
        assert!(CaesiumIsotope::try_from(1000_u32).is_err());
        assert!(CaesiumIsotope::try_from(0_u8).is_err());
    }
    #[test]
    fn test_display() {
        for isotope in CaesiumIsotope::iter() {
            let s = alloc::format!("{isotope}");
            assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
        }
    }
}