From 8689492ae5f4b2b7ae63f28755745ef8f8e7b0a5 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Thu, 2 Jun 2022 03:24:30 -0400 Subject: refactor: unify all mod.rs structure to 2018 style (#742) This is a pretty small change, but at least _for now_, unifies all `mod.rs` use cases to the 2018 style for consistency. I personally don't mind going back to it on a case-by-case basis in the future if it results in cleaner code, though. --- src/app/data_harvester/temperature.rs | 88 +++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/app/data_harvester/temperature.rs (limited to 'src/app/data_harvester/temperature.rs') diff --git a/src/app/data_harvester/temperature.rs b/src/app/data_harvester/temperature.rs new file mode 100644 index 00000000..7d5d247d --- /dev/null +++ b/src/app/data_harvester/temperature.rs @@ -0,0 +1,88 @@ +//! Data collection for temperature metrics. +//! +//! For Linux and macOS, this is handled by Heim. +//! For Windows, this is handled by sysinfo. + +cfg_if::cfg_if! { + if #[cfg(target_os = "linux")] { + pub mod heim; + pub use self::heim::*; + } else if #[cfg(any(target_os = "macos", target_os = "windows"))] { + pub mod sysinfo; + pub use self::sysinfo::*; + } +} + +#[cfg(feature = "nvidia")] +pub mod nvidia; + +use std::cmp::Ordering; + +use crate::app::Filter; + +#[derive(Default, Debug, Clone)] +pub struct TempHarvest { + pub name: String, + pub temperature: f32, +} + +#[derive(Clone, Debug)] +pub enum TemperatureType { + Celsius, + Kelvin, + Fahrenheit, +} + +impl Default for TemperatureType { + fn default() -> Self { + TemperatureType::Celsius + } +} + +cfg_if::cfg_if! { + if #[cfg(any(feature = "nvidia", target_os = "macos", target_os = "windows"))] { + 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 is_temp_filtered(filter: &Option, text: &str) -> bool { + if let Some(filter) = filter { + if filter.is_list_ignored { + let mut ret = true; + for r in &filter.list { + if r.is_match(text) { + ret = false; + break; + } + } + ret + } else { + true + } + } else { + true + } +} + +fn temp_vec_sort(temperature_vec: &mut [TempHarvest]) { + // By default, sort temperature, then by alphabetically! + // TODO: [TEMPS] Allow users to control this. + + // Note we sort in reverse here; we want greater temps to be higher priority. + temperature_vec.sort_by(|a, b| match a.temperature.partial_cmp(&b.temperature) { + Some(x) => match x { + Ordering::Less => Ordering::Greater, + Ordering::Greater => Ordering::Less, + Ordering::Equal => Ordering::Equal, + }, + None => Ordering::Equal, + }); + + temperature_vec.sort_by(|a, b| a.name.partial_cmp(&b.name).unwrap_or(Ordering::Equal)); +} -- cgit v1.2.3