#![allow(unused)]
use crate::errors::FphicsError;
#[derive(Debug)]
pub enum MetricLengthUnit {
MilliMetre, CentiMetre, Metre, KiloMetre, }
#[derive(Debug)]
pub enum MassUnit {
Gram, Kilogram, Pound, }
#[derive(Debug)]
pub enum ImperialUnitLength {
Inch, Yard, Foot, Mile, }
#[derive(Debug)]
pub enum SpeedUnit {
CmPerSecond, MPerSecond, KmPerSecond, KmPerHour, MilesPerHour, }
#[derive(Debug)]
pub struct Unit {
metric_length_unit: (Option<MetricLengthUnit>, Option<f64>), imperial_length_unit: (Option<ImperialUnitLength>, Option<f64>), mass_unit: (Option<MassUnit>, Option<f64>), speed_unit: (Option<SpeedUnit>, Option<f64>), }
const G_TO_LB: f64 = 1.0 / 453.59237; const LB_TO_G: f64 = 453.59237; const LB_TO_KG: f64 = 0.45359237; const KG_TO_LB: f64 = 1.0 / 0.45359237;
const IN_TO_YD: f64 = 1.0 / 36.0; const IN_TO_FT: f64 = 1.0 / 12.0; const IN_TO_MILE: f64 = 1.0 / 63360.0; const YD_TO_MILE: f64 = 1.0 / 1760.0; const FT_TO_YD: f64 = 1.0 / 3.0; const FT_TO_MILE: f64 = 1.0 / 5280.0;
const MM_TO_IN: f64 = 1.0 / 25.4; const CM_TO_IN: f64 = 1.0 / 2.54; const CM_TO_YD: f64 = 1.0 / 91.44; const METRES_TO_YD: f64 = 1.0 / 0.9144; const METRES_TO_FT: f64 = 1.0 / 0.3048; const METRES_TO_MILE: f64 = 1.0 / 1609.344; const KM_TO_FT: f64 = 1000.0 / 0.3048; const KM_TO_MILE: f64 = 1.0 / 1.609344;
const IN_TO_MM: f64 = 25.4; const IN_TO_CM: f64 = 2.54; const YD_TO_CM: f64 = 91.44; const YD_TO_METRES: f64 = 0.9144; const FT_TO_METRES: f64 = 0.3048; const MILES_TO_METRES: f64 = 1609.344; const FT_TO_KM: f64 = 0.0003048; const MILES_TO_KM: f64 = 1.609344;
const CENTIMETRES_TO_METRES_PER_S: f64 = 1.0 / 100.0; const METRES_TO_KM_PER_S: f64 = 1.0 / 1000.0; const METRES_PER_SECOND_TO_KPH: f64 = 3.6; const METRES_PER_SECOND_TO_MPH: f64 = 1.0 / 0.44704; const KM_PER_SECOND_TO_MPH: f64 = 1.0 / 0.00044704; const KPH_TO_METRES_PER_SECOND: f64 = 1.0 / 3.6; const KPH_TO_MPH: f64 = 1.0 / 1.609344; const MPH_TO_KPH: f64 = 1.609344; const MPH_TO_METRES_PER_SECOND: f64 = 0.44704;
impl Unit {
pub fn new() -> Self {
Self {
metric_length_unit: (None, None), imperial_length_unit: (None, None), mass_unit: (None, None), speed_unit: (None, None), }
}
pub fn from_metric_length(mut self, mut unit: MetricLengthUnit, val: f64) -> Self {
self.metric_length_unit = (Some(unit), Some(val));
self
}
pub fn from_imperial_length(mut self, unit: ImperialUnitLength, val: f64) -> Self {
self.imperial_length_unit = (Some(unit), Some(val));
self
}
pub fn from_speed(mut self, unit: SpeedUnit, val: f64) -> Self {
self.speed_unit = (Some(unit), Some(val));
self
}
pub fn from_mass(mut self, unit: MassUnit, val: f64) -> Self {
self.mass_unit = (Some(unit), Some(val));
self
}
pub fn metric_to_metric(&self, unit: MetricLengthUnit) -> Result<f64, FphicsError> {
match unit {
MetricLengthUnit::MilliMetre => {
match &self.metric_length_unit.0 {
Some(stored_val) => {
match stored_val {
MetricLengthUnit::MilliMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::CentiMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * 10.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::Metre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * 1000.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::KiloMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * 1_000_000.0),
None => Err(FphicsError::MissingVal),
}
}
}
},
None => Err(FphicsError::MissingMetricUnit)
}
}
MetricLengthUnit::CentiMetre => {
match &self.metric_length_unit.0 {
Some(stored_val) => {
match stored_val {
MetricLengthUnit::MilliMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v / 10.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::CentiMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::Metre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * 100.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::KiloMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * 100_000.0),
None => Err(FphicsError::MissingVal),
}
}
}
},
None => Err(FphicsError::MissingMetricUnit)
}
}
MetricLengthUnit::Metre => {
match &self.metric_length_unit.0 {
Some(stored_val) => {
match stored_val {
MetricLengthUnit::MilliMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v / 1_000.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::CentiMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v / 100.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::Metre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::KiloMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * 1_000.0),
None => Err(FphicsError::MissingVal),
}
}
}
},
None => Err(FphicsError::MissingMetricUnit)
}
}
MetricLengthUnit::KiloMetre => {
match &self.metric_length_unit.0 {
Some(stored_val) => {
match stored_val {
MetricLengthUnit::MilliMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v / 1_000_000.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::CentiMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v / 100_000.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::Metre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v / 1_000.0),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::KiloMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
}
},
None => Err(FphicsError::MissingMetricUnit)
}
}
}
}
pub fn imperial_to_imperial(&self, unit: ImperialUnitLength) -> Result<f64, FphicsError> {
match unit {
ImperialUnitLength::Inch => {
match &self.imperial_length_unit.0 {
Some(stored_val) => {
match stored_val {
ImperialUnitLength::Inch => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Yard => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * 36.0),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Foot => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * 12.0),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Mile => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * 63_360.0),
None => Err(FphicsError::MissingVal),
}
}
}
}
None => Err(FphicsError::MissingImperialUnit)
}
}
ImperialUnitLength::Yard => {
match &self.imperial_length_unit.0 {
Some(stored_val) => {
match stored_val {
ImperialUnitLength::Inch => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * IN_TO_YD),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Yard => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Foot => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * FT_TO_YD),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Mile => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * 1_760.0),
None => Err(FphicsError::MissingVal),
}
}
}
}
None => Err(FphicsError::MissingImperialUnit)
}
}
ImperialUnitLength::Foot => {
match &self.imperial_length_unit.0 {
Some(stored_val) => {
match stored_val {
ImperialUnitLength::Inch => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * IN_TO_FT),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Yard => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * 3.0),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Foot => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Mile => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * 5280.0),
None => Err(FphicsError::MissingVal),
}
}
}
}
None => Err(FphicsError::MissingImperialUnit)
}
}
ImperialUnitLength::Mile => {
match &self.imperial_length_unit.0 {
Some(stored_val) => {
match stored_val {
ImperialUnitLength::Inch => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * IN_TO_MILE),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Yard => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * YD_TO_MILE),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Foot => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * FT_TO_MILE),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Mile => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
}
}
None => Err(FphicsError::MissingImperialUnit)
}
}
}
}
pub fn metric_to_imperial(&self, unit: ImperialUnitLength) -> Result<f64, FphicsError> {
match unit {
ImperialUnitLength::Inch => {
match &self.metric_length_unit.0 {
Some(m_unit) => {
match m_unit {
MetricLengthUnit::MilliMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * MM_TO_IN),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::CentiMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * CM_TO_IN),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingMetricUnit)
}
}
ImperialUnitLength::Yard => {
match &self.metric_length_unit.0 {
Some(m_unit) => {
match m_unit {
MetricLengthUnit::CentiMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * CM_TO_YD),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::Metre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * METRES_TO_YD),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingMetricUnit)
}
}
ImperialUnitLength::Foot => {
match &self.metric_length_unit.0 {
Some(m_unit) => {
match m_unit {
MetricLengthUnit::Metre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * METRES_TO_FT),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::KiloMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * KM_TO_FT),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingMetricUnit)
}
}
ImperialUnitLength::Mile => {
match &self.metric_length_unit.0 {
Some(m_unit) => {
match m_unit {
MetricLengthUnit::Metre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * METRES_TO_MILE),
None => Err(FphicsError::MissingVal),
}
}
MetricLengthUnit::KiloMetre => {
match self.metric_length_unit.1 {
Some(v) => Ok(v * KM_TO_MILE),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingMetricUnit)
}
}
}
}
pub fn imperial_to_metric(&self, unit: MetricLengthUnit) -> Result<f64, FphicsError> {
match unit {
MetricLengthUnit::MilliMetre => {
match &self.imperial_length_unit.0 {
Some(stored_unit) => {
match stored_unit {
ImperialUnitLength::Inch => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * IN_TO_MM),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingImperialUnit)
}
}
MetricLengthUnit::CentiMetre => {
match &self.imperial_length_unit.0 {
Some(stored_unit) => {
match stored_unit {
ImperialUnitLength::Inch => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * IN_TO_CM),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Yard => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * YD_TO_CM),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingImperialUnit)
}
}
MetricLengthUnit::Metre => {
match &self.imperial_length_unit.0 {
Some(stored_unit) => {
match stored_unit {
ImperialUnitLength::Yard => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * YD_TO_METRES),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Foot => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * FT_TO_METRES),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Mile => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * MILES_TO_METRES),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingImperialUnit)
}
}
MetricLengthUnit::KiloMetre => {
match &self.imperial_length_unit.0 {
Some(stored_unit) => {
match stored_unit {
ImperialUnitLength::Foot => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * FT_TO_KM),
None => Err(FphicsError::MissingVal),
}
}
ImperialUnitLength::Mile => {
match self.imperial_length_unit.1 {
Some(v) => Ok(v * MILES_TO_KM),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingImperialUnit)
}
}
}
}
pub fn to_speed(&self, unit: SpeedUnit) -> Result<f64, FphicsError> {
match unit {
SpeedUnit::CmPerSecond => {
match &self.speed_unit.0 {
Some(stored_unit) => {
match stored_unit {
SpeedUnit::CmPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::MPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v * 100.0),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingSpeedUnit)
}
}
SpeedUnit::MPerSecond => {
match &self.speed_unit.0 {
Some(stored_unit) => {
match stored_unit {
SpeedUnit::CmPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v * CENTIMETRES_TO_METRES_PER_S),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::MPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::KmPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v * 1000.0),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::KmPerHour => {
match self.speed_unit.1 {
Some(v) => Ok(v * KPH_TO_METRES_PER_SECOND),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::MilesPerHour => {
match self.speed_unit.1 {
Some(v) => Ok(v * MPH_TO_METRES_PER_SECOND),
None => Err(FphicsError::MissingVal),
}
}
}
}
None => Err(FphicsError::MissingSpeedUnit)
}
}
SpeedUnit::KmPerSecond => {
match &self.speed_unit.0 {
Some(stored_unit) => {
match stored_unit {
SpeedUnit::MPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v * METRES_TO_KM_PER_S),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::KmPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingSpeedUnit)
}
}
SpeedUnit::KmPerHour => {
match &self.speed_unit.0 {
Some(stored_unit) => {
match stored_unit {
SpeedUnit::MPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v * METRES_PER_SECOND_TO_KPH),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::KmPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v * 3600.0),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::MilesPerHour => {
match self.speed_unit.1 {
Some(v) => Ok(v * MPH_TO_KPH),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingSpeedUnit)
}
}
SpeedUnit::MilesPerHour => {
match &self.speed_unit.0 {
Some(stored_unit) => {
match stored_unit {
SpeedUnit::MPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v * METRES_PER_SECOND_TO_MPH),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::KmPerSecond => {
match self.speed_unit.1 {
Some(v) => Ok(v * KM_PER_SECOND_TO_MPH),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::MilesPerHour => {
match self.speed_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
SpeedUnit::KmPerHour => {
match self.speed_unit.1 {
Some(v) => Ok(v * KPH_TO_MPH),
None => Err(FphicsError::MissingVal),
}
}
_ => Err(FphicsError::ConversionNotSupported)
}
}
None => Err(FphicsError::MissingSpeedUnit)
}
}
}
}
pub fn to_mass(&self, unit: MassUnit) -> Result<f64, FphicsError> {
match unit {
MassUnit::Gram => {
match &self.mass_unit.0 {
Some(stored_val) => {
match stored_val {
MassUnit::Gram => {
match self.mass_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
MassUnit::Kilogram => {
match self.mass_unit.1 {
Some(v) => Ok(v * 1000.0),
None => Err(FphicsError::MissingVal),
}
}
MassUnit::Pound => {
match self.mass_unit.1 {
Some(v) => Ok(v * LB_TO_G),
None => Err(FphicsError::MissingVal),
}
}
}
}
None => Err(FphicsError::MissingMassUnit)
}
}
MassUnit::Kilogram => {
match &self.mass_unit.0 {
Some(stored_val) => {
match stored_val {
MassUnit::Gram => {
match self.mass_unit.1 {
Some(v) => Ok(v / 1000.0),
None => Err(FphicsError::MissingVal),
}
}
MassUnit::Kilogram => {
match self.mass_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
MassUnit::Pound => {
match self.mass_unit.1 {
Some(v) => Ok(v * LB_TO_KG),
None => Err(FphicsError::MissingVal),
}
}
}
}
None => Err(FphicsError::MissingMassUnit)
}
}
MassUnit::Pound => {
match &self.mass_unit.0 {
Some(stored_val) => {
match stored_val {
MassUnit::Gram => {
match self.mass_unit.1 {
Some(v) => Ok(v * G_TO_LB),
None => Err(FphicsError::MissingVal),
}
}
MassUnit::Kilogram => {
match self.mass_unit.1 {
Some(v) => Ok(v * KG_TO_LB),
None => Err(FphicsError::MissingVal),
}
}
MassUnit::Pound => {
match self.mass_unit.1 {
Some(v) => Ok(v),
None => Err(FphicsError::MissingVal),
}
}
}
}
None => Err(FphicsError::MissingMassUnit)
}
}
}
}
}