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