#[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))]
pub enum SulfurIsotope {
S26,
S27,
S28,
S29,
S30,
S31,
S32,
S33,
S34,
S35,
S36,
S37,
S38,
S39,
S40,
S41,
S42,
S43,
S44,
S45,
S46,
S47,
S48,
S49,
}
impl super::RelativeAtomicMass for SulfurIsotope {
#[inline]
fn relative_atomic_mass(&self) -> f64 {
match self {
Self::S26 => 26.02907f64,
Self::S27 => 27.01828f64,
Self::S28 => 28.00437f64,
Self::S29 => 28.996611f64,
Self::S30 => 29.98490703f64,
Self::S31 => 30.97955701f64,
Self::S32 => 31.9720711744f64,
Self::S33 => 32.9714589098f64,
Self::S34 => 33.967867004f64,
Self::S35 => 34.96903231f64,
Self::S36 => 35.96708071f64,
Self::S37 => 36.97112551f64,
Self::S38 => 37.9711633f64,
Self::S39 => 38.975134f64,
Self::S40 => 39.9754826f64,
Self::S41 => 40.9795935f64,
Self::S42 => 41.9810651f64,
Self::S43 => 42.9869076f64,
Self::S44 => 43.9901188f64,
Self::S45 => 44.99572f64,
Self::S46 => 46.00004f64,
Self::S47 => 47.00795f64,
Self::S48 => 48.0137f64,
Self::S49 => 49.021891f64,
}
}
}
impl super::ElementVariant for SulfurIsotope {
#[inline]
fn element(&self) -> crate::Element {
crate::Element::S
}
}
impl super::MassNumber for SulfurIsotope {
#[inline]
fn mass_number(&self) -> u16 {
match self {
Self::S26 => 26u16,
Self::S27 => 27u16,
Self::S28 => 28u16,
Self::S29 => 29u16,
Self::S30 => 30u16,
Self::S31 => 31u16,
Self::S32 => 32u16,
Self::S33 => 33u16,
Self::S34 => 34u16,
Self::S35 => 35u16,
Self::S36 => 36u16,
Self::S37 => 37u16,
Self::S38 => 38u16,
Self::S39 => 39u16,
Self::S40 => 40u16,
Self::S41 => 41u16,
Self::S42 => 42u16,
Self::S43 => 43u16,
Self::S44 => 44u16,
Self::S45 => 45u16,
Self::S46 => 46u16,
Self::S47 => 47u16,
Self::S48 => 48u16,
Self::S49 => 49u16,
}
}
}
impl super::IsotopicComposition for SulfurIsotope {
#[inline]
fn isotopic_composition(&self) -> Option<f64> {
match self {
Self::S32 => Some(0.9499f64),
Self::S33 => Some(0.0075f64),
Self::S34 => Some(0.0425f64),
Self::S36 => Some(0.0001f64),
_ => None,
}
}
}
impl super::MostAbundantIsotope for SulfurIsotope {
fn most_abundant_isotope() -> Self {
Self::S32
}
}
impl From<SulfurIsotope> for crate::Isotope {
fn from(isotope: SulfurIsotope) -> Self {
crate::Isotope::S(isotope)
}
}
impl From<SulfurIsotope> for crate::Element {
fn from(_isotope: SulfurIsotope) -> Self {
crate::Element::S
}
}
impl TryFrom<u64> for SulfurIsotope {
type Error = crate::errors::Error;
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
26u64 => Ok(Self::S26),
27u64 => Ok(Self::S27),
28u64 => Ok(Self::S28),
29u64 => Ok(Self::S29),
30u64 => Ok(Self::S30),
31u64 => Ok(Self::S31),
32u64 => Ok(Self::S32),
33u64 => Ok(Self::S33),
34u64 => Ok(Self::S34),
35u64 => Ok(Self::S35),
36u64 => Ok(Self::S36),
37u64 => Ok(Self::S37),
38u64 => Ok(Self::S38),
39u64 => Ok(Self::S39),
40u64 => Ok(Self::S40),
41u64 => Ok(Self::S41),
42u64 => Ok(Self::S42),
43u64 => Ok(Self::S43),
44u64 => Ok(Self::S44),
45u64 => Ok(Self::S45),
46u64 => Ok(Self::S46),
47u64 => Ok(Self::S47),
48u64 => Ok(Self::S48),
49u64 => Ok(Self::S49),
_ => Err(crate::errors::Error::Isotope(crate::Element::S, value)),
}
}
}
impl TryFrom<u8> for SulfurIsotope {
type Error = crate::errors::Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::try_from(u64::from(value))
}
}
impl TryFrom<u16> for SulfurIsotope {
type Error = crate::errors::Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Self::try_from(u64::from(value))
}
}
impl TryFrom<u32> for SulfurIsotope {
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 SulfurIsotope {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::S26 => write!(f, "S26"),
Self::S27 => write!(f, "S27"),
Self::S28 => write!(f, "S28"),
Self::S29 => write!(f, "S29"),
Self::S30 => write!(f, "S30"),
Self::S31 => write!(f, "S31"),
Self::S32 => write!(f, "S32"),
Self::S33 => write!(f, "S33"),
Self::S34 => write!(f, "S34"),
Self::S35 => write!(f, "S35"),
Self::S36 => write!(f, "S36"),
Self::S37 => write!(f, "S37"),
Self::S38 => write!(f, "S38"),
Self::S39 => write!(f, "S39"),
Self::S40 => write!(f, "S40"),
Self::S41 => write!(f, "S41"),
Self::S42 => write!(f, "S42"),
Self::S43 => write!(f, "S43"),
Self::S44 => write!(f, "S44"),
Self::S45 => write!(f, "S45"),
Self::S46 => write!(f, "S46"),
Self::S47 => write!(f, "S47"),
Self::S48 => write!(f, "S48"),
Self::S49 => write!(f, "S49"),
}
}
}
#[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 SulfurIsotope::iter() {
let mass = isotope.relative_atomic_mass();
assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
}
}
#[test]
fn test_element() {
for isotope in SulfurIsotope::iter() {
let element = isotope.element();
assert_eq!(element, crate::Element::S, "Element should be correct for {isotope:?}");
}
}
#[test]
fn test_mass_number() {
for isotope in SulfurIsotope::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 SulfurIsotope::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 = SulfurIsotope::most_abundant_isotope();
let _ = most_abundant.relative_atomic_mass();
}
#[test]
fn test_from_isotope() {
for isotope in SulfurIsotope::iter() {
let iso: crate::Isotope = isotope.into();
match iso {
crate::Isotope::S(i) => assert_eq!(i, isotope),
_ => panic!("Wrong isotope type"),
}
}
}
#[test]
fn test_from_element() {
for isotope in SulfurIsotope::iter() {
let elem: crate::Element = isotope.into();
assert_eq!(elem, crate::Element::S);
}
}
#[test]
fn test_try_from_mass_number() {
for isotope in SulfurIsotope::iter() {
let mass = isotope.mass_number();
let iso = SulfurIsotope::try_from(mass).unwrap();
assert_eq!(iso, isotope);
let iso_u32 = SulfurIsotope::try_from(u32::from(mass)).unwrap();
assert_eq!(iso_u32, isotope);
if let Ok(mass_u8) = u8::try_from(mass) {
let iso_u8 = SulfurIsotope::try_from(mass_u8).unwrap();
assert_eq!(iso_u8, isotope);
}
}
assert!(SulfurIsotope::try_from(0_u16).is_err());
assert!(SulfurIsotope::try_from(1000_u16).is_err());
assert!(SulfurIsotope::try_from(0_u32).is_err());
assert!(SulfurIsotope::try_from(1000_u32).is_err());
assert!(SulfurIsotope::try_from(0_u8).is_err());
}
#[test]
fn test_display() {
for isotope in SulfurIsotope::iter() {
let s = alloc::format!("{isotope}");
assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
}
}
}