unit_converter_lib/units/
mass.rs1use std::slice::Iter;
2
3#[derive(Clone, Copy)]
4pub enum UNITS {
5 Milligram,
6 Gram,
7 Kilogram,
8 Tonne,
9 ImperialTon,
10 UsTon,
11 Pound,
12 Ounce,
13}
14pub enum UnitContainer {
15 Milligram(Milligram),
16 Gram(Gram),
17 Kilogram(Kilogram),
18 Tonne(Tonne),
19 ImperialTon(ImperialTon),
20 UsTon(UsTon),
21 Pound(Pound),
22 Ounce(Ounce),
23}
24
25impl UnitContainer {
26 pub fn new(unit: UNITS, value: f64) -> Self {
27 match unit {
28 UNITS::Milligram => Self::Milligram(Milligram(value)),
29 UNITS::Gram => Self::Gram(Gram(value)),
30 UNITS::Kilogram => Self::Kilogram(Kilogram(value)),
31 UNITS::Tonne => Self::Tonne(Tonne(value)),
32 UNITS::ImperialTon => Self::ImperialTon(ImperialTon(value)),
33 UNITS::UsTon => Self::UsTon(UsTon(value)),
34 UNITS::Pound => Self::Pound(Pound(value)),
35 UNITS::Ounce => Self::Ounce(Ounce(value)),
36 }
37 }
38}
39pub struct Milligram(pub f64);
40pub struct Gram(pub f64);
41pub struct Kilogram(pub f64);
42pub struct Tonne(pub f64);
43pub struct ImperialTon(pub f64);
44pub struct UsTon(pub f64);
45pub struct Pound(pub f64);
46pub struct Ounce(pub f64);
47
48mod conversions;
49
50impl UNITS {
51 pub fn iterator() -> Iter<'static, UNITS> {
52 use UNITS::*;
53 static DIRECTIONS: [UNITS;8] = [Milligram,Gram,Kilogram,Tonne,ImperialTon,UsTon,Pound,Ounce];
54 DIRECTIONS.iter()
55 }
56}
57impl super::Unit for UNITS {
58 fn by_name(name: &str) -> Self {
59 match name {
60 "milligram" => Self::Milligram,
61 "gram" => Self::Gram,
62 "kilogram" => Self::Kilogram,
63 "tonne" => Self::Tonne,
64 "imperialton" => Self::ImperialTon,
65 "uston" => Self::UsTon,
66 "pound" => Self::Pound,
67 "ounce" => Self::Ounce,
68 &_ => panic!("no unit with name '{name}'"),
69 }
70 }
71 fn to_string(&self) -> String {
72 match self {
73 Self::Milligram => String::from("Milligram"),
74 Self::Gram => String::from("Gram"),
75 Self::Kilogram => String::from("Kilogram"),
76 Self::Tonne => String::from("Tonne"),
77 Self::ImperialTon => String::from("ImperialTon"),
78 Self::UsTon => String::from("UsTon"),
79 Self::Pound => String::from("Pound"),
80 Self::Ounce => String::from("Ounce"),
81 }
82 }
83 fn get_type(n: u32) -> Self {
84 use UNITS::*;
85 static DIRECTIONS: [UNITS;8] = [Milligram,Gram,Kilogram,Tonne,ImperialTon,UsTon,Pound,Ounce];
86 DIRECTIONS[n as usize]
87 }
88}