|
| 1 | +use super::measurement::*; |
| 2 | + |
| 3 | +/// The `Weight` struct can be used to deal with weights in a common way. |
| 4 | +#[derive(Copy, Clone, Debug)] |
| 5 | +pub struct Weight { |
| 6 | + kilograms: f64 |
| 7 | +} |
| 8 | + |
| 9 | +impl Weight { |
| 10 | + // Inputs, metric |
| 11 | + pub fn from_kilograms(kilograms: f64) -> Self { |
| 12 | + Weight { kilograms: kilograms } |
| 13 | + } |
| 14 | + |
| 15 | + pub fn from_micrograms(micrograms: f64) -> Self { |
| 16 | + Self::from_kilograms(micrograms / 1000000000.0) |
| 17 | + } |
| 18 | + |
| 19 | + pub fn from_milligrams(milligrams: f64) -> Self { |
| 20 | + Self::from_kilograms(milligrams / 1000000.0) |
| 21 | + } |
| 22 | + |
| 23 | + pub fn from_carats(carats: f64) -> Self { |
| 24 | + Self::from_kilograms(carats / 5000.0) |
| 25 | + } |
| 26 | + |
| 27 | + pub fn from_grams(grams: f64) -> Self { |
| 28 | + Self::from_kilograms(grams / 1000.0) |
| 29 | + } |
| 30 | + |
| 31 | + pub fn from_metric_tons(metric_tons: f64) -> Self { |
| 32 | + Self::from_kilograms(metric_tons * 1000.0) |
| 33 | + } |
| 34 | + |
| 35 | + // Inputs, imperial |
| 36 | + pub fn from_grains(grains: f64) -> Self { |
| 37 | + Self::from_kilograms(grains / 15432.358) |
| 38 | + } |
| 39 | + |
| 40 | + pub fn from_pennyweights(pennyweights: f64) -> Self { |
| 41 | + Self::from_kilograms(pennyweights / 643.01493) |
| 42 | + } |
| 43 | + |
| 44 | + pub fn from_ounces(ounces: f64) -> Self { |
| 45 | + Self::from_kilograms(ounces / 35.273962) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn from_troy_ounces(troy_ounces: f64) -> Self { |
| 49 | + Self::from_kilograms(troy_ounces / 32.150747) |
| 50 | + } |
| 51 | + |
| 52 | + pub fn from_pounds(pounds: f64) -> Self { |
| 53 | + Self::from_kilograms(pounds / 2.2046228) |
| 54 | + } |
| 55 | + |
| 56 | + pub fn from_troy_pounds(troy_pounds: f64) -> Self { |
| 57 | + Self::from_kilograms(troy_pounds / 2.6792289) |
| 58 | + } |
| 59 | + |
| 60 | + pub fn from_stones(stones: f64) -> Self { |
| 61 | + Self::from_kilograms(stones / 0.15747304) |
| 62 | + } |
| 63 | + |
| 64 | + pub fn from_short_tons(short_tons: f64) -> Self { |
| 65 | + Self::from_short_tons(short_tons * 907.18475) |
| 66 | + } |
| 67 | + |
| 68 | + pub fn from_long_tons(long_tons: f64) -> Self { |
| 69 | + Self::from_kilograms(long_tons * 1016.0469) |
| 70 | + } |
| 71 | + |
| 72 | + // Outputs, metric |
| 73 | + pub fn as_micrograms(&self) -> f64 { |
| 74 | + self.kilograms * 1000000000.0 |
| 75 | + } |
| 76 | + |
| 77 | + pub fn as_milligrams(&self) -> f64 { |
| 78 | + self.kilograms * 1000000.0 |
| 79 | + } |
| 80 | + |
| 81 | + pub fn as_carats(&self) -> f64 { |
| 82 | + self.kilograms * 5000.0 |
| 83 | + } |
| 84 | + |
| 85 | + pub fn as_grams(&self) -> f64 { |
| 86 | + self.kilograms * 1000.0 |
| 87 | + } |
| 88 | + |
| 89 | + pub fn as_kilograms(&self) -> f64 { |
| 90 | + self.kilograms |
| 91 | + } |
| 92 | + |
| 93 | + pub fn as_metric_tons(&self) -> f64 { |
| 94 | + self.kilograms / 1000.0 |
| 95 | + } |
| 96 | + |
| 97 | + // Outputs, imperial |
| 98 | + pub fn as_grains(&self) -> f64 { |
| 99 | + self.kilograms * 15432.358 |
| 100 | + } |
| 101 | + |
| 102 | + pub fn as_pennyweights(&self) -> f64 { |
| 103 | + self.kilograms * 643.01493 |
| 104 | + } |
| 105 | + |
| 106 | + pub fn as_ounces(&self) -> f64 { |
| 107 | + self.kilograms * 35.273962 |
| 108 | + } |
| 109 | + |
| 110 | + pub fn as_pounds(&self) -> f64 { |
| 111 | + self.kilograms * 2.2046228 |
| 112 | + } |
| 113 | + |
| 114 | + pub fn as_troy_ounces(&self) -> f64 { |
| 115 | + self.kilograms * 32.150747 |
| 116 | + } |
| 117 | + |
| 118 | + pub fn as_troy_pounds(&self) -> f64 { |
| 119 | + self.kilograms * 2.6792289 |
| 120 | + } |
| 121 | + |
| 122 | + pub fn as_stones(&self) -> f64 { |
| 123 | + self.kilograms * 0.15747304 |
| 124 | + } |
| 125 | + |
| 126 | + pub fn as_short_tons(&self) -> f64 { |
| 127 | + self.kilograms / 907.18475 |
| 128 | + } |
| 129 | + |
| 130 | + pub fn as_long_tons(&self) -> f64 { |
| 131 | + self.kilograms / 1016.0469 |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl Measurement for Weight { |
| 136 | + fn get_base_units(&self) -> f64 { |
| 137 | + self.kilograms |
| 138 | + } |
| 139 | + |
| 140 | + fn from_base_units(units: f64) -> Self { |
| 141 | + Self::from_kilograms(units) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +implement_measurement! { Weight } |
0 commit comments