elements_rs 0.2.5

A comprehensive library for chemical elements and their isotopes with rich metadata
Documentation
//! Isotopes of the element Chlorine
#[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 Chlorine
pub enum ChlorineIsotope {
    /// Isotope Cl28 of Chlorine
    Cl28,
    /// Isotope Cl29 of Chlorine
    Cl29,
    /// Isotope Cl30 of Chlorine
    Cl30,
    /// Isotope Cl31 of Chlorine
    Cl31,
    /// Isotope Cl32 of Chlorine
    Cl32,
    /// Isotope Cl33 of Chlorine
    Cl33,
    /// Isotope Cl34 of Chlorine
    Cl34,
    /// Isotope Cl35 of Chlorine
    Cl35,
    /// Isotope Cl36 of Chlorine
    Cl36,
    /// Isotope Cl37 of Chlorine
    Cl37,
    /// Isotope Cl38 of Chlorine
    Cl38,
    /// Isotope Cl39 of Chlorine
    Cl39,
    /// Isotope Cl40 of Chlorine
    Cl40,
    /// Isotope Cl41 of Chlorine
    Cl41,
    /// Isotope Cl42 of Chlorine
    Cl42,
    /// Isotope Cl43 of Chlorine
    Cl43,
    /// Isotope Cl44 of Chlorine
    Cl44,
    /// Isotope Cl45 of Chlorine
    Cl45,
    /// Isotope Cl46 of Chlorine
    Cl46,
    /// Isotope Cl47 of Chlorine
    Cl47,
    /// Isotope Cl48 of Chlorine
    Cl48,
    /// Isotope Cl49 of Chlorine
    Cl49,
    /// Isotope Cl50 of Chlorine
    Cl50,
    /// Isotope Cl51 of Chlorine
    Cl51,
    /// Isotope Cl52 of Chlorine
    Cl52,
}
impl super::RelativeAtomicMass for ChlorineIsotope {
    #[inline]
    fn relative_atomic_mass(&self) -> f64 {
        match self {
            Self::Cl28 => 28.02954f64,
            Self::Cl29 => 29.01478f64,
            Self::Cl30 => 30.00477f64,
            Self::Cl31 => 30.992414f64,
            Self::Cl32 => 31.98568464f64,
            Self::Cl33 => 32.97745199f64,
            Self::Cl34 => 33.973762485f64,
            Self::Cl35 => 34.968852682f64,
            Self::Cl36 => 35.968306809f64,
            Self::Cl37 => 36.965902602f64,
            Self::Cl38 => 37.96801044f64,
            Self::Cl39 => 38.9680082f64,
            Self::Cl40 => 39.970415f64,
            Self::Cl41 => 40.970685f64,
            Self::Cl42 => 41.97325f64,
            Self::Cl43 => 42.97389f64,
            Self::Cl44 => 43.97787f64,
            Self::Cl45 => 44.98029f64,
            Self::Cl46 => 45.98517f64,
            Self::Cl47 => 46.98916f64,
            Self::Cl48 => 47.99564f64,
            Self::Cl49 => 49.00123f64,
            Self::Cl50 => 50.00905f64,
            Self::Cl51 => 51.01554f64,
            Self::Cl52 => 52.024004f64,
        }
    }
}
impl super::ElementVariant for ChlorineIsotope {
    #[inline]
    fn element(&self) -> crate::Element {
        crate::Element::Cl
    }
}
impl super::MassNumber for ChlorineIsotope {
    #[inline]
    fn mass_number(&self) -> u16 {
        match self {
            Self::Cl28 => 28u16,
            Self::Cl29 => 29u16,
            Self::Cl30 => 30u16,
            Self::Cl31 => 31u16,
            Self::Cl32 => 32u16,
            Self::Cl33 => 33u16,
            Self::Cl34 => 34u16,
            Self::Cl35 => 35u16,
            Self::Cl36 => 36u16,
            Self::Cl37 => 37u16,
            Self::Cl38 => 38u16,
            Self::Cl39 => 39u16,
            Self::Cl40 => 40u16,
            Self::Cl41 => 41u16,
            Self::Cl42 => 42u16,
            Self::Cl43 => 43u16,
            Self::Cl44 => 44u16,
            Self::Cl45 => 45u16,
            Self::Cl46 => 46u16,
            Self::Cl47 => 47u16,
            Self::Cl48 => 48u16,
            Self::Cl49 => 49u16,
            Self::Cl50 => 50u16,
            Self::Cl51 => 51u16,
            Self::Cl52 => 52u16,
        }
    }
}
impl super::IsotopicComposition for ChlorineIsotope {
    #[inline]
    fn isotopic_composition(&self) -> Option<f64> {
        match self {
            Self::Cl35 => Some(0.7576f64),
            Self::Cl37 => Some(0.2424f64),
            _ => None,
        }
    }
}
impl super::MostAbundantIsotope for ChlorineIsotope {
    fn most_abundant_isotope() -> Self {
        Self::Cl35
    }
}
impl From<ChlorineIsotope> for crate::Isotope {
    fn from(isotope: ChlorineIsotope) -> Self {
        crate::Isotope::Cl(isotope)
    }
}
impl From<ChlorineIsotope> for crate::Element {
    fn from(_isotope: ChlorineIsotope) -> Self {
        crate::Element::Cl
    }
}
impl TryFrom<u64> for ChlorineIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        match value {
            28u64 => Ok(Self::Cl28),
            29u64 => Ok(Self::Cl29),
            30u64 => Ok(Self::Cl30),
            31u64 => Ok(Self::Cl31),
            32u64 => Ok(Self::Cl32),
            33u64 => Ok(Self::Cl33),
            34u64 => Ok(Self::Cl34),
            35u64 => Ok(Self::Cl35),
            36u64 => Ok(Self::Cl36),
            37u64 => Ok(Self::Cl37),
            38u64 => Ok(Self::Cl38),
            39u64 => Ok(Self::Cl39),
            40u64 => Ok(Self::Cl40),
            41u64 => Ok(Self::Cl41),
            42u64 => Ok(Self::Cl42),
            43u64 => Ok(Self::Cl43),
            44u64 => Ok(Self::Cl44),
            45u64 => Ok(Self::Cl45),
            46u64 => Ok(Self::Cl46),
            47u64 => Ok(Self::Cl47),
            48u64 => Ok(Self::Cl48),
            49u64 => Ok(Self::Cl49),
            50u64 => Ok(Self::Cl50),
            51u64 => Ok(Self::Cl51),
            52u64 => Ok(Self::Cl52),
            _ => Err(crate::errors::Error::Isotope(crate::Element::Cl, value)),
        }
    }
}
impl TryFrom<u8> for ChlorineIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u16> for ChlorineIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u16) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u32> for ChlorineIsotope {
    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 ChlorineIsotope {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Cl28 => write!(f, "Cl28"),
            Self::Cl29 => write!(f, "Cl29"),
            Self::Cl30 => write!(f, "Cl30"),
            Self::Cl31 => write!(f, "Cl31"),
            Self::Cl32 => write!(f, "Cl32"),
            Self::Cl33 => write!(f, "Cl33"),
            Self::Cl34 => write!(f, "Cl34"),
            Self::Cl35 => write!(f, "Cl35"),
            Self::Cl36 => write!(f, "Cl36"),
            Self::Cl37 => write!(f, "Cl37"),
            Self::Cl38 => write!(f, "Cl38"),
            Self::Cl39 => write!(f, "Cl39"),
            Self::Cl40 => write!(f, "Cl40"),
            Self::Cl41 => write!(f, "Cl41"),
            Self::Cl42 => write!(f, "Cl42"),
            Self::Cl43 => write!(f, "Cl43"),
            Self::Cl44 => write!(f, "Cl44"),
            Self::Cl45 => write!(f, "Cl45"),
            Self::Cl46 => write!(f, "Cl46"),
            Self::Cl47 => write!(f, "Cl47"),
            Self::Cl48 => write!(f, "Cl48"),
            Self::Cl49 => write!(f, "Cl49"),
            Self::Cl50 => write!(f, "Cl50"),
            Self::Cl51 => write!(f, "Cl51"),
            Self::Cl52 => write!(f, "Cl52"),
        }
    }
}
#[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 ChlorineIsotope::iter() {
            let mass = isotope.relative_atomic_mass();
            assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
        }
    }
    #[test]
    fn test_element() {
        for isotope in ChlorineIsotope::iter() {
            let element = isotope.element();
            assert_eq!(element, crate::Element::Cl, "Element should be correct for {isotope:?}");
        }
    }
    #[test]
    fn test_mass_number() {
        for isotope in ChlorineIsotope::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 ChlorineIsotope::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 = ChlorineIsotope::most_abundant_isotope();
        let _ = most_abundant.relative_atomic_mass();
    }
    #[test]
    fn test_from_isotope() {
        for isotope in ChlorineIsotope::iter() {
            let iso: crate::Isotope = isotope.into();
            match iso {
                crate::Isotope::Cl(i) => assert_eq!(i, isotope),
                _ => panic!("Wrong isotope type"),
            }
        }
    }
    #[test]
    fn test_from_element() {
        for isotope in ChlorineIsotope::iter() {
            let elem: crate::Element = isotope.into();
            assert_eq!(elem, crate::Element::Cl);
        }
    }
    #[test]
    fn test_try_from_mass_number() {
        for isotope in ChlorineIsotope::iter() {
            let mass = isotope.mass_number();
            let iso = ChlorineIsotope::try_from(mass).unwrap();
            assert_eq!(iso, isotope);
            let iso_u32 = ChlorineIsotope::try_from(u32::from(mass)).unwrap();
            assert_eq!(iso_u32, isotope);
            if let Ok(mass_u8) = u8::try_from(mass) {
                let iso_u8 = ChlorineIsotope::try_from(mass_u8).unwrap();
                assert_eq!(iso_u8, isotope);
            }
        }
        assert!(ChlorineIsotope::try_from(0_u16).is_err());
        assert!(ChlorineIsotope::try_from(1000_u16).is_err());
        assert!(ChlorineIsotope::try_from(0_u32).is_err());
        assert!(ChlorineIsotope::try_from(1000_u32).is_err());
        assert!(ChlorineIsotope::try_from(0_u8).is_err());
    }
    #[test]
    fn test_display() {
        for isotope in ChlorineIsotope::iter() {
            let s = alloc::format!("{isotope}");
            assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
        }
    }
}