diff options
Diffstat (limited to 'src/app/data_harvester/temperature.rs')
-rw-r--r-- | src/app/data_harvester/temperature.rs | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/src/app/data_harvester/temperature.rs b/src/app/data_harvester/temperature.rs index 8287d585..a15eb26a 100644 --- a/src/app/data_harvester/temperature.rs +++ b/src/app/data_harvester/temperature.rs @@ -29,19 +29,22 @@ pub enum TemperatureType { Fahrenheit, } -fn convert_celsius_to_kelvin(celsius: f32) -> f32 { - celsius + 273.15 -} +impl TemperatureType { + /// Given a temperature in Celsius, covert it if necessary for a different unit. + pub fn convert_temp_unit(&self, temp_celsius: f32) -> f32 { + fn convert_celsius_to_kelvin(celsius: f32) -> f32 { + celsius + 273.15 + } -fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 { - (celsius * (9.0 / 5.0)) + 32.0 -} + fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 { + (celsius * (9.0 / 5.0)) + 32.0 + } -pub fn convert_temp_unit(temp: f32, temp_type: &TemperatureType) -> f32 { - match temp_type { - TemperatureType::Celsius => temp, - TemperatureType::Kelvin => convert_celsius_to_kelvin(temp), - TemperatureType::Fahrenheit => convert_celsius_to_fahrenheit(temp), + match self { + TemperatureType::Celsius => temp_celsius, + TemperatureType::Kelvin => convert_celsius_to_kelvin(temp_celsius), + TemperatureType::Fahrenheit => convert_celsius_to_fahrenheit(temp_celsius), + } } } @@ -59,3 +62,23 @@ pub fn is_temp_filtered(filter: &Option<Filter>, text: &str) -> bool { true } } + +#[cfg(test)] +mod test { + use crate::app::data_harvester::temperature::TemperatureType; + + #[test] + fn temp_conversions() { + const TEMP: f32 = 100.0; + + assert_eq!( + TemperatureType::Celsius.convert_temp_unit(TEMP), + TEMP, + "celsius to celsius is the same" + ); + + assert_eq!(TemperatureType::Kelvin.convert_temp_unit(TEMP), 373.15); + + assert_eq!(TemperatureType::Fahrenheit.convert_temp_unit(TEMP), 212.0); + } +} |