#[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 OxygenIsotope {
O11,
O12,
O13,
O14,
O15,
O16,
O17,
O18,
O19,
O20,
O21,
O22,
O23,
O24,
O25,
O26,
O27,
O28,
}
impl super::RelativeAtomicMass for OxygenIsotope {
#[inline]
fn relative_atomic_mass(&self) -> f64 {
match self {
Self::O11 => 11.051249828f64,
Self::O12 => 12.034262f64,
Self::O13 => 13.024815f64,
Self::O14 => 14.00859636f64,
Self::O15 => 15.00306562f64,
Self::O16 => 15.99491461957f64,
Self::O17 => 16.9991317565f64,
Self::O18 => 17.99915961286f64,
Self::O19 => 19.003578f64,
Self::O20 => 20.00407535f64,
Self::O21 => 21.008655f64,
Self::O22 => 22.009966f64,
Self::O23 => 23.015696f64,
Self::O24 => 24.01986f64,
Self::O25 => 25.02936f64,
Self::O26 => 26.03729f64,
Self::O27 => 27.04772f64,
Self::O28 => 28.05591f64,
}
}
}
impl super::ElementVariant for OxygenIsotope {
#[inline]
fn element(&self) -> crate::Element {
crate::Element::O
}
}
impl super::MassNumber for OxygenIsotope {
#[inline]
fn mass_number(&self) -> u16 {
match self {
Self::O11 => 11u16,
Self::O12 => 12u16,
Self::O13 => 13u16,
Self::O14 => 14u16,
Self::O15 => 15u16,
Self::O16 => 16u16,
Self::O17 => 17u16,
Self::O18 => 18u16,
Self::O19 => 19u16,
Self::O20 => 20u16,
Self::O21 => 21u16,
Self::O22 => 22u16,
Self::O23 => 23u16,
Self::O24 => 24u16,
Self::O25 => 25u16,
Self::O26 => 26u16,
Self::O27 => 27u16,
Self::O28 => 28u16,
}
}
}
impl super::IsotopicComposition for OxygenIsotope {
#[inline]
fn isotopic_composition(&self) -> Option<f64> {
match self {
Self::O16 => Some(0.99757f64),
Self::O17 => Some(0.00038f64),
Self::O18 => Some(0.00205f64),
_ => None,
}
}
}
impl super::MostAbundantIsotope for OxygenIsotope {
fn most_abundant_isotope() -> Self {
Self::O16
}
}
impl From<OxygenIsotope> for crate::Isotope {
fn from(isotope: OxygenIsotope) -> Self {
crate::Isotope::O(isotope)
}
}
impl From<OxygenIsotope> for crate::Element {
fn from(_isotope: OxygenIsotope) -> Self {
crate::Element::O
}
}
impl TryFrom<u64> for OxygenIsotope {
type Error = crate::errors::Error;
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
11u64 => Ok(Self::O11),
12u64 => Ok(Self::O12),
13u64 => Ok(Self::O13),
14u64 => Ok(Self::O14),
15u64 => Ok(Self::O15),
16u64 => Ok(Self::O16),
17u64 => Ok(Self::O17),
18u64 => Ok(Self::O18),
19u64 => Ok(Self::O19),
20u64 => Ok(Self::O20),
21u64 => Ok(Self::O21),
22u64 => Ok(Self::O22),
23u64 => Ok(Self::O23),
24u64 => Ok(Self::O24),
25u64 => Ok(Self::O25),
26u64 => Ok(Self::O26),
27u64 => Ok(Self::O27),
28u64 => Ok(Self::O28),
_ => Err(crate::errors::Error::Isotope(crate::Element::O, value)),
}
}
}
impl TryFrom<u8> for OxygenIsotope {
type Error = crate::errors::Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Self::try_from(u64::from(value))
}
}
impl TryFrom<u16> for OxygenIsotope {
type Error = crate::errors::Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Self::try_from(u64::from(value))
}
}
impl TryFrom<u32> for OxygenIsotope {
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 OxygenIsotope {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::O11 => write!(f, "O11"),
Self::O12 => write!(f, "O12"),
Self::O13 => write!(f, "O13"),
Self::O14 => write!(f, "O14"),
Self::O15 => write!(f, "O15"),
Self::O16 => write!(f, "O16"),
Self::O17 => write!(f, "O17"),
Self::O18 => write!(f, "O18"),
Self::O19 => write!(f, "O19"),
Self::O20 => write!(f, "O20"),
Self::O21 => write!(f, "O21"),
Self::O22 => write!(f, "O22"),
Self::O23 => write!(f, "O23"),
Self::O24 => write!(f, "O24"),
Self::O25 => write!(f, "O25"),
Self::O26 => write!(f, "O26"),
Self::O27 => write!(f, "O27"),
Self::O28 => write!(f, "O28"),
}
}
}
#[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 OxygenIsotope::iter() {
let mass = isotope.relative_atomic_mass();
assert!(mass > 0.0, "Mass should be positive for {isotope:?}");
}
}
#[test]
fn test_element() {
for isotope in OxygenIsotope::iter() {
let element = isotope.element();
assert_eq!(element, crate::Element::O, "Element should be correct for {isotope:?}");
}
}
#[test]
fn test_mass_number() {
for isotope in OxygenIsotope::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 OxygenIsotope::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 = OxygenIsotope::most_abundant_isotope();
let _ = most_abundant.relative_atomic_mass();
}
#[test]
fn test_from_isotope() {
for isotope in OxygenIsotope::iter() {
let iso: crate::Isotope = isotope.into();
match iso {
crate::Isotope::O(i) => assert_eq!(i, isotope),
_ => panic!("Wrong isotope type"),
}
}
}
#[test]
fn test_from_element() {
for isotope in OxygenIsotope::iter() {
let elem: crate::Element = isotope.into();
assert_eq!(elem, crate::Element::O);
}
}
#[test]
fn test_try_from_mass_number() {
for isotope in OxygenIsotope::iter() {
let mass = isotope.mass_number();
let iso = OxygenIsotope::try_from(mass).unwrap();
assert_eq!(iso, isotope);
let iso_u32 = OxygenIsotope::try_from(u32::from(mass)).unwrap();
assert_eq!(iso_u32, isotope);
if let Ok(mass_u8) = u8::try_from(mass) {
let iso_u8 = OxygenIsotope::try_from(mass_u8).unwrap();
assert_eq!(iso_u8, isotope);
}
}
assert!(OxygenIsotope::try_from(0_u16).is_err());
assert!(OxygenIsotope::try_from(1000_u16).is_err());
assert!(OxygenIsotope::try_from(0_u32).is_err());
assert!(OxygenIsotope::try_from(1000_u32).is_err());
assert!(OxygenIsotope::try_from(0_u8).is_err());
}
#[test]
fn test_display() {
for isotope in OxygenIsotope::iter() {
let s = alloc::format!("{isotope}");
assert!(!s.is_empty(), "Display should not be empty for {isotope:?}");
}
}
}