use crate::Result;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Temperature {
pub label: String,
pub kind: SensorKind,
pub celsius: f32,
pub high: Option<f32>,
pub critical: Option<f32>,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum SensorKind {
Cpu,
Gpu,
Storage,
Battery,
Ambient,
Other,
}
impl SensorKind {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Cpu => "cpu",
Self::Gpu => "gpu",
Self::Storage => "storage",
Self::Battery => "battery",
Self::Ambient => "ambient",
Self::Other => "other",
}
}
}
pub fn temperatures() -> Result<Vec<Temperature>> {
crate::sys::temperatures()
}
#[allow(dead_code)] pub(crate) fn plausible(celsius: f32) -> bool {
(-60.0..=250.0).contains(&celsius)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sensors_live_list_or_unavailable() {
match temperatures() {
Ok(list) => {
for t in list {
assert!(plausible(t.celsius), "{t:?}");
assert!(!t.label.is_empty());
}
}
Err(e) => assert!(e.is_unavailable(), "{e}"),
}
}
#[test]
fn test_sensors_plausible_bounds() {
assert!(plausible(45.0) && plausible(-20.0));
assert!(!plausible(-273.0) && !plausible(511.0) && !plausible(f32::NAN));
}
}