#[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))]
pub enum SodiumIsotope {
Na17,
Na18,
Na19,
Na20,
Na21,
Na22,
Na23,
Na24,
Na25,
Na26,
Na27,
Na28,
Na29,
Na30,
Na31,
Na32,
Na33,
Na34,
Na35,
Na36,
Na37,
Na39,
}
impl super::RelativeAtomicMass for SodiumIsotope {
#[inline]
fn relative_atomic_mass(&self) -> f64 {
match self {
Self::Na17 => 17.037273f64,
Self::Na18 => 18.02688f64,
Self::Na19 => 19.01388f64,
Self::Na20 => 20.0073544f64,
Self::Na21 => 20.99765469f64,
Self::Na22 => 21.99443741f64,
Self::Na23 => 22.989769282f64,
Self::Na24 => 23.99096295f64,
Self::Na25 => 24.989954f64,
Self::Na26 => 25.9926346f64,
Self::Na27 => 26.9940765f64,
Self::Na28 => 27.998939f64,
Self::Na29 => 29.0028771f64,
Self::Na30 => 30.0090979f64,
Self::Na31 => 31.013163f64,
Self::Na32 => 32.02019f64,
Self::Na33 => 33.02573f64,
Self::Na34 => 34.03359f64,
Self::Na35 => 35.04062f64,
Self::Na36 => 36.04929f64,
Self::Na37 => 37.05705f64,
Self::Na39 => 39.075127f64,
}
}
}
impl super::ElementVariant for SodiumIsotope {
#[inline]
fn element(&self) -> crate::Element {
crate::Element::Na
}
}
impl super::MassNumber for SodiumIsotope {
#[inline]
fn mass_number(&self) -> u16 {
match self {
Self::Na17 => 17u16,
Self::Na18 => 18u16,
Self::Na19 => 19u16,
Self::Na20 => 20u16,
Self::Na21 => 21u16,
Self::Na22 => 22u16,
Self::Na23 => 23u16,
Self::Na24 => 24u16,
Self::Na25 => 25u16,
Self::Na26 => 26u16,
Self::Na27 => 27u16,
Self::Na28 => 28u16,
Self::Na29 => 29u16,
Self::Na30 => 30u16,
Self::Na31 => 31u16,
Self::Na32 => 32u16,
Self::Na33 => 33u16,
Self::Na34 => 34u16,
Self::Na35 => 35u16,
Self::Na36 => 36u16,
Self::Na37 => 37u16,
Self::Na39 => 39u16,
}
}
}
impl super::IsotopicComposition for SodiumIsotope {
#[inline]
fn isotopic_composition(&self) -> Option<f64> {
match self {
Self::Na23 => Some(1f64),
_ => None,
}
}
}
impl super::MostAbundantIsotope for SodiumIsotope {
fn most_abundant_isotope() -> Self {
Self::Na23
}
}
impl From<SodiumIsotope> for crate::Isotope {
fn from(isotope: SodiumIsotope) -> Self {
crate::Isotope::Na(isotope)
}
}
impl From<SodiumIsotope> for crate::Element {
fn from(_isotope: SodiumIsotope) -> Self {
crate::Element::Na
}
}
impl TryFrom<u64> for SodiumIsotope {
type Error = crate::errors::Error;
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
17u64 => Ok(Self::Na17),
18u64 => Ok(Self::Na18),
19u64 => Ok(Self::Na19),
20u64 => Ok(Self::Na20),
21u64 => Ok(Self::Na21),
22u64 => Ok(Self::Na22),
23u64 => Ok(Self::Na23),
24u64 => Ok(Self::Na24),
25u64 => Ok(Self::Na25),
26u64 => Ok(Self::Na26),
27u64 => Ok(Self::Na27),
28u64 => Ok(Self::Na28),
29u64 => Ok(Self::Na29),
30u64 => Ok(Self::Na30),
31u64 => Ok(Self::Na31),
32u64 => Ok(Self::Na32),
33u64 => Ok(Self::Na33),
34u64 => Ok(Self::Na34),
35u64 => Ok(Self::Na35),
36u64 => Ok(Self::Na36),
37u64 => Ok(Self::Na37),
39u64 => Ok(Self::Na39),
_ => Err(crate::errors::Error::Isotope(crate::Element::Na, value)),
}
}
}
impl TryFrom<u8> for SodiumIsotope {
type Error = crate::errors::Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::try_from(u64::from(value))
}
}
impl TryFrom<u16> for SodiumIsotope {
type Error = crate::errors::Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Self::try_from(u64::from(value))
}
}
impl TryFrom<u32> for SodiumIsotope {
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 SodiumIsotope {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Na17 => write!(f, "Na17"),
Self::Na18 => write!(f, "Na18"),
Self::Na19 => write!(f, "Na19"),
Self::Na20 => write!(f, "Na20"),
Self::Na21 => write!(f, "Na21"),
Self::Na22 => write!(f, "Na22"),
Self::Na23 => write!(f, "Na23"),
Self::Na24 => write!(f, "Na24"),
Self::Na25 => write!(f, "Na25"),
Self::Na26 => write!(f, "Na26"),
Self::Na27 => write!(f, "Na27"),
Self::Na28 => write!(f, "Na28"),
Self::Na29 => write!(f, "Na29"),
Self::Na30 => write!(f, "Na30"),
Self::Na31 => write!(f, "Na31"),
Self::Na32 => write!(f, "Na32"),
Self::Na33 => write!(f, "Na33"),
Self::Na34 => write!(f, "Na34"),
Self::Na35 => write!(f, "Na35"),
Self::Na36 => write!(f, "Na36"),
Self::Na37 => write!(f, "Na37"),
Self::Na39 => write!(f, "Na39"),
}
}
}
#[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 SodiumIsotope::iter() {
let mass = isotope.relative_atomic_mass();
assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
}
}
#[test]
fn test_element() {
for isotope in SodiumIsotope::iter() {
let element = isotope.element();
assert_eq!(element, crate::Element::Na, "Element should be correct for {isotope:?}");
}
}
#[test]
fn test_mass_number() {
for isotope in SodiumIsotope::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 SodiumIsotope::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 = SodiumIsotope::most_abundant_isotope();
let _ = most_abundant.relative_atomic_mass();
}
#[test]
fn test_from_isotope() {
for isotope in SodiumIsotope::iter() {
let iso: crate::Isotope = isotope.into();
match iso {
crate::Isotope::Na(i) => assert_eq!(i, isotope),
_ => panic!("Wrong isotope type"),
}
}
}
#[test]
fn test_from_element() {
for isotope in SodiumIsotope::iter() {
let elem: crate::Element = isotope.into();
assert_eq!(elem, crate::Element::Na);
}
}
#[test]
fn test_try_from_mass_number() {
for isotope in SodiumIsotope::iter() {
let mass = isotope.mass_number();
let iso = SodiumIsotope::try_from(mass).unwrap();
assert_eq!(iso, isotope);
let iso_u32 = SodiumIsotope::try_from(u32::from(mass)).unwrap();
assert_eq!(iso_u32, isotope);
if let Ok(mass_u8) = u8::try_from(mass) {
let iso_u8 = SodiumIsotope::try_from(mass_u8).unwrap();
assert_eq!(iso_u8, isotope);
}
}
assert!(SodiumIsotope::try_from(0_u16).is_err());
assert!(SodiumIsotope::try_from(1000_u16).is_err());
assert!(SodiumIsotope::try_from(0_u32).is_err());
assert!(SodiumIsotope::try_from(1000_u32).is_err());
assert!(SodiumIsotope::try_from(0_u8).is_err());
}
#[test]
fn test_display() {
for isotope in SodiumIsotope::iter() {
let s = alloc::format!("{isotope}");
assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
}
}
}