Make unit constants const in conversion.rs

This commit is contained in:
Juhani Krekelä 2023-06-01 19:58:18 +03:00
parent 18ef6c8787
commit 897f87a9e2
1 changed files with 64 additions and 64 deletions

View File

@ -14,65 +14,65 @@ struct Conversion {
} }
fn get_conversion(unit: NonMetric) -> Conversion { fn get_conversion(unit: NonMetric) -> Conversion {
let inch_from = 10_000.0; const INCH_FROM: f64 = 10_000.0;
let inch_to = 254.0; const INCH_TO: f64 = 254.0;
let pound_from = 100_000.0; const POUND_FROM: f64 = 100_000.0;
let pound_to = 45359237.0; const POUND_TO: f64 = 45359237.0;
let imperial_gallon_from = 100_000.0; const IMPERIAL_GALLON_FROM: f64 = 100_000.0;
let imperial_gallon_to = 454609.0; const IMPERIAL_GALLON_TO: f64 = 454609.0;
let us_gallon_from = inch_from * inch_from * inch_from; const US_GALLON_FROM: f64 = INCH_FROM * INCH_FROM * INCH_FROM;
let us_gallon_to = 231.0 * inch_to * inch_to * inch_to * 1000.0; const US_GALLON_TO: f64 = 231.0 * INCH_TO * INCH_TO * INCH_TO * 1000.0;
match unit { match unit {
// Length // Length
NonMetric::Inch => Conversion { NonMetric::Inch => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from, from: INCH_FROM,
to: MetricQuantity { amount: inch_to, unit: Metric::Metre }, to: MetricQuantity { amount: INCH_TO, unit: Metric::Metre },
}, },
NonMetric::Foot => Conversion { NonMetric::Foot => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from, from: INCH_FROM,
to: MetricQuantity { amount: 12.0 * inch_to, unit: Metric::Metre }, to: MetricQuantity { amount: 12.0 * INCH_TO, unit: Metric::Metre },
}, },
NonMetric::Yard => Conversion { NonMetric::Yard => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from, from: INCH_FROM,
to: MetricQuantity { amount: 3.0 * 12.0 * inch_to, unit: Metric::Metre }, to: MetricQuantity { amount: 3.0 * 12.0 * INCH_TO, unit: Metric::Metre },
}, },
NonMetric::Mile => Conversion { NonMetric::Mile => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from, from: INCH_FROM,
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::Metre }, to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * INCH_TO, unit: Metric::Metre },
}, },
// Mass // Mass
NonMetric::Ounce => Conversion { NonMetric::Ounce => Conversion {
offset: 0.0, offset: 0.0,
from: 16.0 * pound_from, from: 16.0 * POUND_FROM,
to: MetricQuantity { amount: pound_to, unit: Metric::Gram }, to: MetricQuantity { amount: POUND_TO, unit: Metric::Gram },
}, },
NonMetric::Pound => Conversion { NonMetric::Pound => Conversion {
offset: 0.0, offset: 0.0,
from: pound_from, from: POUND_FROM,
to: MetricQuantity { amount: pound_to, unit: Metric::Gram }, to: MetricQuantity { amount: POUND_TO, unit: Metric::Gram },
}, },
NonMetric::Stone => Conversion { NonMetric::Stone => Conversion {
offset: 0.0, offset: 0.0,
from: pound_from, from: POUND_FROM,
to: MetricQuantity { amount: 14.0 * pound_to, unit: Metric::Gram }, to: MetricQuantity { amount: 14.0 * POUND_TO, unit: Metric::Gram },
}, },
NonMetric::ShortTon => Conversion { NonMetric::ShortTon => Conversion {
offset: 0.0, offset: 0.0,
from: pound_from, from: POUND_FROM,
to: MetricQuantity { amount: 2000.0 * pound_to, unit: Metric::Gram }, to: MetricQuantity { amount: 2000.0 * POUND_TO, unit: Metric::Gram },
}, },
NonMetric::LongTon => Conversion { NonMetric::LongTon => Conversion {
offset: 0.0, offset: 0.0,
from: pound_from, from: POUND_FROM,
to: MetricQuantity { amount: 2240.0 * pound_to, unit: Metric::Gram }, to: MetricQuantity { amount: 2240.0 * POUND_TO, unit: Metric::Gram },
}, },
// Temperature // Temperature
NonMetric::Fahrenheit => Conversion { NonMetric::Fahrenheit => Conversion {
@ -83,100 +83,100 @@ fn get_conversion(unit: NonMetric) -> Conversion {
// Area // Area
NonMetric::SquareInch => Conversion { NonMetric::SquareInch => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from * inch_from, from: INCH_FROM * INCH_FROM,
to: MetricQuantity { amount: inch_to * inch_to, unit: Metric::SquareMetre }, to: MetricQuantity { amount: INCH_TO * INCH_TO, unit: Metric::SquareMetre },
}, },
NonMetric::SquareFoot => Conversion { NonMetric::SquareFoot => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from * inch_from, from: INCH_FROM * INCH_FROM,
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre }, to: MetricQuantity { amount: 12.0 * INCH_TO * 12.0 * INCH_TO, unit: Metric::SquareMetre },
}, },
NonMetric::SquareYard => Conversion { NonMetric::SquareYard => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from * inch_from, from: INCH_FROM * INCH_FROM,
to: MetricQuantity { amount: 3.0 * 12.0 * inch_to * 3.0 * 12.0 * inch_to, unit: Metric::SquareMetre }, to: MetricQuantity { amount: 3.0 * 12.0 * INCH_TO * 3.0 * 12.0 * INCH_TO, unit: Metric::SquareMetre },
}, },
NonMetric::Acre => Conversion { NonMetric::Acre => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from * inch_from, from: INCH_FROM * INCH_FROM,
to: MetricQuantity { amount: 43_560.0 * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::SquareMetre }, to: MetricQuantity { amount: 43_560.0 * 12.0 * INCH_TO * 12.0 * INCH_TO, unit: Metric::SquareMetre },
}, },
NonMetric::SquareMile => Conversion { NonMetric::SquareMile => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from * inch_from, from: INCH_FROM * INCH_FROM,
to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * inch_to * 1760.0 * 3.0 * 12.0 * inch_to, unit: Metric::SquareMetre }, to: MetricQuantity { amount: 1760.0 * 3.0 * 12.0 * INCH_TO * 1760.0 * 3.0 * 12.0 * INCH_TO, unit: Metric::SquareMetre },
}, },
// Volume // Volume
NonMetric::CubicInch => Conversion { NonMetric::CubicInch => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from * inch_from * inch_from, from: INCH_FROM * INCH_FROM * INCH_FROM,
to: MetricQuantity { amount: inch_to * inch_to * inch_to, unit: Metric::CubicMetre }, to: MetricQuantity { amount: INCH_TO * INCH_TO * INCH_TO, unit: Metric::CubicMetre },
}, },
NonMetric::CubicFoot => Conversion { NonMetric::CubicFoot => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from * inch_from * inch_from, from: INCH_FROM * INCH_FROM * INCH_FROM,
to: MetricQuantity { amount: 12.0 * inch_to * 12.0 * inch_to * 12.0 * inch_to, unit: Metric::CubicMetre }, to: MetricQuantity { amount: 12.0 * INCH_TO * 12.0 * INCH_TO * 12.0 * INCH_TO, unit: Metric::CubicMetre },
}, },
NonMetric::CubicYard => Conversion { NonMetric::CubicYard => Conversion {
offset: 0.0, offset: 0.0,
from: inch_from * inch_from * inch_from, from: INCH_FROM * INCH_FROM * INCH_FROM,
to: MetricQuantity { amount: 3.0 * 12.0 * inch_to * 3.0 * 12.0 * inch_to * 3.0 * 12.0 * inch_to, unit: Metric::CubicMetre }, to: MetricQuantity { amount: 3.0 * 12.0 * INCH_TO * 3.0 * 12.0 * INCH_TO * 3.0 * 12.0 * INCH_TO, unit: Metric::CubicMetre },
}, },
// Fluid volume // Fluid volume
NonMetric::ImperialFluidOunce => Conversion { NonMetric::ImperialFluidOunce => Conversion {
offset: 0.0, offset: 0.0,
from: 20.0 * 2.0 * 4.0 * imperial_gallon_from, from: 20.0 * 2.0 * 4.0 * IMPERIAL_GALLON_FROM,
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: IMPERIAL_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::ImperialPint => Conversion { NonMetric::ImperialPint => Conversion {
offset: 0.0, offset: 0.0,
from: 2.0 * 4.0 * imperial_gallon_from, from: 2.0 * 4.0 * IMPERIAL_GALLON_FROM,
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: IMPERIAL_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::ImperialQuart => Conversion { NonMetric::ImperialQuart => Conversion {
offset: 0.0, offset: 0.0,
from: 4.0 * imperial_gallon_from, from: 4.0 * IMPERIAL_GALLON_FROM,
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: IMPERIAL_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::ImperialGallon => Conversion { NonMetric::ImperialGallon => Conversion {
offset: 0.0, offset: 0.0,
from: imperial_gallon_from, from: IMPERIAL_GALLON_FROM,
to: MetricQuantity { amount: imperial_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: IMPERIAL_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::USTeaspoon => Conversion { NonMetric::USTeaspoon => Conversion {
offset: 0.0, offset: 0.0,
from: 6.0 * 16.0 * 2.0 * 4.0 * us_gallon_from, from: 6.0 * 16.0 * 2.0 * 4.0 * US_GALLON_FROM,
to: MetricQuantity { amount: us_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: US_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::USTablespoon => Conversion { NonMetric::USTablespoon => Conversion {
offset: 0.0, offset: 0.0,
from: 2.0 * 16.0 * 2.0 * 4.0 * us_gallon_from, from: 2.0 * 16.0 * 2.0 * 4.0 * US_GALLON_FROM,
to: MetricQuantity { amount: us_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: US_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::USFluidOunce => Conversion { NonMetric::USFluidOunce => Conversion {
offset: 0.0, offset: 0.0,
from: 16.0 * 2.0 * 4.0 * us_gallon_from, from: 16.0 * 2.0 * 4.0 * US_GALLON_FROM,
to: MetricQuantity { amount: us_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: US_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::USCup => Conversion { NonMetric::USCup => Conversion {
offset: 0.0, offset: 0.0,
from: 2.0 * 2.0 * 4.0 * us_gallon_from, from: 2.0 * 2.0 * 4.0 * US_GALLON_FROM,
to: MetricQuantity { amount: us_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: US_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::USLiquidPint => Conversion { NonMetric::USLiquidPint => Conversion {
offset: 0.0, offset: 0.0,
from: 2.0 * 4.0 * us_gallon_from, from: 2.0 * 4.0 * US_GALLON_FROM,
to: MetricQuantity { amount: us_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: US_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::USLiquidQuart => Conversion { NonMetric::USLiquidQuart => Conversion {
offset: 0.0, offset: 0.0,
from: 4.0 * us_gallon_from, from: 4.0 * US_GALLON_FROM,
to: MetricQuantity { amount: us_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: US_GALLON_TO, unit: Metric::Litre },
}, },
NonMetric::USGallon => Conversion { NonMetric::USGallon => Conversion {
offset: 0.0, offset: 0.0,
from: us_gallon_from, from: US_GALLON_FROM,
to: MetricQuantity { amount: us_gallon_to, unit: Metric::Litre }, to: MetricQuantity { amount: US_GALLON_TO, unit: Metric::Litre },
}, },
} }
} }