From 06083dbce9a03d3b78d16e62dc9d6c297959d545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 13 May 2023 17:50:18 +0300 Subject: [PATCH] First commit --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 8 +++++ src/conversions.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 6 ++++ src/main.rs | 7 ++++ src/units.rs | 30 ++++++++++++++++ 7 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/conversions.rs create mode 100644 src/lib.rs create mode 100644 src/main.rs create mode 100644 src/units.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6636a95 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cfc5bc6 --- /dev/null +++ b/Cargo.toml @@ -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] diff --git a/src/conversions.rs b/src/conversions.rs new file mode 100644 index 0000000..963f8a4 --- /dev/null +++ b/src/conversions.rs @@ -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); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..42089ba --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,6 @@ +mod units; +mod conversions; + +pub use units::{NonMetric, NonMetricQuantity}; + +pub use conversions::convert; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d3f6a74 --- /dev/null +++ b/src/main.rs @@ -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)); +} diff --git a/src/units.rs b/src/units.rs new file mode 100644 index 0000000..dca8437 --- /dev/null +++ b/src/units.rs @@ -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, +}