summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2023-11-20 08:08:24 +0000
committerGitHub <noreply@github.com>2023-11-20 03:08:24 -0500
commitab9331140a002841cce99fdfbcc35243e0aa4e7f (patch)
treea0ee16003a52b7e7aeb7b7acef22fd971b605327
parent10a37c263a436a176cd82b5e2a688882acbd99f4 (diff)
refactor: simplify temperature conversion function usage (#1326)
* refactor: simplify temperature conversion function usage Just make it a function on the temperature type enum. * fix sysinfo variant * simple test
-rw-r--r--src/app/data_harvester/nvidia.rs8
-rw-r--r--src/app/data_harvester/temperature.rs45
-rw-r--r--src/app/data_harvester/temperature/linux.rs10
-rw-r--r--src/app/data_harvester/temperature/sysinfo.rs13
4 files changed, 44 insertions, 32 deletions
diff --git a/src/app/data_harvester/nvidia.rs b/src/app/data_harvester/nvidia.rs
index 19abdb9e..848a18f7 100644
--- a/src/app/data_harvester/nvidia.rs
+++ b/src/app/data_harvester/nvidia.rs
@@ -8,9 +8,7 @@ use crate::app::Filter;
use crate::app::layout_manager::UsedWidgets;
use crate::data_harvester::memory::MemHarvest;
-use crate::data_harvester::temperature::{
- convert_temp_unit, is_temp_filtered, TempHarvest, TemperatureType,
-};
+use crate::data_harvester::temperature::{is_temp_filtered, TempHarvest, TemperatureType};
pub static NVML_DATA: Lazy<Result<Nvml, NvmlError>> = Lazy::new(Nvml::init);
@@ -52,8 +50,8 @@ pub fn get_nvidia_vecs(
}
if widgets_to_harvest.use_temp && is_temp_filtered(filter, &name) {
if let Ok(temperature) = device.temperature(TemperatureSensor::Gpu) {
- let temperature = temperature as f32;
- let temperature = convert_temp_unit(temperature, temp_type);
+ let temperature = temp_type.convert_temp_unit(temperature as f32);
+
temp_vec.push(TempHarvest {
name: name.clone(),
temperature,
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);
+ }
+}
diff --git a/src/app/data_harvester/temperature/linux.rs b/src/app/data_harvester/temperature/linux.rs
index 5b6fbbb0..a62c52b1 100644
--- a/src/app/data_harvester/temperature/linux.rs
+++ b/src/app/data_harvester/temperature/linux.rs
@@ -9,7 +9,7 @@ use anyhow::Result;
use hashbrown::{HashMap, HashSet};
use super::{is_temp_filtered, TempHarvest, TemperatureType};
-use crate::app::{data_harvester::temperature::convert_temp_unit, Filter};
+use crate::app::Filter;
const EMPTY_NAME: &str = "Unknown";
@@ -272,7 +272,7 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option<Filter>) -> H
let name = finalize_name(hwmon_name, sensor_label, &sensor_name, &mut seen_names);
if is_temp_filtered(filter, &name) {
- let temp = if should_read_temp {
+ let temp_celsius = if should_read_temp {
if let Ok(temp) = read_temp(&temp_path) {
temp
} else {
@@ -284,7 +284,7 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option<Filter>) -> H
temperatures.push(TempHarvest {
name,
- temperature: convert_temp_unit(temp, temp_type),
+ temperature: temp_type.convert_temp_unit(temp_celsius),
});
}
}
@@ -324,12 +324,12 @@ fn add_thermal_zone_temperatures(
if let Some(name) = read_to_string_lossy(name_path) {
if is_temp_filtered(filter, &name) {
let temp_path = file_path.join("temp");
- if let Ok(temp) = read_temp(&temp_path) {
+ if let Ok(temp_celsius) = read_temp(&temp_path) {
let name = counted_name(&mut seen_names, name);
temperatures.push(TempHarvest {
name,
- temperature: convert_temp_unit(temp, temp_type),
+ temperature: temp_type.convert_temp_unit(temp_celsius),
});
}
}
diff --git a/src/app/data_harvester/temperature/sysinfo.rs b/src/app/data_harvester/temperature/sysinfo.rs
index 908ff736..cc415164 100644
--- a/src/app/data_harvester/temperature/sysinfo.rs
+++ b/src/app/data_harvester/temperature/sysinfo.rs
@@ -2,10 +2,7 @@
use anyhow::Result;
-use super::{
- convert_celsius_to_fahrenheit, convert_celsius_to_kelvin, is_temp_filtered, TempHarvest,
- TemperatureType,
-};
+use super::{is_temp_filtered, TempHarvest, TemperatureType};
use crate::app::Filter;
pub fn get_temperature_data(
@@ -22,13 +19,7 @@ pub fn get_temperature_data(
if is_temp_filtered(filter, &name) {
temperature_vec.push(TempHarvest {
name,
- temperature: match temp_type {
- TemperatureType::Celsius => component.temperature(),
- TemperatureType::Kelvin => convert_celsius_to_kelvin(component.temperature()),
- TemperatureType::Fahrenheit => {
- convert_celsius_to_fahrenheit(component.temperature())
- }
- },
+ temperature: temp_type.convert_temp_unit(component.temperature()),
});
}
}