Skip to content

Commit 7d7f7e9

Browse files
author
James O'Cull
committed
Proper organization and tests. Added equality and comparison traits.
1 parent 93d8ad3 commit 7d7f7e9

File tree

7 files changed

+346
-263
lines changed

7 files changed

+346
-263
lines changed

src/length/consts.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Constants, metric
2+
pub const METER_NANOMETER_FACTOR: f64 = 1000000000.0;
3+
pub const METER_MICROMETER_FACTOR: f64 = 1000000.0;
4+
pub const METER_MILLIMETER_FACTOR: f64 = 1000.0;
5+
pub const METER_CENTIMETER_FACTOR: f64 = 100.0;
6+
pub const METER_DECAMETER_FACTOR: f64 = 0.1;
7+
pub const METER_HECTOMETER_FACTOR: f64 = 0.01;
8+
pub const METER_KILOMETER_FACTOR: f64 = 0.001;
9+
10+
// Constants, imperial
11+
pub const METER_INCH_FACTOR: f64 = 39.3700787402;
12+
pub const METER_FEET_FACTOR: f64 = 3.28083989501;
13+
pub const METER_YARD_FACTOR: f64 = 1.09361329834;
14+
pub const METER_FURLONG_FACTOR: f64 = 0.0049709695379;
15+
pub const METER_MILE_FACTOR: f64 = 0.000621371192237;

src/length/mod.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
pub mod consts;
2+
mod traits;
3+
4+
use self::consts::*;
5+
6+
#[derive(Copy, Clone, Debug)]
7+
pub struct Length {
8+
meters: f64
9+
}
10+
11+
impl Length {
12+
// Inputs, metric
13+
pub fn from_meters(meters: f64) -> Length {
14+
Length { meters: meters }
15+
}
16+
17+
pub fn from_nanometers(nanometers: f64) -> Length {
18+
Self::from_meters(nanometers / METER_NANOMETER_FACTOR)
19+
}
20+
21+
pub fn from_micrometers(micrometers: f64) -> Length {
22+
Self::from_meters(micrometers / METER_MICROMETER_FACTOR)
23+
}
24+
25+
pub fn from_millimeters(millimeters: f64) -> Length {
26+
Self::from_meters(millimeters / METER_MILLIMETER_FACTOR)
27+
}
28+
29+
pub fn from_centimeters(centimeters: f64) -> Length {
30+
Self::from_meters(centimeters / METER_CENTIMETER_FACTOR)
31+
}
32+
33+
pub fn from_decameters(decameters: f64) -> Length {
34+
Self::from_meters(decameters / METER_DECAMETER_FACTOR)
35+
}
36+
37+
pub fn from_hectometers(hectometers: f64) -> Length {
38+
Self::from_meters(hectometers / METER_HECTOMETER_FACTOR)
39+
}
40+
41+
pub fn from_kilometers(kilometers: f64) -> Length {
42+
Self::from_meters(kilometers / METER_KILOMETER_FACTOR)
43+
}
44+
45+
// Inputs, imperial
46+
pub fn from_inches(inches: f64) -> Length {
47+
Self::from_meters(inches / METER_INCH_FACTOR)
48+
}
49+
50+
pub fn from_feet(feet: f64) -> Length {
51+
Self::from_meters(feet / METER_FEET_FACTOR)
52+
}
53+
54+
pub fn from_yards(yards: f64) -> Length {
55+
Self::from_meters(yards / METER_YARD_FACTOR)
56+
}
57+
58+
pub fn from_furlongs(furlongs: f64) -> Length {
59+
Self::from_meters(furlongs / METER_FURLONG_FACTOR)
60+
}
61+
62+
pub fn from_miles(miles: f64) -> Length {
63+
Self::from_meters(miles / METER_MILE_FACTOR)
64+
}
65+
66+
// Outputs, metric
67+
pub fn as_nanometers(&self) -> f64 {
68+
self.meters * METER_NANOMETER_FACTOR
69+
}
70+
71+
pub fn as_micrometers(&self) -> f64 {
72+
self.meters * METER_MICROMETER_FACTOR
73+
}
74+
75+
pub fn as_millimeters(&self) -> f64 {
76+
self.meters * METER_MILLIMETER_FACTOR
77+
}
78+
79+
pub fn as_centimeters(&self) -> f64 {
80+
self.meters * METER_CENTIMETER_FACTOR
81+
}
82+
83+
pub fn as_meters(&self) -> f64 {
84+
self.meters
85+
}
86+
87+
pub fn as_decameters(&self) -> f64 {
88+
self.meters * METER_DECAMETER_FACTOR
89+
}
90+
91+
pub fn as_hectometer(&self) -> f64 {
92+
self.meters * METER_HECTOMETER_FACTOR
93+
}
94+
95+
pub fn as_kilometers(&self) -> f64 {
96+
self.meters * METER_KILOMETER_FACTOR
97+
}
98+
99+
// Outputs, imperial
100+
pub fn as_inches(&self) -> f64 {
101+
self.meters * METER_INCH_FACTOR
102+
}
103+
104+
pub fn as_feet(&self) -> f64 {
105+
self.meters * METER_FEET_FACTOR
106+
}
107+
108+
pub fn as_yards(&self) -> f64 {
109+
self.meters * METER_YARD_FACTOR
110+
}
111+
112+
pub fn as_furlongs(&self) -> f64 {
113+
self.meters * METER_FURLONG_FACTOR
114+
}
115+
116+
pub fn as_miles(&self) -> f64 {
117+
self.meters * METER_MILE_FACTOR
118+
}
119+
}

src/length/traits.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::ops::{Add,Sub,Div,Mul};
2+
use std::cmp::{Eq, PartialEq};
3+
use std::cmp::{PartialOrd, Ordering};
4+
use super::Length;
5+
6+
impl Add for Length {
7+
type Output = Length;
8+
9+
fn add(self, rhs: Length) -> Length {
10+
Length::from_meters(self.meters + rhs.meters)
11+
}
12+
}
13+
14+
impl Sub for Length {
15+
type Output = Length;
16+
17+
fn sub(self, rhs: Length) -> Length {
18+
Length::from_meters(self.meters - rhs.meters)
19+
}
20+
}
21+
22+
impl Div for Length {
23+
type Output = Length;
24+
25+
fn div(self, rhs: Length) -> Length {
26+
Length::from_meters(self.meters / rhs.meters)
27+
}
28+
}
29+
30+
impl Mul for Length {
31+
type Output = Length;
32+
33+
fn mul(self, rhs: Length) -> Length {
34+
Length::from_meters(self.meters * rhs.meters)
35+
}
36+
}
37+
38+
impl Eq for Length { }
39+
impl PartialEq for Length {
40+
fn eq(&self, other: &Length) -> bool {
41+
self.meters == other.meters
42+
}
43+
}
44+
45+
impl PartialOrd for Length {
46+
fn partial_cmp(&self, other: &Length) -> Option<Ordering> {
47+
self.meters.partial_cmp(&other.meters)
48+
}
49+
}

src/lib.rs

Lines changed: 3 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,5 @@
1+
#[cfg(test)]
12
mod tests;
23

3-
use std::ops::{Add,Sub,Div,Mul};
4-
5-
#[derive(Copy, Clone, Debug)]
6-
pub struct Length {
7-
meters: f64
8-
}
9-
10-
// Constants, metric
11-
const METER_NANOMETER_FACTOR: f64 = 1000000000.0;
12-
const METER_MICROMETER_FACTOR: f64 = 1000000.0;
13-
const METER_MILLIMETER_FACTOR: f64 = 1000.0;
14-
const METER_CENTIMETER_FACTOR: f64 = 100.0;
15-
const METER_DECAMETER_FACTOR: f64 = 0.1;
16-
const METER_HECTOMETER_FACTOR: f64 = 0.01;
17-
const METER_KILOMETER_FACTOR: f64 = 0.001;
18-
19-
// Constants, imperial
20-
const METER_INCH_FACTOR: f64 = 39.3700787402;
21-
const METER_FEET_FACTOR: f64 = 3.28083989501;
22-
const METER_YARD_FACTOR: f64 = 1.09361329834;
23-
const METER_FURLONG_FACTOR: f64 = 0.0049709695379;
24-
const METER_MILE_FACTOR: f64 = 0.000621371192237;
25-
26-
impl Length {
27-
// Inputs, metric
28-
pub fn from_meters(meters: f64) -> Length {
29-
Length { meters: meters }
30-
}
31-
32-
fn from_nanometers(nanometers: f64) -> Length {
33-
Self::from_meters(nanometers / METER_NANOMETER_FACTOR)
34-
}
35-
36-
fn from_micrometers(micrometers: f64) -> Length {
37-
Self::from_meters(micrometers / METER_MICROMETER_FACTOR)
38-
}
39-
40-
fn from_millimeters(millimeters: f64) -> Length {
41-
Self::from_meters(millimeters / METER_MILLIMETER_FACTOR)
42-
}
43-
44-
fn from_centimeters(centimeters: f64) -> Length {
45-
Self::from_meters(centimeters / METER_CENTIMETER_FACTOR)
46-
}
47-
48-
fn from_decameters(decameters: f64) -> Length {
49-
Self::from_meters(decameters / METER_DECAMETER_FACTOR)
50-
}
51-
52-
fn from_hectometers(hectometers: f64) -> Length {
53-
Self::from_meters(hectometers / METER_HECTOMETER_FACTOR)
54-
}
55-
56-
fn from_kilometers(kilometers: f64) -> Length {
57-
Self::from_meters(kilometers / METER_KILOMETER_FACTOR)
58-
}
59-
60-
// Inputs, imperial
61-
fn from_inches(inches: f64) -> Length {
62-
Self::from_meters(inches / METER_INCH_FACTOR)
63-
}
64-
65-
fn from_feet(feet: f64) -> Length {
66-
Self::from_meters(feet / METER_FEET_FACTOR)
67-
}
68-
69-
fn from_yards(yards: f64) -> Length {
70-
Self::from_meters(yards / METER_YARD_FACTOR)
71-
}
72-
73-
fn from_furlongs(furlongs: f64) -> Length {
74-
Self::from_meters(furlongs / METER_FURLONG_FACTOR)
75-
}
76-
77-
fn from_miles(miles: f64) -> Length {
78-
Self::from_meters(miles / METER_MILE_FACTOR)
79-
}
80-
81-
// Outputs, metric
82-
fn as_nanometers(&self) -> f64 {
83-
self.meters * METER_NANOMETER_FACTOR
84-
}
85-
86-
fn as_micrometers(&self) -> f64 {
87-
self.meters * METER_MICROMETER_FACTOR
88-
}
89-
90-
fn as_millimeters(&self) -> f64 {
91-
self.meters * METER_MILLIMETER_FACTOR
92-
}
93-
94-
fn as_centimeters(&self) -> f64 {
95-
self.meters * METER_CENTIMETER_FACTOR
96-
}
97-
98-
fn as_meters(&self) -> f64 {
99-
self.meters
100-
}
101-
102-
fn as_decameters(&self) -> f64 {
103-
self.meters * METER_DECAMETER_FACTOR
104-
}
105-
106-
fn as_hectometer(&self) -> f64 {
107-
self.meters * METER_HECTOMETER_FACTOR
108-
}
109-
110-
fn as_kilometers(&self) -> f64 {
111-
self.meters * METER_KILOMETER_FACTOR
112-
}
113-
114-
// Outputs, imperial
115-
fn as_inches(&self) -> f64 {
116-
self.meters * METER_INCH_FACTOR
117-
}
118-
119-
fn as_feet(&self) -> f64 {
120-
self.meters * METER_FEET_FACTOR
121-
}
122-
123-
fn as_yards(&self) -> f64 {
124-
self.meters * METER_YARD_FACTOR
125-
}
126-
127-
fn as_furlongs(&self) -> f64 {
128-
self.meters * METER_FURLONG_FACTOR
129-
}
130-
131-
fn as_miles(&self) -> f64 {
132-
self.meters * METER_MILE_FACTOR
133-
}
134-
}
135-
136-
impl Add for Length {
137-
type Output = Length;
138-
139-
fn add(self, rhs: Length) -> Length {
140-
Length::from_meters(self.meters + rhs.meters)
141-
}
142-
}
143-
144-
impl Sub for Length {
145-
type Output = Length;
146-
147-
fn sub(self, rhs: Length) -> Length {
148-
Length::from_meters(self.meters - rhs.meters)
149-
}
150-
}
151-
152-
impl Div for Length {
153-
type Output = Length;
154-
155-
fn div(self, rhs: Length) -> Length {
156-
Length::from_meters(self.meters / rhs.meters)
157-
}
158-
}
159-
160-
impl Mul for Length {
161-
type Output = Length;
162-
163-
fn mul(self, rhs: Length) -> Length {
164-
Length::from_meters(self.meters * rhs.meters)
165-
}
166-
}
4+
#[allow(dead_code)]
5+
pub mod length;

0 commit comments

Comments
 (0)