-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: only process temp type in data eat step
- Loading branch information
1 parent
a41142a
commit c3365be
Showing
15 changed files
with
151 additions
and
229 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,6 @@ pub use process::ProcessData; | |
|
||
mod store; | ||
pub use store::*; | ||
|
||
mod temperature; | ||
pub use temperature::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! Code around temperature data. | ||
use std::{fmt::Display, str::FromStr}; | ||
|
||
#[derive(Clone, Debug, Copy, PartialEq, Eq, Default)] | ||
pub enum TemperatureType { | ||
#[default] | ||
Celsius, | ||
Kelvin, | ||
Fahrenheit, | ||
} | ||
|
||
impl FromStr for TemperatureType { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"fahrenheit" | "f" => Ok(TemperatureType::Fahrenheit), | ||
"kelvin" | "k" => Ok(TemperatureType::Kelvin), | ||
"celsius" | "c" => Ok(TemperatureType::Celsius), | ||
_ => Err(format!( | ||
"'{s}' is an invalid temperature type, use one of: [kelvin, k, celsius, c, fahrenheit, f]." | ||
)), | ||
} | ||
} | ||
} | ||
|
||
impl TemperatureType { | ||
/// Given a temperature in Celsius, covert it if necessary for a different | ||
/// unit. | ||
pub fn convert_temp_unit(&self, celsius: f32) -> TypedTemperature { | ||
match self { | ||
TemperatureType::Celsius => TypedTemperature::Celsius(celsius.ceil() as u32), | ||
TemperatureType::Kelvin => TypedTemperature::Kelvin((celsius + 273.15).ceil() as u32), | ||
TemperatureType::Fahrenheit => { | ||
TypedTemperature::Fahrenheit(((celsius * (9.0 / 5.0)) + 32.0).ceil() as u32) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// A temperature and its type. | ||
#[derive(Debug, PartialEq, Clone, Eq, PartialOrd, Ord)] | ||
pub enum TypedTemperature { | ||
Celsius(u32), | ||
Kelvin(u32), | ||
Fahrenheit(u32), | ||
} | ||
|
||
impl Display for TypedTemperature { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
TypedTemperature::Celsius(val) => write!(f, "{val}°C"), | ||
TypedTemperature::Kelvin(val) => write!(f, "{val}K"), | ||
TypedTemperature::Fahrenheit(val) => write!(f, "{val}°F"), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn temp_conversions() { | ||
const TEMP: f32 = 100.0; | ||
|
||
assert_eq!( | ||
TemperatureType::Celsius.convert_temp_unit(TEMP), | ||
TypedTemperature::Celsius(TEMP as u32), | ||
); | ||
|
||
assert_eq!( | ||
TemperatureType::Kelvin.convert_temp_unit(TEMP), | ||
TypedTemperature::Kelvin(373.15_f32.ceil() as u32) | ||
); | ||
|
||
assert_eq!( | ||
TemperatureType::Fahrenheit.convert_temp_unit(TEMP), | ||
TypedTemperature::Fahrenheit(212) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.