elements_rs 0.2.6

A comprehensive library for chemical elements and their isotopes with rich metadata
Documentation
//! Isotopes of the element Phosphorus
#[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 Phosphorus
pub enum PhosphorusIsotope {
    /// Isotope P25 of Phosphorus
    P25,
    /// Isotope P26 of Phosphorus
    P26,
    /// Isotope P27 of Phosphorus
    P27,
    /// Isotope P28 of Phosphorus
    P28,
    /// Isotope P29 of Phosphorus
    P29,
    /// Isotope P30 of Phosphorus
    P30,
    /// Isotope P31 of Phosphorus
    P31,
    /// Isotope P32 of Phosphorus
    P32,
    /// Isotope P33 of Phosphorus
    P33,
    /// Isotope P34 of Phosphorus
    P34,
    /// Isotope P35 of Phosphorus
    P35,
    /// Isotope P36 of Phosphorus
    P36,
    /// Isotope P37 of Phosphorus
    P37,
    /// Isotope P38 of Phosphorus
    P38,
    /// Isotope P39 of Phosphorus
    P39,
    /// Isotope P40 of Phosphorus
    P40,
    /// Isotope P41 of Phosphorus
    P41,
    /// Isotope P42 of Phosphorus
    P42,
    /// Isotope P43 of Phosphorus
    P43,
    /// Isotope P44 of Phosphorus
    P44,
    /// Isotope P45 of Phosphorus
    P45,
    /// Isotope P46 of Phosphorus
    P46,
    /// Isotope P47 of Phosphorus
    P47,
}
impl super::RelativeAtomicMass for PhosphorusIsotope {
    #[inline]
    fn relative_atomic_mass(&self) -> f64 {
        match self {
            Self::P25 => 25.02119f64,
            Self::P26 => 26.01178f64,
            Self::P27 => 26.999224f64,
            Self::P28 => 27.9923266f64,
            Self::P29 => 28.98180079f64,
            Self::P30 => 29.97831375f64,
            Self::P31 => 30.97376199842f64,
            Self::P32 => 31.973907643f64,
            Self::P33 => 32.9717257f64,
            Self::P34 => 33.97364589f64,
            Self::P35 => 34.9733141f64,
            Self::P36 => 35.97826f64,
            Self::P37 => 36.979607f64,
            Self::P38 => 37.984252f64,
            Self::P39 => 38.986227f64,
            Self::P40 => 39.99133f64,
            Self::P41 => 40.994654f64,
            Self::P42 => 42.00108f64,
            Self::P43 => 43.00502f64,
            Self::P44 => 44.01121f64,
            Self::P45 => 45.01645f64,
            Self::P46 => 46.02446f64,
            Self::P47 => 47.030929f64,
        }
    }
}
impl super::ElementVariant for PhosphorusIsotope {
    #[inline]
    fn element(&self) -> crate::Element {
        crate::Element::P
    }
}
impl super::MassNumber for PhosphorusIsotope {
    #[inline]
    fn mass_number(&self) -> u16 {
        match self {
            Self::P25 => 25u16,
            Self::P26 => 26u16,
            Self::P27 => 27u16,
            Self::P28 => 28u16,
            Self::P29 => 29u16,
            Self::P30 => 30u16,
            Self::P31 => 31u16,
            Self::P32 => 32u16,
            Self::P33 => 33u16,
            Self::P34 => 34u16,
            Self::P35 => 35u16,
            Self::P36 => 36u16,
            Self::P37 => 37u16,
            Self::P38 => 38u16,
            Self::P39 => 39u16,
            Self::P40 => 40u16,
            Self::P41 => 41u16,
            Self::P42 => 42u16,
            Self::P43 => 43u16,
            Self::P44 => 44u16,
            Self::P45 => 45u16,
            Self::P46 => 46u16,
            Self::P47 => 47u16,
        }
    }
}
impl super::IsotopicComposition for PhosphorusIsotope {
    #[inline]
    fn isotopic_composition(&self) -> Option<f64> {
        match self {
            Self::P31 => Some(1f64),
            _ => None,
        }
    }
}
impl super::MostAbundantIsotope for PhosphorusIsotope {
    fn most_abundant_isotope() -> Self {
        Self::P31
    }
}
impl From<PhosphorusIsotope> for crate::Isotope {
    fn from(isotope: PhosphorusIsotope) -> Self {
        crate::Isotope::P(isotope)
    }
}
impl From<PhosphorusIsotope> for crate::Element {
    fn from(_isotope: PhosphorusIsotope) -> Self {
        crate::Element::P
    }
}
impl TryFrom<u64> for PhosphorusIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        match value {
            25u64 => Ok(Self::P25),
            26u64 => Ok(Self::P26),
            27u64 => Ok(Self::P27),
            28u64 => Ok(Self::P28),
            29u64 => Ok(Self::P29),
            30u64 => Ok(Self::P30),
            31u64 => Ok(Self::P31),
            32u64 => Ok(Self::P32),
            33u64 => Ok(Self::P33),
            34u64 => Ok(Self::P34),
            35u64 => Ok(Self::P35),
            36u64 => Ok(Self::P36),
            37u64 => Ok(Self::P37),
            38u64 => Ok(Self::P38),
            39u64 => Ok(Self::P39),
            40u64 => Ok(Self::P40),
            41u64 => Ok(Self::P41),
            42u64 => Ok(Self::P42),
            43u64 => Ok(Self::P43),
            44u64 => Ok(Self::P44),
            45u64 => Ok(Self::P45),
            46u64 => Ok(Self::P46),
            47u64 => Ok(Self::P47),
            _ => Err(crate::errors::Error::Isotope(crate::Element::P, value)),
        }
    }
}
impl TryFrom<u8> for PhosphorusIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u16> for PhosphorusIsotope {
    type Error = crate::errors::Error;
    fn try_from(value: u16) -> Result<Self, Self::Error> {
        Self::try_from(u64::from(value))
    }
}
impl TryFrom<u32> for PhosphorusIsotope {
    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 PhosphorusIsotope {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::P25 => write!(f, "P25"),
            Self::P26 => write!(f, "P26"),
            Self::P27 => write!(f, "P27"),
            Self::P28 => write!(f, "P28"),
            Self::P29 => write!(f, "P29"),
            Self::P30 => write!(f, "P30"),
            Self::P31 => write!(f, "P31"),
            Self::P32 => write!(f, "P32"),
            Self::P33 => write!(f, "P33"),
            Self::P34 => write!(f, "P34"),
            Self::P35 => write!(f, "P35"),
            Self::P36 => write!(f, "P36"),
            Self::P37 => write!(f, "P37"),
            Self::P38 => write!(f, "P38"),
            Self::P39 => write!(f, "P39"),
            Self::P40 => write!(f, "P40"),
            Self::P41 => write!(f, "P41"),
            Self::P42 => write!(f, "P42"),
            Self::P43 => write!(f, "P43"),
            Self::P44 => write!(f, "P44"),
            Self::P45 => write!(f, "P45"),
            Self::P46 => write!(f, "P46"),
            Self::P47 => write!(f, "P47"),
        }
    }
}
#[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 PhosphorusIsotope::iter() {
            let mass = isotope.relative_atomic_mass();
            assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
        }
    }
    #[test]
    fn test_element() {
        for isotope in PhosphorusIsotope::iter() {
            let element = isotope.element();
            assert_eq!(element, crate::Element::P, "Element should be correct for {isotope:?}");
        }
    }
    #[test]
    fn test_mass_number() {
        for isotope in PhosphorusIsotope::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 PhosphorusIsotope::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 = PhosphorusIsotope::most_abundant_isotope();
        let _ = most_abundant.relative_atomic_mass();
    }
    #[test]
    fn test_from_isotope() {
        for isotope in PhosphorusIsotope::iter() {
            let iso: crate::Isotope = isotope.into();
            match iso {
                crate::Isotope::P(i) => assert_eq!(i, isotope),
                _ => panic!("Wrong isotope type"),
            }
        }
    }
    #[test]
    fn test_from_element() {
        for isotope in PhosphorusIsotope::iter() {
            let elem: crate::Element = isotope.into();
            assert_eq!(elem, crate::Element::P);
        }
    }
    #[test]
    fn test_try_from_mass_number() {
        for isotope in PhosphorusIsotope::iter() {
            let mass = isotope.mass_number();
            let iso = PhosphorusIsotope::try_from(mass).unwrap();
            assert_eq!(iso, isotope);
            let iso_u32 = PhosphorusIsotope::try_from(u32::from(mass)).unwrap();
            assert_eq!(iso_u32, isotope);
            if let Ok(mass_u8) = u8::try_from(mass) {
                let iso_u8 = PhosphorusIsotope::try_from(mass_u8).unwrap();
                assert_eq!(iso_u8, isotope);
            }
        }
        assert!(PhosphorusIsotope::try_from(0_u16).is_err());
        assert!(PhosphorusIsotope::try_from(1000_u16).is_err());
        assert!(PhosphorusIsotope::try_from(0_u32).is_err());
        assert!(PhosphorusIsotope::try_from(1000_u32).is_err());
        assert!(PhosphorusIsotope::try_from(0_u8).is_err());
    }
    #[test]
    fn test_display() {
        for isotope in PhosphorusIsotope::iter() {
            let s = alloc::format!("{isotope}");
            assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
        }
    }
}