Skip to content

Commit 9bbee41

Browse files
committed
feat: length_conversion implemented
1 parent 274ca13 commit 9bbee41

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/// Author : https://github.com/ali77gh
2+
/// Conversion of length units.
3+
///
4+
/// Available Units:
5+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Millimeter
6+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Centimeter
7+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Meter
8+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Kilometer
9+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Inch
10+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Foot
11+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Yard
12+
/// -> Wikipedia reference: https://en.wikipedia.org/wiki/Mile
13+
14+
/// Universal Units on Length
15+
pub enum LengthUnit {
16+
Millimeter,
17+
Centimeter,
18+
Meter,
19+
Kilometer,
20+
Inch,
21+
Foot,
22+
Yard,
23+
Mile,
24+
}
25+
26+
/// Private methods
27+
/// This is a n*n problem
28+
/// It's gonna be 56 functions or 56 cases in match statement
29+
/// It's hard to write code for every unit to unit so I solve this problem
30+
/// by converting input to meter and than convert it to output
31+
impl LengthUnit {
32+
/// This function give you a number (let's call it n)
33+
/// So if you multiple a value in this unit to n you will get meters
34+
///
35+
/// m * in-unit = meter
36+
fn get_unit_to_meter_multiplier(&self) -> f64 {
37+
match self {
38+
LengthUnit::Millimeter => 0.001,
39+
LengthUnit::Centimeter => 0.01,
40+
LengthUnit::Meter => 1.0,
41+
LengthUnit::Kilometer => 1000.0,
42+
LengthUnit::Inch => 0.0254,
43+
LengthUnit::Foot => 0.3048,
44+
LengthUnit::Yard => 0.9144,
45+
LengthUnit::Mile => 1609.34,
46+
}
47+
}
48+
49+
/// This function give you a number (let's call it n)
50+
/// So if you multiple a value in meters to n you will get unit
51+
///
52+
/// m * meter = in-unit
53+
fn get_unit_from_meter_multiplier(&self) -> f64 {
54+
1.0 / self.get_unit_to_meter_multiplier()
55+
}
56+
}
57+
58+
/// This function will convert a value in unit of [from] to value in unit of [to]
59+
/// by first converting it to meter and than convert it to destination unit
60+
pub fn length_conversion(input: f64, from: LengthUnit, to: LengthUnit) -> f64 {
61+
input * from.get_unit_to_meter_multiplier() * to.get_unit_from_meter_multiplier()
62+
}
63+
64+
#[cfg(test)]
65+
mod length_conversion_tests {
66+
use super::LengthUnit::*;
67+
use super::*;
68+
69+
#[test]
70+
fn meter_to_other() {
71+
assert_eq!(length_conversion(4f64, Meter, Millimeter), 4000.0);
72+
assert_eq!(length_conversion(4f64, Meter, Foot), 13.123359580052492);
73+
assert_eq!(length_conversion(1.0, Meter, Kilometer), 0.001);
74+
}
75+
76+
#[test]
77+
fn other_to_meter() {
78+
assert_eq!(length_conversion(4f64, Millimeter, Meter), 0.004);
79+
assert_eq!(length_conversion(2.0, Foot, Meter), 0.6096);
80+
assert_eq!(length_conversion(1.0, Inch, Meter), 0.0254);
81+
assert_eq!(length_conversion(4.0, Yard, Meter), 3.6576);
82+
assert_eq!(length_conversion(3.0, Foot, Meter), 0.9144000000000001);
83+
}
84+
85+
#[test]
86+
fn other_to_other() {
87+
// ---------------
88+
assert_eq!(length_conversion(1.0, Kilometer, Inch), 39370.07874015748);
89+
assert_eq!(length_conversion(3.0, Kilometer, Mile), 1.8641182099494205);
90+
assert_eq!(length_conversion(4.0, Foot, Yard), 1.3333333333333335);
91+
assert_eq!(length_conversion(2.0, Inch, Mile), 3.156573502181019e-5);
92+
assert_eq!(length_conversion(2.0, Centimeter, Millimeter), 20.0);
93+
assert_eq!(
94+
length_conversion(2.0, Centimeter, Yard),
95+
0.021872265966754158
96+
);
97+
assert_eq!(length_conversion(4.0, Yard, Kilometer), 0.0036576);
98+
assert_eq!(length_conversion(3.0, Foot, Inch), 36.00000000000001);
99+
assert_eq!(length_conversion(4.0, Mile, Kilometer), 6.43736);
100+
assert_eq!(length_conversion(2.0, Mile, Inch), 126719.68503937007);
101+
assert_eq!(length_conversion(3.0, Millimeter, Centimeter), 0.3);
102+
assert_eq!(
103+
length_conversion(3.0, Millimeter, Inch),
104+
0.11811023622047245
105+
);
106+
}
107+
}

src/conversions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod decimal_to_binary;
44
mod decimal_to_hexadecimal;
55
mod hexadecimal_to_binary;
66
mod hexadecimal_to_decimal;
7+
mod length_conversion;
78
mod octal_to_binary;
89
mod octal_to_decimal;
910
pub use self::binary_to_decimal::binary_to_decimal;
@@ -12,5 +13,6 @@ pub use self::decimal_to_binary::decimal_to_binary;
1213
pub use self::decimal_to_hexadecimal::decimal_to_hexadecimal;
1314
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
1415
pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal;
16+
pub use self::length_conversion::length_conversion;
1517
pub use self::octal_to_binary::octal_to_binary;
1618
pub use self::octal_to_decimal::octal_to_decimal;

0 commit comments

Comments
 (0)