Skip to content

Commit 42a196d

Browse files
authored
Merge pull request #3 from thejpster/master
Added Pressure
2 parents 6c8c57a + d1cf85c commit 42a196d

14 files changed

+354
-226
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
target
22
Cargo.lock
3+
*.bk
4+

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
[package]
22
name = "measurements"
3-
version = "0.2.1"
4-
authors = ["James O'Cull <[email protected]>"]
5-
documentation = "https://jocull.github.io/rust-measurements/"
3+
version = "0.3.0"
4+
authors = ["James O'Cull <[email protected]>", "Jonathan Pallant <[email protected]"]
5+
documentation = "https://docs.rs/crate/measurements"
66
repository = "https://github.com/jocull/rust-measurements"
77
keywords = ["measurements", "lengths", "weights", "temperatures", "volumes"]
8-
description = "Handle metric, imperial, and other measurements with ease! Types: Length, Temperature, Weight, Volume"
8+
description = "Handle metric, imperial, and other measurements with ease! Types: Length, Temperature, Weight, Volume, Pressure"
99
license = "MIT"
10+
readme = "README.md"

README.md

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,51 @@ Conversions to and from different units are simple, and operator overrides allow
2222
- Temperature
2323
- Weight
2424
- Volume
25+
- Pressure
2526

2627
### Examples
2728

2829
In your Cargo.toml add the dependency...
2930

30-
```
31+
```toml
3132
[dependencies]
32-
measurements = "^0.2.1"
33+
measurements = "^0.3.0"
3334
```
3435

3536
In your code...
3637

3738
```rust
3839
extern crate measurements;
3940

40-
use measurements::{Length, Temperature, Weight, Volume};
41-
42-
// Lengths!
43-
let football_field = Length::from_yards(100.0);
44-
let meters = football_field.as_meters();
45-
println!("There are {} meters in a football field.", meters);
46-
47-
/// Temperatures!
48-
let boiling_water = Temperature::from_celsius(100.0);
49-
let fahrenheit = boiling_water.as_fahrenheit();
50-
println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
51-
52-
// Weights!
53-
let metric_ton = Weight::from_metric_tons(1.0);
54-
let united_states_tons = metric_ton.as_short_tons();
55-
let united_states_pounds = metric_ton.as_pounds();
56-
println!("One metric ton is {} U.S. tons - that's {} pounds!", united_states_tons, united_states_pounds);
57-
58-
// Volumes!
59-
let gallon = Volume::from_gallons(1.0);
60-
let pint = Volume::from_pints(1.0);
61-
let beers = gallon / pint;
62-
println!("A gallon of beer will pour {} pints!", beers);
41+
use measurements::{Length, Pressure, Temperature, Volume, Weight};
42+
43+
fn main() {
44+
// Lengths!
45+
let football_field = Length::from_yards(100.0);
46+
let meters = football_field.as_meters();
47+
println!("There are {} meters in a football field.", meters);
48+
49+
/// Temperatures!
50+
let boiling_water = Temperature::from_celsius(100.0);
51+
let fahrenheit = boiling_water.as_fahrenheit();
52+
println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
53+
54+
// Weights!
55+
let metric_ton = Weight::from_metric_tons(1.0);
56+
let united_states_tons = metric_ton.as_short_tons();
57+
let united_states_pounds = metric_ton.as_pounds();
58+
println!("One metric ton is {} U.S. tons - that's {} pounds!", united_states_tons, united_states_pounds);
59+
60+
// Volumes!
61+
let gallon = Volume::from_gallons(1.0);
62+
let pint = Volume::from_pints(1.0);
63+
let beers = gallon / pint;
64+
println!("A gallon of beer will pour {:.1} pints!", beers);
65+
66+
// Pressures!
67+
let atmosphere = Pressure::from_atmospheres(1.0);
68+
println!("Earth's atmosphere is usually {} psi", atmosphere.as_psi());
69+
}
6370
```
6471

6572
--------------------------------------

src/length.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,126 +18,126 @@ const METER_MILE_FACTOR: f64 = 0.000621371192237;
1818

1919
/// The `Length` struct can be used to deal with lengths in a common way.
2020
/// Common metric and imperial units are supported.
21-
///
21+
///
2222
/// # Example
23-
///
23+
///
2424
/// ```
2525
/// use measurements::Length;
26-
///
26+
///
2727
/// let football_field = Length::from_yards(100.0);
2828
/// let meters = football_field.as_meters();
2929
/// println!("There are {} meters in a football field.", meters);
3030
/// ```
3131
#[derive(Copy, Clone, Debug)]
3232
pub struct Length {
33-
meters: f64
33+
meters: f64,
3434
}
3535

3636
impl Length {
3737
// Inputs, metric
3838
pub fn from_meters(meters: f64) -> Self {
3939
Length { meters: meters }
4040
}
41-
41+
4242
pub fn from_nanometers(nanometers: f64) -> Self {
4343
Self::from_meters(nanometers / METER_NANOMETER_FACTOR)
4444
}
45-
45+
4646
pub fn from_micrometers(micrometers: f64) -> Self {
4747
Self::from_meters(micrometers / METER_MICROMETER_FACTOR)
4848
}
49-
49+
5050
pub fn from_millimeters(millimeters: f64) -> Self {
5151
Self::from_meters(millimeters / METER_MILLIMETER_FACTOR)
5252
}
53-
53+
5454
pub fn from_centimeters(centimeters: f64) -> Self {
5555
Self::from_meters(centimeters / METER_CENTIMETER_FACTOR)
5656
}
57-
57+
5858
pub fn from_decameters(decameters: f64) -> Self {
5959
Self::from_meters(decameters / METER_DECAMETER_FACTOR)
6060
}
61-
61+
6262
pub fn from_hectometers(hectometers: f64) -> Self {
6363
Self::from_meters(hectometers / METER_HECTOMETER_FACTOR)
6464
}
65-
65+
6666
pub fn from_kilometers(kilometers: f64) -> Self {
6767
Self::from_meters(kilometers / METER_KILOMETER_FACTOR)
6868
}
69-
69+
7070
// Inputs, imperial
7171
pub fn from_inches(inches: f64) -> Self {
7272
Self::from_meters(inches / METER_INCH_FACTOR)
7373
}
74-
74+
7575
pub fn from_feet(feet: f64) -> Self {
7676
Self::from_meters(feet / METER_FEET_FACTOR)
7777
}
78-
78+
7979
pub fn from_yards(yards: f64) -> Self {
8080
Self::from_meters(yards / METER_YARD_FACTOR)
8181
}
82-
82+
8383
pub fn from_furlongs(furlongs: f64) -> Self {
8484
Self::from_meters(furlongs / METER_FURLONG_FACTOR)
8585
}
86-
86+
8787
pub fn from_miles(miles: f64) -> Self {
8888
Self::from_meters(miles / METER_MILE_FACTOR)
8989
}
90-
90+
9191
// Outputs, metric
9292
pub fn as_nanometers(&self) -> f64 {
9393
self.meters * METER_NANOMETER_FACTOR
9494
}
95-
95+
9696
pub fn as_micrometers(&self) -> f64 {
9797
self.meters * METER_MICROMETER_FACTOR
9898
}
99-
99+
100100
pub fn as_millimeters(&self) -> f64 {
101101
self.meters * METER_MILLIMETER_FACTOR
102102
}
103-
103+
104104
pub fn as_centimeters(&self) -> f64 {
105105
self.meters * METER_CENTIMETER_FACTOR
106106
}
107-
107+
108108
pub fn as_meters(&self) -> f64 {
109109
self.meters
110110
}
111-
111+
112112
pub fn as_decameters(&self) -> f64 {
113113
self.meters * METER_DECAMETER_FACTOR
114114
}
115-
115+
116116
pub fn as_hectometer(&self) -> f64 {
117117
self.meters * METER_HECTOMETER_FACTOR
118118
}
119-
119+
120120
pub fn as_kilometers(&self) -> f64 {
121121
self.meters * METER_KILOMETER_FACTOR
122122
}
123-
123+
124124
// Outputs, imperial
125125
pub fn as_inches(&self) -> f64 {
126126
self.meters * METER_INCH_FACTOR
127127
}
128-
128+
129129
pub fn as_feet(&self) -> f64 {
130130
self.meters * METER_FEET_FACTOR
131131
}
132-
132+
133133
pub fn as_yards(&self) -> f64 {
134134
self.meters * METER_YARD_FACTOR
135135
}
136-
136+
137137
pub fn as_furlongs(&self) -> f64 {
138138
self.meters * METER_FURLONG_FACTOR
139139
}
140-
140+
141141
pub fn as_miles(&self) -> f64 {
142142
self.meters * METER_MILE_FACTOR
143143
}
@@ -147,10 +147,16 @@ impl Measurement for Length {
147147
fn get_base_units(&self) -> f64 {
148148
self.meters
149149
}
150-
150+
151151
fn from_base_units(units: f64) -> Self {
152152
Self::from_meters(units)
153153
}
154154
}
155155

156156
implement_measurement! { Length }
157+
158+
impl ::std::fmt::Display for Length {
159+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
160+
write!(f, "{:.1} m", self.as_meters())
161+
}
162+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub use weight::Weight;
1818
mod volume;
1919
pub use volume::Volume;
2020

21+
#[allow(dead_code)]
22+
mod pressure;
23+
pub use pressure::Pressure;
24+
2125
// Include when running tests, but don't export them
2226
#[cfg(test)]
2327
#[allow(dead_code)]

src/measurement.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/// The `Measurement` trait and the `implement_measurement!` macro
22
/// provides a common way for various measurements to be implemented.
3-
///
3+
///
44
/// # Example
55
/// ```
66
/// // Importing the `implement_measurement` macro from the external crate is important
77
/// #[macro_use]
88
/// extern crate measurements;
9-
///
9+
///
1010
/// use measurements::Measurement;
11-
///
11+
///
1212
/// struct Cubits {
1313
/// forearms: f64
1414
/// }
15-
///
15+
///
1616
/// impl Measurement for Cubits {
1717
/// fn get_base_units(&self) -> f64 {
1818
/// self.forearms
1919
/// }
20-
///
20+
///
2121
/// fn from_base_units(units: f64) -> Self {
2222
/// Cubits { forearms: units }
2323
/// }
@@ -42,59 +42,55 @@ macro_rules! implement_measurement {
4242
($($t:ty)*) => ($(
4343
impl ::std::ops::Add for $t {
4444
type Output = Self;
45-
45+
4646
fn add(self, rhs: Self) -> Self {
4747
Self::from_base_units(self.get_base_units() + rhs.get_base_units())
4848
}
4949
}
5050

5151
impl ::std::ops::Sub for $t {
5252
type Output = Self;
53-
53+
5454
fn sub(self, rhs: Self) -> Self {
5555
Self::from_base_units(self.get_base_units() - rhs.get_base_units())
5656
}
5757
}
5858

59-
///
60-
/// Dividing a `$t` by another `$` returns a ratio.
61-
///
59+
// Dividing a `$t` by another `$` returns a ratio.
60+
//
6261
impl ::std::ops::Div<$t> for $t {
6362
type Output = f64;
64-
63+
6564
fn div(self, rhs: Self) -> f64 {
6665
self.get_base_units() / rhs.get_base_units()
6766
}
6867
}
6968

70-
///
71-
/// Dividing a `$` by a factor returns a new portion of the measurement.
72-
///
69+
// Dividing a `$` by a factor returns a new portion of the measurement.
70+
//
7371
impl ::std::ops::Div<f64> for $t {
7472
type Output = Self;
75-
73+
7674
fn div(self, rhs: f64) -> Self {
7775
Self::from_base_units(self.get_base_units() / rhs)
7876
}
7977
}
8078

81-
///
82-
/// Multiplying a `$t` by another `$t` returns the product of those measurements.
83-
///
79+
// Multiplying a `$t` by another `$t` returns the product of those measurements.
80+
//
8481
impl ::std::ops::Mul<$t> for $t {
8582
type Output = Self;
86-
83+
8784
fn mul(self, rhs: Self) -> Self {
8885
Self::from_base_units(self.get_base_units() * rhs.get_base_units())
8986
}
9087
}
9188

92-
///
93-
/// Multiplying a `$t` by a factor increases (or decreases) that measurement a number of times.
94-
///
89+
// Multiplying a `$t` by a factor increases (or decreases) that measurement a number of times.
90+
//
9591
impl ::std::ops::Mul<f64> for $t {
9692
type Output = Self;
97-
93+
9894
fn mul(self, rhs: f64) -> Self {
9995
Self::from_base_units(self.get_base_units() * rhs)
10096
}

0 commit comments

Comments
 (0)