eng_units/units/
length_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::AmountOfSubstanceUnit;
18use crate::units::ElectricCurrentUnit;
19use crate::units::IsEngUnitType;
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 LengthUnit {
27    Meter,
28    Foot,
29    None,
30}
31
32impl LengthUnit {
33    pub fn conversion_factor(from: &LengthUnit, to: &LengthUnit) -> f64 {
34        match from {
35            Self::Meter => match to {
36                Self::Meter => 1.0,
37                Self::Foot => 3.28084,
38                Self::None => 1.0,
39            },
40            Self::Foot => match to {
41                Self::Meter => 1.0 / 3.28084,
42                Self::Foot => 1.0,
43                Self::None => 1.0,
44            },
45            Self::None => 1.0,
46        }
47    }
48
49    pub fn to_string(&self) -> &'static str {
50        match self {
51            LengthUnit::Meter => "m",
52            LengthUnit::Foot => "ft",
53            LengthUnit::None => "",
54        }
55    }
56}
57
58impl<
59        T: IsEngUnitType
60            + Into<AmountOfSubstanceUnit>
61            + Into<ElectricCurrentUnit>
62            + Into<LengthUnit>
63            + Into<LuminousIntensityUnit>
64            + Into<MassUnit>
65            + Into<TemperatureDeltaUnit>
66            + Into<TimeUnit>,
67    > From<&T> for LengthUnit
68{
69    fn from(value: &T) -> Self {
70        if T::is_amount_unit() {
71            value.into()
72        } else {
73            Self::None
74        }
75    }
76}
77impl IsEngUnitType for LengthUnit {
78    fn is_length_unit() -> bool {
79        true
80    }
81}
82impl From<AmountOfSubstanceUnit> for LengthUnit {
83    fn from(_: AmountOfSubstanceUnit) -> Self {
84        LengthUnit::None
85    }
86}
87impl From<ElectricCurrentUnit> for LengthUnit {
88    fn from(_: ElectricCurrentUnit) -> Self {
89        LengthUnit::None
90    }
91}
92impl From<LuminousIntensityUnit> for LengthUnit {
93    fn from(_: LuminousIntensityUnit) -> Self {
94        LengthUnit::None
95    }
96}
97impl From<MassUnit> for LengthUnit {
98    fn from(_: MassUnit) -> Self {
99        LengthUnit::None
100    }
101}
102impl From<TemperatureDeltaUnit> for LengthUnit {
103    fn from(_: TemperatureDeltaUnit) -> Self {
104        LengthUnit::None
105    }
106}
107impl From<TimeUnit> for LengthUnit {
108    fn from(_: TimeUnit) -> Self {
109        LengthUnit::None
110    }
111}