First commit
This commit is contained in:
commit
06083dbce9
|
@ -0,0 +1 @@
|
||||||
|
/target
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "metrify"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "metrify"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,90 @@
|
||||||
|
use crate::units::{Metric, MetricQuantity, NonMetric, NonMetricQuantity};
|
||||||
|
|
||||||
|
struct Conversion {
|
||||||
|
from: f64,
|
||||||
|
to: MetricQuantity,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_conversion(unit: NonMetric) -> Conversion {
|
||||||
|
match unit {
|
||||||
|
// Length
|
||||||
|
NonMetric::Foot => Conversion {
|
||||||
|
from: 10_000.0,
|
||||||
|
to: MetricQuantity { amount: 3048.0, unit: Metric::Metre },
|
||||||
|
},
|
||||||
|
NonMetric::Inch => Conversion {
|
||||||
|
from: 10_000.0,
|
||||||
|
to: MetricQuantity { amount: 254.0, unit: Metric::Metre },
|
||||||
|
},
|
||||||
|
NonMetric::Yard => Conversion {
|
||||||
|
from: 10_000.0,
|
||||||
|
to: MetricQuantity { amount: 9144.0, unit: Metric::Metre },
|
||||||
|
},
|
||||||
|
NonMetric::Mile => Conversion {
|
||||||
|
from: 1_000.0,
|
||||||
|
to: MetricQuantity { amount: 1609344.0, unit: Metric::Metre },
|
||||||
|
},
|
||||||
|
// Weight
|
||||||
|
NonMetric::Ounce => Conversion {
|
||||||
|
from: 1_000_000_000.0,
|
||||||
|
to: MetricQuantity { amount: 28349523125.0, unit: Metric::Gram },
|
||||||
|
},
|
||||||
|
NonMetric::Pound => Conversion {
|
||||||
|
from: 100_000.0,
|
||||||
|
to: MetricQuantity { amount: 45359237.0, unit: Metric::Gram },
|
||||||
|
},
|
||||||
|
NonMetric::Stone => Conversion {
|
||||||
|
from: 100_000.0,
|
||||||
|
to: MetricQuantity { amount: 635029318.0, unit: Metric::Gram },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn convert(from: NonMetricQuantity) -> MetricQuantity {
|
||||||
|
let conversion = get_conversion(from.unit);
|
||||||
|
let amount = from.amount * conversion.to.amount / conversion.from;
|
||||||
|
let unit = conversion.to.unit;
|
||||||
|
MetricQuantity { amount, unit }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
struct Test(NonMetric, f64);
|
||||||
|
|
||||||
|
fn run_tests(tests: &[Test], unit: Metric) {
|
||||||
|
for test in tests {
|
||||||
|
let from = NonMetricQuantity {
|
||||||
|
amount: 1.0,
|
||||||
|
unit: test.0,
|
||||||
|
};
|
||||||
|
let to = MetricQuantity {
|
||||||
|
amount: test.1,
|
||||||
|
unit: unit,
|
||||||
|
};
|
||||||
|
assert_eq!(convert(from), to);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn length() {
|
||||||
|
let tests = [
|
||||||
|
Test(NonMetric::Inch, 0.0254),
|
||||||
|
Test(NonMetric::Foot, 0.3048),
|
||||||
|
Test(NonMetric::Yard, 0.9144),
|
||||||
|
Test(NonMetric::Mile, 1609.344),
|
||||||
|
];
|
||||||
|
run_tests(&tests, Metric::Metre);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn weight() {
|
||||||
|
let tests = [
|
||||||
|
Test(NonMetric::Ounce, 28.349523125),
|
||||||
|
Test(NonMetric::Pound, 453.59237),
|
||||||
|
Test(NonMetric::Stone, 6350.29318),
|
||||||
|
];
|
||||||
|
run_tests(&tests, Metric::Gram);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
mod units;
|
||||||
|
mod conversions;
|
||||||
|
|
||||||
|
pub use units::{NonMetric, NonMetricQuantity};
|
||||||
|
|
||||||
|
pub use conversions::convert;
|
|
@ -0,0 +1,7 @@
|
||||||
|
use metrify::{NonMetric, NonMetricQuantity};
|
||||||
|
use metrify::convert;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let quantity = NonMetricQuantity { amount: 6.0, unit: NonMetric::Foot };
|
||||||
|
dbg!(convert(quantity));
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub enum Metric {
|
||||||
|
Metre,
|
||||||
|
Gram,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub enum NonMetric {
|
||||||
|
// Length
|
||||||
|
Foot,
|
||||||
|
Inch,
|
||||||
|
Mile,
|
||||||
|
Yard,
|
||||||
|
// Weight
|
||||||
|
Ounce,
|
||||||
|
Pound,
|
||||||
|
Stone,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct MetricQuantity {
|
||||||
|
pub amount: f64,
|
||||||
|
pub unit: Metric,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct NonMetricQuantity {
|
||||||
|
pub amount: f64,
|
||||||
|
pub unit: NonMetric,
|
||||||
|
}
|
Loading…
Reference in New Issue