eng_units/units/
amount_of_substance_unit.rs

1// eng-units - engineering unit conversion and calculation library
2// Copyright (C) 2023 Frank Pereny
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17use crate::units::ElectricCurrentUnit;
18use crate::units::IsEngUnitType;
19use crate::units::LengthUnit;
20use crate::units::LuminousIntensityUnit;
21use crate::units::MassUnit;
22use crate::units::TemperatureDeltaUnit;
23use crate::units::TimeUnit;
24
25#[derive(Copy, Clone, Debug, PartialEq)]
26pub enum AmountOfSubstanceUnit {
27    Mol,
28    None,
29}
30
31impl AmountOfSubstanceUnit {
32    pub fn conversion_factor(from: &AmountOfSubstanceUnit, to: &AmountOfSubstanceUnit) -> f64 {
33        match from {
34            Self::Mol => match to {
35                Self::Mol => 1.0,
36                Self::None => 1.0,
37            },
38            Self::None => 1.0,
39        }
40    }
41
42    pub fn to_string(&self) -> &'static str {
43        match self {
44            AmountOfSubstanceUnit::Mol => "mol",
45            AmountOfSubstanceUnit::None => "",
46        }
47    }
48}
49
50impl IsEngUnitType for AmountOfSubstanceUnit {
51    fn is_amount_unit() -> bool {
52        true
53    }
54}
55impl<
56        T: IsEngUnitType
57            + Into<AmountOfSubstanceUnit>
58            + Into<ElectricCurrentUnit>
59            + Into<LengthUnit>
60            + Into<LuminousIntensityUnit>
61            + Into<MassUnit>
62            + Into<TemperatureDeltaUnit>
63            + Into<TimeUnit>,
64    > From<&T> for AmountOfSubstanceUnit
65{
66    fn from(value: &T) -> Self {
67        if T::is_amount_unit() {
68            value.into()
69        } else {
70            Self::None
71        }
72    }
73}
74impl From<ElectricCurrentUnit> for AmountOfSubstanceUnit {
75    fn from(_: ElectricCurrentUnit) -> Self {
76        AmountOfSubstanceUnit::None
77    }
78}
79impl From<LengthUnit> for AmountOfSubstanceUnit {
80    fn from(_: LengthUnit) -> Self {
81        AmountOfSubstanceUnit::None
82    }
83}
84impl From<LuminousIntensityUnit> for AmountOfSubstanceUnit {
85    fn from(_: LuminousIntensityUnit) -> Self {
86        AmountOfSubstanceUnit::None
87    }
88}
89impl From<MassUnit> for AmountOfSubstanceUnit {
90    fn from(_: MassUnit) -> Self {
91        AmountOfSubstanceUnit::None
92    }
93}
94impl From<TemperatureDeltaUnit> for AmountOfSubstanceUnit {
95    fn from(_: TemperatureDeltaUnit) -> Self {
96        AmountOfSubstanceUnit::None
97    }
98}
99impl From<TimeUnit> for AmountOfSubstanceUnit {
100    fn from(_: TimeUnit) -> Self {
101        AmountOfSubstanceUnit::None
102    }
103}