use elements_rs::{ElementVariant, Isotope, MassNumber};
use crate::{SuperscriptMinus, SuperscriptPlus, superscript_digits_ltr};
pub(crate) fn display_isotope(
isotope: Isotope,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "[")?;
for superscript in superscript_digits_ltr(isotope.mass_number()) {
write!(f, "{superscript}")?;
}
write!(f, "{}", isotope.element())?;
write!(f, "]")
}
pub(crate) fn display_charge<C: Into<i64>>(
charge: C,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
let charge: i64 = charge.into();
if charge.abs() > 1 {
for digit in superscript_digits_ltr(charge) {
write!(f, "{digit}")?;
}
}
if charge < 0 { write!(f, "{SuperscriptMinus}") } else { write!(f, "{SuperscriptPlus}") }
}
#[cfg(test)]
mod tests {
use alloc::string::ToString;
use core::fmt::Display;
use elements_rs::Element;
use super::*;
struct IsotopeWrapper(Isotope);
impl Display for IsotopeWrapper {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
display_isotope(self.0, f)
}
}
struct ChargeWrapper(i32);
impl Display for ChargeWrapper {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
display_charge(self.0, f)
}
}
#[test]
fn test_display_isotope() {
let c13 = Isotope::try_from((Element::C, 13_u16)).unwrap();
assert_eq!(IsotopeWrapper(c13).to_string(), "[¹³C]");
let h2 = Isotope::try_from((Element::H, 2_u16)).unwrap();
assert_eq!(IsotopeWrapper(h2).to_string(), "[²H]");
}
#[test]
fn test_display_charge() {
assert_eq!(ChargeWrapper(1).to_string(), "⁺");
assert_eq!(ChargeWrapper(-1).to_string(), "⁻");
assert_eq!(ChargeWrapper(2).to_string(), "²⁺");
assert_eq!(ChargeWrapper(-2).to_string(), "²⁻");
assert_eq!(ChargeWrapper(10).to_string(), "¹⁰⁺");
assert_eq!(ChargeWrapper(-10).to_string(), "¹⁰⁻");
}
}