use_molar_mass/
molar_mass.rs1use std::fmt;
2
3use crate::{MolarMassUnit, MolarMassValidationError};
4
5#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
7pub struct MolarMass {
8 value: f64,
9 unit: MolarMassUnit,
10}
11
12impl MolarMass {
13 pub fn new(value: f64, unit: MolarMassUnit) -> Result<Self, MolarMassValidationError> {
21 validate_molar_mass(value)?;
22
23 Ok(Self { value, unit })
24 }
25
26 pub fn grams_per_mole(value: f64) -> Result<Self, MolarMassValidationError> {
32 Self::new(value, MolarMassUnit::GramsPerMole)
33 }
34
35 pub fn kilograms_per_mole(value: f64) -> Result<Self, MolarMassValidationError> {
41 Self::new(value, MolarMassUnit::KilogramsPerMole)
42 }
43
44 #[must_use]
46 pub const fn value(self) -> f64 {
47 self.value
48 }
49
50 #[must_use]
52 pub const fn unit(self) -> MolarMassUnit {
53 self.unit
54 }
55}
56
57impl fmt::Display for MolarMass {
58 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
59 write!(formatter, "{} {}", self.value, self.unit)
60 }
61}
62
63fn validate_molar_mass(value: f64) -> Result<(), MolarMassValidationError> {
64 if !value.is_finite() {
65 Err(MolarMassValidationError::NonFiniteMolarMass)
66 } else if value <= 0.0 {
67 Err(MolarMassValidationError::NonPositiveMolarMass)
68 } else {
69 Ok(())
70 }
71}