elements_rs 0.2.5

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 Cerium
#[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))]
/// Isotopes of the element Cerium
pub enum CeriumIsotope {
    /// Isotope Ce119 of Cerium
    Ce119,
    /// Isotope Ce120 of Cerium
    Ce120,
    /// Isotope Ce121 of Cerium
    Ce121,
    /// Isotope Ce122 of Cerium
    Ce122,
    /// Isotope Ce123 of Cerium
    Ce123,
    /// Isotope Ce124 of Cerium
    Ce124,
    /// Isotope Ce125 of Cerium
    Ce125,
    /// Isotope Ce126 of Cerium
    Ce126,
    /// Isotope Ce127 of Cerium
    Ce127,
    /// Isotope Ce128 of Cerium
    Ce128,
    /// Isotope Ce129 of Cerium
    Ce129,
    /// Isotope Ce130 of Cerium
    Ce130,
    /// Isotope Ce131 of Cerium
    Ce131,
    /// Isotope Ce132 of Cerium
    Ce132,
    /// Isotope Ce133 of Cerium
    Ce133,
    /// Isotope Ce134 of Cerium
    Ce134,
    /// Isotope Ce135 of Cerium
    Ce135,
    /// Isotope Ce136 of Cerium
    Ce136,
    /// Isotope Ce137 of Cerium
    Ce137,
    /// Isotope Ce138 of Cerium
    Ce138,
    /// Isotope Ce139 of Cerium
    Ce139,
    /// Isotope Ce140 of Cerium
    Ce140,
    /// Isotope Ce141 of Cerium
    Ce141,
    /// Isotope Ce142 of Cerium
    Ce142,
    /// Isotope Ce143 of Cerium
    Ce143,
    /// Isotope Ce144 of Cerium
    Ce144,
    /// Isotope Ce145 of Cerium
    Ce145,
    /// Isotope Ce146 of Cerium
    Ce146,
    /// Isotope Ce147 of Cerium
    Ce147,
    /// Isotope Ce148 of Cerium
    Ce148,
    /// Isotope Ce149 of Cerium
    Ce149,
    /// Isotope Ce150 of Cerium
    Ce150,
    /// Isotope Ce151 of Cerium
    Ce151,
    /// Isotope Ce152 of Cerium
    Ce152,
    /// Isotope Ce153 of Cerium
    Ce153,
    /// Isotope Ce154 of Cerium
    Ce154,
    /// Isotope Ce155 of Cerium
    Ce155,
    /// Isotope Ce156 of Cerium
    Ce156,
    /// Isotope Ce157 of Cerium
    Ce157,
    /// Isotope Ce158 of Cerium
    Ce158,
    /// Isotope Ce159 of Cerium
    Ce159,
}
impl super::RelativeAtomicMass for CeriumIsotope {
    #[inline]
    fn relative_atomic_mass(&self) -> f64 {
        match self {
            Self::Ce119 => 118.95271f64,
            Self::Ce120 => 119.94654f64,
            Self::Ce121 => 120.94335f64,
            Self::Ce122 => 121.93787f64,
            Self::Ce123 => 122.93528f64,
            Self::Ce124 => 123.93031f64,
            Self::Ce125 => 124.92844f64,
            Self::Ce126 => 125.923971f64,
            Self::Ce127 => 126.922727f64,
            Self::Ce128 => 127.918911f64,
            Self::Ce129 => 128.918102f64,
            Self::Ce130 => 129.914736f64,
            Self::Ce131 => 130.914429f64,
            Self::Ce132 => 131.911464f64,
            Self::Ce133 => 132.91152f64,
            Self::Ce134 => 133.908928f64,
            Self::Ce135 => 134.909161f64,
            Self::Ce136 => 135.90712921f64,
            Self::Ce137 => 136.90776236f64,
            Self::Ce138 => 137.905991f64,
            Self::Ce139 => 138.9066551f64,
            Self::Ce140 => 139.9054431f64,
            Self::Ce141 => 140.9082807f64,
            Self::Ce142 => 141.9092504f64,
            Self::Ce143 => 142.9123921f64,
            Self::Ce144 => 143.9136529f64,
            Self::Ce145 => 144.917265f64,
            Self::Ce146 => 145.918802f64,
            Self::Ce147 => 146.9226899f64,
            Self::Ce148 => 147.924424f64,
            Self::Ce149 => 148.928427f64,
            Self::Ce150 => 149.930384f64,
            Self::Ce151 => 150.934272f64,
            Self::Ce152 => 151.9366f64,
            Self::Ce153 => 152.94093f64,
            Self::Ce154 => 153.9438f64,
            Self::Ce155 => 154.94855f64,
            Self::Ce156 => 155.95183f64,
            Self::Ce157 => 156.95705f64,
            Self::Ce158 => 157.960773f64,
            Self::Ce159 => 158.966355f64,
        }
    }
}
impl super::ElementVariant for CeriumIsotope {
    #[inline]
    fn element(&self) -> crate::Element {
        crate::Element::Ce
    }
}
impl super::MassNumber for CeriumIsotope {
    #[inline]
    fn mass_number(&self) -> u16 {
        match self {
            Self::Ce119 => 119u16,
            Self::Ce120 => 120u16,
            Self::Ce121 => 121u16,
            Self::Ce122 => 122u16,
            Self::Ce123 => 123u16,
            Self::Ce124 => 124u16,
            Self::Ce125 => 125u16,
            Self::Ce126 => 126u16,
            Self::Ce127 => 127u16,
            Self::Ce128 => 128u16,
            Self::Ce129 => 129u16,
            Self::Ce130 => 130u16,
            Self::Ce131 => 131u16,
            Self::Ce132 => 132u16,
            Self::Ce133 => 133u16,
            Self::Ce134 => 134u16,
            Self::Ce135 => 135u16,
            Self::Ce136 => 136u16,
            Self::Ce137 => 137u16,
            Self::Ce138 => 138u16,
            Self::Ce139 => 139u16,
            Self::Ce140 => 140u16,
            Self::Ce141 => 141u16,
            Self::Ce142 => 142u16,
            Self::Ce143 => 143u16,
            Self::Ce144 => 144u16,
            Self::Ce145 => 145u16,
            Self::Ce146 => 146u16,
            Self::Ce147 => 147u16,
            Self::Ce148 => 148u16,
            Self::Ce149 => 149u16,
            Self::Ce150 => 150u16,
            Self::Ce151 => 151u16,
            Self::Ce152 => 152u16,
            Self::Ce153 => 153u16,
            Self::Ce154 => 154u16,
            Self::Ce155 => 155u16,
            Self::Ce156 => 156u16,
            Self::Ce157 => 157u16,
            Self::Ce158 => 158u16,
            Self::Ce159 => 159u16,
        }
    }
}
impl super::IsotopicComposition for CeriumIsotope {
    #[inline]
    fn isotopic_composition(&self) -> Option<f64> {
        match self {
            Self::Ce136 => Some(0.00185f64),
            Self::Ce138 => Some(0.00251f64),
            Self::Ce140 => Some(0.8845f64),
            Self::Ce142 => Some(0.11114f64),
            _ => None,
        }
    }
}
impl super::MostAbundantIsotope for CeriumIsotope {
    fn most_abundant_isotope() -> Self {
        Self::Ce140
    }
}
impl From<CeriumIsotope> for crate::Isotope {
    fn from(isotope: CeriumIsotope) -> Self {
        crate::Isotope::Ce(isotope)
    }
}
impl From<CeriumIsotope> for crate::Element {
    fn from(_isotope: CeriumIsotope) -> Self {
        crate::Element::Ce
    }
}
impl TryFrom<u64> for CeriumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        match value {
            119u64 => Ok(Self::Ce119),
            120u64 => Ok(Self::Ce120),
            121u64 => Ok(Self::Ce121),
            122u64 => Ok(Self::Ce122),
            123u64 => Ok(Self::Ce123),
            124u64 => Ok(Self::Ce124),
            125u64 => Ok(Self::Ce125),
            126u64 => Ok(Self::Ce126),
            127u64 => Ok(Self::Ce127),
            128u64 => Ok(Self::Ce128),
            129u64 => Ok(Self::Ce129),
            130u64 => Ok(Self::Ce130),
            131u64 => Ok(Self::Ce131),
            132u64 => Ok(Self::Ce132),
            133u64 => Ok(Self::Ce133),
            134u64 => Ok(Self::Ce134),
            135u64 => Ok(Self::Ce135),
            136u64 => Ok(Self::Ce136),
            137u64 => Ok(Self::Ce137),
            138u64 => Ok(Self::Ce138),
            139u64 => Ok(Self::Ce139),
            140u64 => Ok(Self::Ce140),
            141u64 => Ok(Self::Ce141),
            142u64 => Ok(Self::Ce142),
            143u64 => Ok(Self::Ce143),
            144u64 => Ok(Self::Ce144),
            145u64 => Ok(Self::Ce145),
            146u64 => Ok(Self::Ce146),
            147u64 => Ok(Self::Ce147),
            148u64 => Ok(Self::Ce148),
            149u64 => Ok(Self::Ce149),
            150u64 => Ok(Self::Ce150),
            151u64 => Ok(Self::Ce151),
            152u64 => Ok(Self::Ce152),
            153u64 => Ok(Self::Ce153),
            154u64 => Ok(Self::Ce154),
            155u64 => Ok(Self::Ce155),
            156u64 => Ok(Self::Ce156),
            157u64 => Ok(Self::Ce157),
            158u64 => Ok(Self::Ce158),
            159u64 => Ok(Self::Ce159),
            _ => Err(crate::errors::Error::Isotope(crate::Element::Ce, value)),
        }
    }
}
impl TryFrom<u8> for CeriumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u16> for CeriumIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u16) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u32> for CeriumIsotope {
    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 CeriumIsotope {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Ce119 => write!(f, "Ce119"),
            Self::Ce120 => write!(f, "Ce120"),
            Self::Ce121 => write!(f, "Ce121"),
            Self::Ce122 => write!(f, "Ce122"),
            Self::Ce123 => write!(f, "Ce123"),
            Self::Ce124 => write!(f, "Ce124"),
            Self::Ce125 => write!(f, "Ce125"),
            Self::Ce126 => write!(f, "Ce126"),
            Self::Ce127 => write!(f, "Ce127"),
            Self::Ce128 => write!(f, "Ce128"),
            Self::Ce129 => write!(f, "Ce129"),
            Self::Ce130 => write!(f, "Ce130"),
            Self::Ce131 => write!(f, "Ce131"),
            Self::Ce132 => write!(f, "Ce132"),
            Self::Ce133 => write!(f, "Ce133"),
            Self::Ce134 => write!(f, "Ce134"),
            Self::Ce135 => write!(f, "Ce135"),
            Self::Ce136 => write!(f, "Ce136"),
            Self::Ce137 => write!(f, "Ce137"),
            Self::Ce138 => write!(f, "Ce138"),
            Self::Ce139 => write!(f, "Ce139"),
            Self::Ce140 => write!(f, "Ce140"),
            Self::Ce141 => write!(f, "Ce141"),
            Self::Ce142 => write!(f, "Ce142"),
            Self::Ce143 => write!(f, "Ce143"),
            Self::Ce144 => write!(f, "Ce144"),
            Self::Ce145 => write!(f, "Ce145"),
            Self::Ce146 => write!(f, "Ce146"),
            Self::Ce147 => write!(f, "Ce147"),
            Self::Ce148 => write!(f, "Ce148"),
            Self::Ce149 => write!(f, "Ce149"),
            Self::Ce150 => write!(f, "Ce150"),
            Self::Ce151 => write!(f, "Ce151"),
            Self::Ce152 => write!(f, "Ce152"),
            Self::Ce153 => write!(f, "Ce153"),
            Self::Ce154 => write!(f, "Ce154"),
            Self::Ce155 => write!(f, "Ce155"),
            Self::Ce156 => write!(f, "Ce156"),
            Self::Ce157 => write!(f, "Ce157"),
            Self::Ce158 => write!(f, "Ce158"),
            Self::Ce159 => write!(f, "Ce159"),
        }
    }
}
#[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 CeriumIsotope::iter() {
            let mass = isotope.relative_atomic_mass();
            assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
        }
    }
    #[test]
    fn test_element() {
        for isotope in CeriumIsotope::iter() {
            let element = isotope.element();
            assert_eq!(element, crate::Element::Ce, "Element should be correct for {isotope:?}");
        }
    }
    #[test]
    fn test_mass_number() {
        for isotope in CeriumIsotope::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 CeriumIsotope::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 = CeriumIsotope::most_abundant_isotope();
        let _ = most_abundant.relative_atomic_mass();
    }
    #[test]
    fn test_from_isotope() {
        for isotope in CeriumIsotope::iter() {
            let iso: crate::Isotope = isotope.into();
            match iso {
                crate::Isotope::Ce(i) => assert_eq!(i, isotope),
                _ => panic!("Wrong isotope type"),
            }
        }
    }
    #[test]
    fn test_from_element() {
        for isotope in CeriumIsotope::iter() {
            let elem: crate::Element = isotope.into();
            assert_eq!(elem, crate::Element::Ce);
        }
    }
    #[test]
    fn test_try_from_mass_number() {
        for isotope in CeriumIsotope::iter() {
            let mass = isotope.mass_number();
            let iso = CeriumIsotope::try_from(mass).unwrap();
            assert_eq!(iso, isotope);
            let iso_u32 = CeriumIsotope::try_from(u32::from(mass)).unwrap();
            assert_eq!(iso_u32, isotope);
            if let Ok(mass_u8) = u8::try_from(mass) {
                let iso_u8 = CeriumIsotope::try_from(mass_u8).unwrap();
                assert_eq!(iso_u8, isotope);
            }
        }
        assert!(CeriumIsotope::try_from(0_u16).is_err());
        assert!(CeriumIsotope::try_from(1000_u16).is_err());
        assert!(CeriumIsotope::try_from(0_u32).is_err());
        assert!(CeriumIsotope::try_from(1000_u32).is_err());
        assert!(CeriumIsotope::try_from(0_u8).is_err());
    }
    #[test]
    fn test_display() {
        for isotope in CeriumIsotope::iter() {
            let s = alloc::format!("{isotope}");
            assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
        }
    }
}