Skip to content

Commit d34148f

Browse files
author
James O'Cull
committed
Added weight conversion. Doesn't have tests or docs yet.
1 parent 9d9caca commit d34148f

File tree

4 files changed

+167
-19
lines changed

4 files changed

+167
-19
lines changed

src/length.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,56 +25,56 @@ pub struct Length {
2525

2626
impl Length {
2727
// Inputs, metric
28-
pub fn from_meters(meters: f64) -> Length {
28+
pub fn from_meters(meters: f64) -> Self {
2929
Length { meters: meters }
3030
}
3131

32-
pub fn from_nanometers(nanometers: f64) -> Length {
32+
pub fn from_nanometers(nanometers: f64) -> Self {
3333
Self::from_meters(nanometers / METER_NANOMETER_FACTOR)
3434
}
3535

36-
pub fn from_micrometers(micrometers: f64) -> Length {
36+
pub fn from_micrometers(micrometers: f64) -> Self {
3737
Self::from_meters(micrometers / METER_MICROMETER_FACTOR)
3838
}
3939

40-
pub fn from_millimeters(millimeters: f64) -> Length {
40+
pub fn from_millimeters(millimeters: f64) -> Self {
4141
Self::from_meters(millimeters / METER_MILLIMETER_FACTOR)
4242
}
4343

44-
pub fn from_centimeters(centimeters: f64) -> Length {
44+
pub fn from_centimeters(centimeters: f64) -> Self {
4545
Self::from_meters(centimeters / METER_CENTIMETER_FACTOR)
4646
}
4747

48-
pub fn from_decameters(decameters: f64) -> Length {
48+
pub fn from_decameters(decameters: f64) -> Self {
4949
Self::from_meters(decameters / METER_DECAMETER_FACTOR)
5050
}
5151

52-
pub fn from_hectometers(hectometers: f64) -> Length {
52+
pub fn from_hectometers(hectometers: f64) -> Self {
5353
Self::from_meters(hectometers / METER_HECTOMETER_FACTOR)
5454
}
5555

56-
pub fn from_kilometers(kilometers: f64) -> Length {
56+
pub fn from_kilometers(kilometers: f64) -> Self {
5757
Self::from_meters(kilometers / METER_KILOMETER_FACTOR)
5858
}
5959

6060
// Inputs, imperial
61-
pub fn from_inches(inches: f64) -> Length {
61+
pub fn from_inches(inches: f64) -> Self {
6262
Self::from_meters(inches / METER_INCH_FACTOR)
6363
}
6464

65-
pub fn from_feet(feet: f64) -> Length {
65+
pub fn from_feet(feet: f64) -> Self {
6666
Self::from_meters(feet / METER_FEET_FACTOR)
6767
}
6868

69-
pub fn from_yards(yards: f64) -> Length {
69+
pub fn from_yards(yards: f64) -> Self {
7070
Self::from_meters(yards / METER_YARD_FACTOR)
7171
}
7272

73-
pub fn from_furlongs(furlongs: f64) -> Length {
73+
pub fn from_furlongs(furlongs: f64) -> Self {
7474
Self::from_meters(furlongs / METER_FURLONG_FACTOR)
7575
}
7676

77-
pub fn from_miles(miles: f64) -> Length {
77+
pub fn from_miles(miles: f64) -> Self {
7878
Self::from_meters(miles / METER_MILE_FACTOR)
7979
}
8080

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub mod measurement;
3838
/// let meters = football_field.as_meters();
3939
/// println!("There are {} meters in a football field.", meters);
4040
/// ```
41-
4241
#[allow(dead_code)]
4342
pub mod length;
4443

@@ -53,10 +52,14 @@ pub mod length;
5352
/// let fahrenheit = boiling_water.as_fahrenheit();
5453
/// println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
5554
/// ```
56-
5755
#[allow(dead_code)]
5856
pub mod temperature;
5957

58+
/// TODO: Provide docs and examples for this.
59+
///
60+
#[allow(dead_code)]
61+
pub mod weight;
62+
6063
// Include when running tests, but don't export them
6164
#[cfg(test)]
6265
mod tests;

src/temperature.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ pub struct Temperature {
77
}
88

99
impl Temperature {
10-
pub fn from_kelvin(kelvin: f64) -> Temperature {
10+
pub fn from_kelvin(kelvin: f64) -> Self {
1111
Temperature { kelvin: kelvin }
1212
}
1313

14-
pub fn from_celsius(celsius: f64) -> Temperature {
14+
pub fn from_celsius(celsius: f64) -> Self {
1515
Self::from_kelvin(celsius + 273.15)
1616
}
1717

18-
pub fn from_fahrenheit(fahrenheit: f64) -> Temperature {
18+
pub fn from_fahrenheit(fahrenheit: f64) -> Self {
1919
Self::from_kelvin((fahrenheit - 32.0) / 1.8 + 273.15)
2020
}
2121

22-
pub fn from_rankine(rankine: f64) -> Temperature {
22+
pub fn from_rankine(rankine: f64) -> Self {
2323
Self::from_kelvin((rankine - 491.67) / 1.8 + 273.15)
2424
}
2525

src/weight.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)