| 1 | //! Code around temperature data. |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#}; |
| 4 | |
| 5 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 6 | pub enum TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType# { |
| 7 | #[defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/default:] |
| 8 | Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Celsius#, |
| 9 | Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Kelvin#, |
| 10 | Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Fahrenheit#, |
| 11 | } |
| 12 | |
| 13 | impl FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType# { |
| 14 | type Errrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType][FromStr]Err# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#; |
| 15 | |
| 16 | fn from_strrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType][FromStr]from_str().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType][FromStr], Selfrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType][FromStr]::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#> { |
| 17 | match s { |
| 18 | "fahrenheit" | "f" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Fahrenheit#), |
| 19 | "kelvin" | "k" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Kelvin#), |
| 20 | "celsius" | "c" => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Celsius#), |
| 21 | _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 22 | "'{s}' is an invalid temperature type, use one of: [kelvin, k, celsius, c, fahrenheit, f]." |
| 23 | )), |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType# { |
| 29 | /// Return the unit string. |
| 30 | pub fn unitrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType]unit().(&self) -> &'static str { |
| 31 | match self { |
| 32 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Celsius# => "Β°C", |
| 33 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Kelvin# => "K", |
| 34 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Fahrenheit# => "Β°F", |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// Given a temperature in Celsius, convert it if necessary for a different |
| 39 | /// unit. |
| 40 | pub fn convert_temp_unitrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType]convert_temp_unit().(&self, celsius: f32) -> TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature# { |
| 41 | match self { |
| 42 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Celsius# => TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Celsius#(celsius.ceilrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]ceil().() as u32), |
| 43 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Kelvin# => TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Kelvin#((celsius rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().273.15).ceilrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]ceil().() as u32), |
| 44 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Fahrenheit# => { |
| 45 | TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Fahrenheit#(((celsius rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().(9.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().5.0)) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().32.0).ceilrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]ceil().() as u32) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Given a temperature in Celsius, convert it if necessary for a different |
| 51 | /// unit as a bare float. |
| 52 | pub fn convert_temp_unit_floatrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType]convert_temp_unit_float().(&self, celsius: f32) -> f32 { |
| 53 | match self { |
| 54 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Celsius# => celsius, |
| 55 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Kelvin# => celsius rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().273.15, |
| 56 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Fahrenheit# => celsius rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().(9.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().5.0) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().32.0, |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /// A temperature and its type. |
| 62 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 63 | pub enum TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature# { |
| 64 | Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Celsius#(u32), |
| 65 | Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Kelvin#(u32), |
| 66 | Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Fahrenheit#(u32), |
| 67 | } |
| 68 | |
| 69 | impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature# { |
| 70 | fn fmtrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TypedTemperature][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 71 | match self { |
| 72 | TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Celsius#(val) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{val}Β°C"), |
| 73 | TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Kelvin#(val) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{val}K"), |
| 74 | TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Fahrenheit#(val) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{val}Β°F"), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[cfg(test)] |
| 80 | mod testrust-analyzer cargo bottom 0.14.9 app/data/temperature/test/ { |
| 81 | use superrust-analyzer cargo bottom 0.14.9 app/data/temperature/::*; |
| 82 | |
| 83 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 84 | fn temp_conversionsrust-analyzer cargo bottom 0.14.9 app/data/temperature/test/temp_conversions().() { |
| 85 | const TEMPrust-analyzer cargo bottom 0.14.9 app/data/temperature/test/TEMP.: f32 = 100.0; |
| 86 | |
| 87 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 88 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Celsius#.convert_temp_unitrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType]convert_temp_unit().(TEMPrust-analyzer cargo bottom 0.14.9 app/data/temperature/test/TEMP.), |
| 89 | TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Celsiusrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Celsius#(TEMPrust-analyzer cargo bottom 0.14.9 app/data/temperature/test/TEMP. as u32), |
| 90 | ); |
| 91 | |
| 92 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 93 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Kelvin#.convert_temp_unitrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType]convert_temp_unit().(TEMPrust-analyzer cargo bottom 0.14.9 app/data/temperature/test/TEMP.), |
| 94 | TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Kelvinrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Kelvin#(373.15_f32.ceilrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]ceil().() as u32) |
| 95 | ); |
| 96 | |
| 97 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 98 | TemperatureTyperust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#::Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TemperatureType#Fahrenheit#.convert_temp_unitrust-analyzer cargo bottom 0.14.9 app/data/temperature/impl#[TemperatureType]convert_temp_unit().(TEMPrust-analyzer cargo bottom 0.14.9 app/data/temperature/test/TEMP.), |
| 99 | TypedTemperaturerust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#::Fahrenheitrust-analyzer cargo bottom 0.14.9 app/data/temperature/TypedTemperature#Fahrenheit#(212) |
| 100 | ); |
| 101 | } |
| 102 | } |