#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{Measurement, PhysicalQuantity, UnitOfMeasure};
mod constants {
pub const IN_HG_IN_PA: f32 = 3386.39;
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(C)]
pub enum PressureUnit {
InchesOfMercury,
Hektopascal,
Pascal,
}
impl UnitOfMeasure<f32> for PressureUnit {
fn quantity() -> PhysicalQuantity {
PhysicalQuantity::Pressure
}
fn si() -> Self {
Self::Pascal
}
fn symbol(&self) -> &'static str {
match self {
Self::InchesOfMercury => "inHg",
Self::Hektopascal => "hPa",
Self::Pascal => "Pa",
}
}
fn from_si(value: f32, to: &Self) -> f32 {
match to {
Self::InchesOfMercury => value / constants::IN_HG_IN_PA,
Self::Hektopascal => value / 100.0,
Self::Pascal => value,
}
}
fn to_si(&self, value: &f32) -> f32 {
match self {
Self::InchesOfMercury => value * constants::IN_HG_IN_PA,
Self::Hektopascal => value * 100.0,
Self::Pascal => *value,
}
}
}
pub type Pressure = Measurement<f32, PressureUnit>;
impl Pressure {
pub const STD: Self = Self::h_pa(1013.25);
pub const fn in_hg(value: f32) -> Self {
Measurement {
value,
unit: PressureUnit::InchesOfMercury,
}
}
pub const fn h_pa(value: f32) -> Self {
Measurement {
value,
unit: PressureUnit::Hektopascal,
}
}
pub const fn pa(value: f32) -> Self {
Measurement {
value,
unit: PressureUnit::Pascal,
}
}
}