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
426
427
428
429
430
431
//! Isotopes of the element Barium
#[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 Barium
pub enum BariumIsotope {
    /// Isotope Ba114 of Barium
    Ba114,
    /// Isotope Ba115 of Barium
    Ba115,
    /// Isotope Ba116 of Barium
    Ba116,
    /// Isotope Ba117 of Barium
    Ba117,
    /// Isotope Ba118 of Barium
    Ba118,
    /// Isotope Ba119 of Barium
    Ba119,
    /// Isotope Ba120 of Barium
    Ba120,
    /// Isotope Ba121 of Barium
    Ba121,
    /// Isotope Ba122 of Barium
    Ba122,
    /// Isotope Ba123 of Barium
    Ba123,
    /// Isotope Ba124 of Barium
    Ba124,
    /// Isotope Ba125 of Barium
    Ba125,
    /// Isotope Ba126 of Barium
    Ba126,
    /// Isotope Ba127 of Barium
    Ba127,
    /// Isotope Ba128 of Barium
    Ba128,
    /// Isotope Ba129 of Barium
    Ba129,
    /// Isotope Ba130 of Barium
    Ba130,
    /// Isotope Ba131 of Barium
    Ba131,
    /// Isotope Ba132 of Barium
    Ba132,
    /// Isotope Ba133 of Barium
    Ba133,
    /// Isotope Ba134 of Barium
    Ba134,
    /// Isotope Ba135 of Barium
    Ba135,
    /// Isotope Ba136 of Barium
    Ba136,
    /// Isotope Ba137 of Barium
    Ba137,
    /// Isotope Ba138 of Barium
    Ba138,
    /// Isotope Ba139 of Barium
    Ba139,
    /// Isotope Ba140 of Barium
    Ba140,
    /// Isotope Ba141 of Barium
    Ba141,
    /// Isotope Ba142 of Barium
    Ba142,
    /// Isotope Ba143 of Barium
    Ba143,
    /// Isotope Ba144 of Barium
    Ba144,
    /// Isotope Ba145 of Barium
    Ba145,
    /// Isotope Ba146 of Barium
    Ba146,
    /// Isotope Ba147 of Barium
    Ba147,
    /// Isotope Ba148 of Barium
    Ba148,
    /// Isotope Ba149 of Barium
    Ba149,
    /// Isotope Ba150 of Barium
    Ba150,
    /// Isotope Ba151 of Barium
    Ba151,
    /// Isotope Ba152 of Barium
    Ba152,
    /// Isotope Ba153 of Barium
    Ba153,
    /// Isotope Ba154 of Barium
    Ba154,
}
impl super::RelativeAtomicMass for BariumIsotope {
    #[inline]
    fn relative_atomic_mass(&self) -> f64 {
        match self {
            Self::Ba114 => 113.95066f64,
            Self::Ba115 => 114.94737f64,
            Self::Ba116 => 115.94128f64,
            Self::Ba117 => 116.93814f64,
            Self::Ba118 => 117.93306f64,
            Self::Ba119 => 118.93066f64,
            Self::Ba120 => 119.92605f64,
            Self::Ba121 => 120.92405f64,
            Self::Ba122 => 121.919904f64,
            Self::Ba123 => 122.918781f64,
            Self::Ba124 => 123.915094f64,
            Self::Ba125 => 124.914472f64,
            Self::Ba126 => 125.91125f64,
            Self::Ba127 => 126.911091f64,
            Self::Ba128 => 127.908342f64,
            Self::Ba129 => 128.908681f64,
            Self::Ba130 => 129.9063207f64,
            Self::Ba131 => 130.906941f64,
            Self::Ba132 => 131.9050611f64,
            Self::Ba133 => 132.9060074f64,
            Self::Ba134 => 133.90450818f64,
            Self::Ba135 => 134.90568838f64,
            Self::Ba136 => 135.90457573f64,
            Self::Ba137 => 136.90582714f64,
            Self::Ba138 => 137.905247f64,
            Self::Ba139 => 138.9088411f64,
            Self::Ba140 => 139.9106057f64,
            Self::Ba141 => 140.9144033f64,
            Self::Ba142 => 141.9164324f64,
            Self::Ba143 => 142.9206253f64,
            Self::Ba144 => 143.9229549f64,
            Self::Ba145 => 144.9275184f64,
            Self::Ba146 => 145.930284f64,
            Self::Ba147 => 146.935304f64,
            Self::Ba148 => 147.938171f64,
            Self::Ba149 => 148.94308f64,
            Self::Ba150 => 149.94605f64,
            Self::Ba151 => 150.95127f64,
            Self::Ba152 => 151.95481f64,
            Self::Ba153 => 152.96036f64,
            Self::Ba154 => 153.964659f64,
        }
    }
}
impl super::ElementVariant for BariumIsotope {
    #[inline]
    fn element(&self) -> crate::Element {
        crate::Element::Ba
    }
}
impl super::MassNumber for BariumIsotope {
    #[inline]
    fn mass_number(&self) -> u16 {
        match self {
            Self::Ba114 => 114u16,
            Self::Ba115 => 115u16,
            Self::Ba116 => 116u16,
            Self::Ba117 => 117u16,
            Self::Ba118 => 118u16,
            Self::Ba119 => 119u16,
            Self::Ba120 => 120u16,
            Self::Ba121 => 121u16,
            Self::Ba122 => 122u16,
            Self::Ba123 => 123u16,
            Self::Ba124 => 124u16,
            Self::Ba125 => 125u16,
            Self::Ba126 => 126u16,
            Self::Ba127 => 127u16,
            Self::Ba128 => 128u16,
            Self::Ba129 => 129u16,
            Self::Ba130 => 130u16,
            Self::Ba131 => 131u16,
            Self::Ba132 => 132u16,
            Self::Ba133 => 133u16,
            Self::Ba134 => 134u16,
            Self::Ba135 => 135u16,
            Self::Ba136 => 136u16,
            Self::Ba137 => 137u16,
            Self::Ba138 => 138u16,
            Self::Ba139 => 139u16,
            Self::Ba140 => 140u16,
            Self::Ba141 => 141u16,
            Self::Ba142 => 142u16,
            Self::Ba143 => 143u16,
            Self::Ba144 => 144u16,
            Self::Ba145 => 145u16,
            Self::Ba146 => 146u16,
            Self::Ba147 => 147u16,
            Self::Ba148 => 148u16,
            Self::Ba149 => 149u16,
            Self::Ba150 => 150u16,
            Self::Ba151 => 151u16,
            Self::Ba152 => 152u16,
            Self::Ba153 => 153u16,
            Self::Ba154 => 154u16,
        }
    }
}
impl super::IsotopicComposition for BariumIsotope {
    #[inline]
    fn isotopic_composition(&self) -> Option<f64> {
        match self {
            Self::Ba130 => Some(0.00106f64),
            Self::Ba132 => Some(0.00101f64),
            Self::Ba134 => Some(0.02417f64),
            Self::Ba135 => Some(0.06592f64),
            Self::Ba136 => Some(0.07854f64),
            Self::Ba137 => Some(0.11232f64),
            Self::Ba138 => Some(0.71698f64),
            _ => None,
        }
    }
}
impl super::MostAbundantIsotope for BariumIsotope {
    fn most_abundant_isotope() -> Self {
        Self::Ba138
    }
}
impl From<BariumIsotope> for crate::Isotope {
    fn from(isotope: BariumIsotope) -> Self {
        crate::Isotope::Ba(isotope)
    }
}
impl From<BariumIsotope> for crate::Element {
    fn from(_isotope: BariumIsotope) -> Self {
        crate::Element::Ba
    }
}
impl TryFrom<u64> for BariumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        match value {
            114u64 => Ok(Self::Ba114),
            115u64 => Ok(Self::Ba115),
            116u64 => Ok(Self::Ba116),
            117u64 => Ok(Self::Ba117),
            118u64 => Ok(Self::Ba118),
            119u64 => Ok(Self::Ba119),
            120u64 => Ok(Self::Ba120),
            121u64 => Ok(Self::Ba121),
            122u64 => Ok(Self::Ba122),
            123u64 => Ok(Self::Ba123),
            124u64 => Ok(Self::Ba124),
            125u64 => Ok(Self::Ba125),
            126u64 => Ok(Self::Ba126),
            127u64 => Ok(Self::Ba127),
            128u64 => Ok(Self::Ba128),
            129u64 => Ok(Self::Ba129),
            130u64 => Ok(Self::Ba130),
            131u64 => Ok(Self::Ba131),
            132u64 => Ok(Self::Ba132),
            133u64 => Ok(Self::Ba133),
            134u64 => Ok(Self::Ba134),
            135u64 => Ok(Self::Ba135),
            136u64 => Ok(Self::Ba136),
            137u64 => Ok(Self::Ba137),
            138u64 => Ok(Self::Ba138),
            139u64 => Ok(Self::Ba139),
            140u64 => Ok(Self::Ba140),
            141u64 => Ok(Self::Ba141),
            142u64 => Ok(Self::Ba142),
            143u64 => Ok(Self::Ba143),
            144u64 => Ok(Self::Ba144),
            145u64 => Ok(Self::Ba145),
            146u64 => Ok(Self::Ba146),
            147u64 => Ok(Self::Ba147),
            148u64 => Ok(Self::Ba148),
            149u64 => Ok(Self::Ba149),
            150u64 => Ok(Self::Ba150),
            151u64 => Ok(Self::Ba151),
            152u64 => Ok(Self::Ba152),
            153u64 => Ok(Self::Ba153),
            154u64 => Ok(Self::Ba154),
            _ => Err(crate::errors::Error::Isotope(crate::Element::Ba, value)),
        }
    }
}
impl TryFrom<u8> for BariumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u16> for BariumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u16) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u32> for BariumIsotope {
    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 BariumIsotope {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Ba114 => write!(f, "Ba114"),
            Self::Ba115 => write!(f, "Ba115"),
            Self::Ba116 => write!(f, "Ba116"),
            Self::Ba117 => write!(f, "Ba117"),
            Self::Ba118 => write!(f, "Ba118"),
            Self::Ba119 => write!(f, "Ba119"),
            Self::Ba120 => write!(f, "Ba120"),
            Self::Ba121 => write!(f, "Ba121"),
            Self::Ba122 => write!(f, "Ba122"),
            Self::Ba123 => write!(f, "Ba123"),
            Self::Ba124 => write!(f, "Ba124"),
            Self::Ba125 => write!(f, "Ba125"),
            Self::Ba126 => write!(f, "Ba126"),
            Self::Ba127 => write!(f, "Ba127"),
            Self::Ba128 => write!(f, "Ba128"),
            Self::Ba129 => write!(f, "Ba129"),
            Self::Ba130 => write!(f, "Ba130"),
            Self::Ba131 => write!(f, "Ba131"),
            Self::Ba132 => write!(f, "Ba132"),
            Self::Ba133 => write!(f, "Ba133"),
            Self::Ba134 => write!(f, "Ba134"),
            Self::Ba135 => write!(f, "Ba135"),
            Self::Ba136 => write!(f, "Ba136"),
            Self::Ba137 => write!(f, "Ba137"),
            Self::Ba138 => write!(f, "Ba138"),
            Self::Ba139 => write!(f, "Ba139"),
            Self::Ba140 => write!(f, "Ba140"),
            Self::Ba141 => write!(f, "Ba141"),
            Self::Ba142 => write!(f, "Ba142"),
            Self::Ba143 => write!(f, "Ba143"),
            Self::Ba144 => write!(f, "Ba144"),
            Self::Ba145 => write!(f, "Ba145"),
            Self::Ba146 => write!(f, "Ba146"),
            Self::Ba147 => write!(f, "Ba147"),
            Self::Ba148 => write!(f, "Ba148"),
            Self::Ba149 => write!(f, "Ba149"),
            Self::Ba150 => write!(f, "Ba150"),
            Self::Ba151 => write!(f, "Ba151"),
            Self::Ba152 => write!(f, "Ba152"),
            Self::Ba153 => write!(f, "Ba153"),
            Self::Ba154 => write!(f, "Ba154"),
        }
    }
}
#[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 BariumIsotope::iter() {
            let mass = isotope.relative_atomic_mass();
            assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
        }
    }
    #[test]
    fn test_element() {
        for isotope in BariumIsotope::iter() {
            let element = isotope.element();
            assert_eq!(element, crate::Element::Ba, "Element should be correct for {isotope:?}");
        }
    }
    #[test]
    fn test_mass_number() {
        for isotope in BariumIsotope::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 BariumIsotope::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 = BariumIsotope::most_abundant_isotope();
        let _ = most_abundant.relative_atomic_mass();
    }
    #[test]
    fn test_from_isotope() {
        for isotope in BariumIsotope::iter() {
            let iso: crate::Isotope = isotope.into();
            match iso {
                crate::Isotope::Ba(i) => assert_eq!(i, isotope),
                _ => panic!("Wrong isotope type"),
            }
        }
    }
    #[test]
    fn test_from_element() {
        for isotope in BariumIsotope::iter() {
            let elem: crate::Element = isotope.into();
            assert_eq!(elem, crate::Element::Ba);
        }
    }
    #[test]
    fn test_try_from_mass_number() {
        for isotope in BariumIsotope::iter() {
            let mass = isotope.mass_number();
            let iso = BariumIsotope::try_from(mass).unwrap();
            assert_eq!(iso, isotope);
            let iso_u32 = BariumIsotope::try_from(u32::from(mass)).unwrap();
            assert_eq!(iso_u32, isotope);
            if let Ok(mass_u8) = u8::try_from(mass) {
                let iso_u8 = BariumIsotope::try_from(mass_u8).unwrap();
                assert_eq!(iso_u8, isotope);
            }
        }
        assert!(BariumIsotope::try_from(0_u16).is_err());
        assert!(BariumIsotope::try_from(1000_u16).is_err());
        assert!(BariumIsotope::try_from(0_u32).is_err());
        assert!(BariumIsotope::try_from(1000_u32).is_err());
        assert!(BariumIsotope::try_from(0_u8).is_err());
    }
    #[test]
    fn test_display() {
        for isotope in BariumIsotope::iter() {
            let s = alloc::format!("{isotope}");
            assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
        }
    }
}