| 1 | use core::{ |
| 2 | cmp::Ordering, |
| 3 | fmt::Display, |
| 4 | ops::{Add, Div, Mul, Sub}, |
| 5 | }; |
| 6 | |
| 7 | /// Create a type containing a single f64 value. |
| 8 | /// |
| 9 | /// The type implements Display and basic arithmetic operations. |
| 10 | /// This is meant for creating quantity types to be returned by the element/isotope properties, so |
| 11 | /// there is no ambiguity about which unit is used. |
| 12 | macro_rules! newtype_f64 { |
| 13 | ($name:ident, $plural:literal, $abbrev:literal) => { |
| 14 | #[doc = concat!("A value in ", $plural, " (", $abbrev, ").")] |
| 15 | #[doc = ""] |
| 16 | #[doc = "Basic arithmetic is implemented for convenience, and the underlying f64 value is"] |
| 17 | #[doc = "accessible as a tuple field."] |
| 18 | #[doc = ""] |
| 19 | #[doc = "# Example"] |
| 20 | #[doc = "```"] |
| 21 | #[doc = concat!("use mendeleev::", stringify!($name), ";")] |
| 22 | #[doc = "use core::cmp::Ordering;"] |
| 23 | #[doc = ""] |
| 24 | #[doc = concat!("let value1 = ", stringify!($name), "(1.0);")] |
| 25 | #[doc = concat!("let value2 = ", stringify!($name), "(2.0);")] |
| 26 | #[doc = "assert_eq!(value1.0, 1.0);"] |
| 27 | #[doc = "assert!(value1 < value2);"] |
| 28 | #[doc = "assert_eq!(value1.total_cmp(&value2), Ordering::Less);"] |
| 29 | #[doc = concat!("assert_eq!(value1 + value2, ", stringify!($name), "(3.0));")] |
| 30 | #[doc = concat!("assert_eq!(value1 - value2, ", stringify!($name), "(-1.0));")] |
| 31 | #[doc = concat!("assert_eq!(value1 * 5.0, ", stringify!($name), "(5.0));")] |
| 32 | #[doc = concat!("assert_eq!(value1 / 4.0, ", stringify!($name), "(0.25));")] |
| 33 | #[doc = "```"] |
| 34 | #[derive(Clone, Copy, Debug, PartialEq, PartialOrd)] |
| 35 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 36 | pub struct $name(pub f64); |
| 37 | |
| 38 | impl $name { |
| 39 | /// Return the ordering between `self` and `other`. |
| 40 | /// |
| 41 | /// This simply calls [`f64::total_cmp`] on the inner value. |
| 42 | pub fn total_cmp(&self, other: &Self) -> Ordering { |
| 43 | self.0.total_cmp(&other.0) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl Add for $name { |
| 48 | type Output = Self; |
| 49 | |
| 50 | fn add(self, rhs: Self) -> Self::Output { |
| 51 | Self(self.0 + rhs.0) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl Sub for $name { |
| 56 | type Output = Self; |
| 57 | |
| 58 | fn sub(self, rhs: Self) -> Self::Output { |
| 59 | Self(self.0 - rhs.0) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl Mul<f64> for $name { |
| 64 | type Output = Self; |
| 65 | |
| 66 | fn mul(self, rhs: f64) -> Self::Output { |
| 67 | Self(self.0 * rhs) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl Div<f64> for $name { |
| 72 | type Output = Self; |
| 73 | |
| 74 | fn div(self, rhs: f64) -> Self::Output { |
| 75 | Self(self.0 / rhs) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl Display for $name { |
| 80 | /// Displays the value along with the unit. Supports numeric formatting. |
| 81 | /// |
| 82 | /// A space is added between the number and the unit, in accordance with the SI |
| 83 | /// convention. |
| 84 | #[doc = "# Example"] |
| 85 | #[doc = "```"] |
| 86 | #[doc = concat!("use mendeleev::", stringify!($name), ";")] |
| 87 | #[doc = ""] |
| 88 | #[doc = concat!("let value = ", stringify!($name), "(1.2345);")] |
| 89 | #[doc = concat!("assert_eq!(format!(\"{value:.1}\"), \"1.2 ", $abbrev, "\");")] |
| 90 | #[doc = "```"] |
| 91 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 92 | self.0.fmt(f)?; |
| 93 | f.write_str(" ")?; |
| 94 | f.write_str($abbrev) |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | newtype_f64!(Percent, "percent", "%"); |
| 101 | newtype_f64!(Picometer, "picometers", "pm"); |
| 102 | newtype_f64!(KiloJoulePerMole, "kilojoules per mole", "kJ/mol"); |
| 103 | newtype_f64!(Kelvin, "Kelvin", "K"); |
| 104 | newtype_f64!( |
| 105 | GramPerCubicCentimeter, |
| 106 | "grams per cubic centimeter", |
| 107 | "g/cm³" |
| 108 | ); |
| 109 | newtype_f64!(Electronvolt, "electronvolts", "eV"); |