Added micro liters and gallons
This commit is contained in:
+111
-2
@@ -1,7 +1,11 @@
|
|||||||
use crate::quantity::{Quantity, Value};
|
use crate::quantity::{Quantity, Value};
|
||||||
use generate_quantity::quantity_type;
|
use generate_quantity::quantity_type;
|
||||||
|
|
||||||
use crate::quantity::MILLI;
|
use crate::quantity::{MICRO, MILLI};
|
||||||
|
|
||||||
|
const LITERS_PER_GALLON: f64 = 3_785_411_784.0 / 1_000_000_000.0;
|
||||||
|
const MILLI_LITERS_PER_GALLON: f64 = 3_785_411_784.0 / 1_000_000.0;
|
||||||
|
const MICRO_LITERS_PER_GALLON: f64 = 3_785_411_784.0 / 1_000.0;
|
||||||
|
|
||||||
//----- Liters ----------------------------------
|
//----- Liters ----------------------------------
|
||||||
quantity_type! {Liters, "L"}
|
quantity_type! {Liters, "L"}
|
||||||
@@ -12,6 +16,18 @@ impl<V: Value> Liters<V> {
|
|||||||
let multiplier = V::from_u16(MILLI).unwrap();
|
let multiplier = V::from_u16(MILLI).unwrap();
|
||||||
MilliLiters(self.0 * multiplier)
|
MilliLiters(self.0 * multiplier)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_micro_liters(self) -> MicroLiters<V> {
|
||||||
|
let multiplier = V::from_u32(MICRO).unwrap();
|
||||||
|
MicroLiters(self.0 * multiplier)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_gallons(self) -> Gallons<V> {
|
||||||
|
let divisor = V::from_f64(LITERS_PER_GALLON).unwrap();
|
||||||
|
Gallons(self.0 / divisor)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//----- Milliliters ----------------------------------
|
//----- Milliliters ----------------------------------
|
||||||
@@ -23,6 +39,64 @@ impl<V: Value> MilliLiters<V> {
|
|||||||
let divisor = V::from_u16(MILLI).unwrap();
|
let divisor = V::from_u16(MILLI).unwrap();
|
||||||
Liters(self.0 / divisor)
|
Liters(self.0 / divisor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_micro_liters(self) -> MicroLiters<V> {
|
||||||
|
let multiplier = V::from_u16(MILLI).unwrap();
|
||||||
|
MicroLiters(self.0 * multiplier)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_gallons(self) -> Gallons<V> {
|
||||||
|
let divisor = V::from_f64(MILLI_LITERS_PER_GALLON).unwrap();
|
||||||
|
Gallons(self.0 / divisor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//----- Microliters ----------------------------------
|
||||||
|
quantity_type! {MicroLiters, "µL"}
|
||||||
|
|
||||||
|
impl<V: Value> MicroLiters<V> {
|
||||||
|
#[inline]
|
||||||
|
pub fn to_liters(self) -> Liters<V> {
|
||||||
|
let divisor = V::from_u32(MICRO).unwrap();
|
||||||
|
Liters(self.0 / divisor)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_milli_liters(self) -> MilliLiters<V> {
|
||||||
|
let divisor = V::from_u16(MILLI).unwrap();
|
||||||
|
MilliLiters(self.0 / divisor)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_gallons(self) -> Gallons<V> {
|
||||||
|
let divisor = V::from_f64(MICRO_LITERS_PER_GALLON).unwrap();
|
||||||
|
Gallons(self.0 / divisor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//----- Gallons ----------------------------------
|
||||||
|
quantity_type! {Gallons, "gal"}
|
||||||
|
|
||||||
|
impl<V: Value> Gallons<V> {
|
||||||
|
#[inline]
|
||||||
|
pub fn to_liters(self) -> Liters<V> {
|
||||||
|
let multiplier = V::from_f64(LITERS_PER_GALLON).unwrap();
|
||||||
|
Liters(self.0 * multiplier)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_milli_liters(self) -> MilliLiters<V> {
|
||||||
|
let multiplier = V::from_f64(MILLI_LITERS_PER_GALLON).unwrap();
|
||||||
|
MilliLiters(self.0 * multiplier)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn to_micro_liters(self) -> MicroLiters<V> {
|
||||||
|
let multiplier = V::from_f64(MICRO_LITERS_PER_GALLON).unwrap();
|
||||||
|
MicroLiters(self.0 * multiplier)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------------------
|
||||||
@@ -31,13 +105,48 @@ impl<V: Value> MilliLiters<V> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use float_cmp::assert_approx_eq;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_u32() {
|
fn convert_metric_u32() {
|
||||||
let liters: Liters<u32> = 12.liters();
|
let liters: Liters<u32> = 12.liters();
|
||||||
let milli_liters: MilliLiters<u32> = 12_000.milli_liters();
|
let milli_liters: MilliLiters<u32> = 12_000.milli_liters();
|
||||||
|
let micro_liters: MicroLiters<u32> = 12_000_000.micro_liters();
|
||||||
|
|
||||||
assert_eq!(liters.0, milli_liters.to_liters().0);
|
assert_eq!(liters.0, milli_liters.to_liters().0);
|
||||||
assert_eq!(milli_liters.0, liters.to_milli_liters().0);
|
assert_eq!(milli_liters.0, liters.to_milli_liters().0);
|
||||||
|
|
||||||
|
assert_eq!(liters.0, micro_liters.to_liters().0);
|
||||||
|
assert_eq!(micro_liters.0, liters.to_micro_liters().0);
|
||||||
|
|
||||||
|
assert_eq!(milli_liters.0, micro_liters.to_milli_liters().0);
|
||||||
|
assert_eq!(micro_liters.0, milli_liters.to_micro_liters().0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gallons_liters() {
|
||||||
|
let gallons: Gallons<f64> = 1.0.gallons();
|
||||||
|
let liters: Liters<f64> = 3.785411784.liters();
|
||||||
|
|
||||||
|
assert_approx_eq!(f64, gallons.to_liters().0, liters.0);
|
||||||
|
assert_approx_eq!(f64, liters.to_gallons().0, gallons.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gallons_milli_liters() {
|
||||||
|
let gallons: Gallons<f64> = 1.0.gallons();
|
||||||
|
let milli_liters: MilliLiters<f64> = 3_785.411784.milli_liters();
|
||||||
|
|
||||||
|
assert_approx_eq!(f64, gallons.to_milli_liters().0, milli_liters.0);
|
||||||
|
assert_approx_eq!(f64, milli_liters.to_gallons().0, gallons.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gallons_micro_liters() {
|
||||||
|
let gallons: Gallons<f64> = 1.0.gallons();
|
||||||
|
let micro_liters: MicroLiters<f64> = 3_785_411.784.micro_liters();
|
||||||
|
|
||||||
|
assert_approx_eq!(f64, gallons.to_micro_liters().0, micro_liters.0);
|
||||||
|
assert_approx_eq!(f64, micro_liters.to_gallons().0, gallons.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user