use crate::{ComponentInner, ComponentsInner};
pub struct Components {
pub(crate) inner: ComponentsInner,
}
impl Default for Components {
fn default() -> Self {
Self::new()
}
}
impl From<Components> for Vec<Component> {
fn from(components: Components) -> Self {
components.inner.into_vec()
}
}
impl From<Vec<Component>> for Components {
fn from(components: Vec<Component>) -> Self {
Self {
inner: ComponentsInner::from_vec(components),
}
}
}
impl std::ops::Deref for Components {
type Target = [Component];
fn deref(&self) -> &Self::Target {
self.list()
}
}
impl std::ops::DerefMut for Components {
fn deref_mut(&mut self) -> &mut Self::Target {
self.list_mut()
}
}
impl<'a> IntoIterator for &'a Components {
type Item = &'a Component;
type IntoIter = std::slice::Iter<'a, Component>;
fn into_iter(self) -> Self::IntoIter {
self.list().iter()
}
}
impl<'a> IntoIterator for &'a mut Components {
type Item = &'a mut Component;
type IntoIter = std::slice::IterMut<'a, Component>;
fn into_iter(self) -> Self::IntoIter {
self.list_mut().iter_mut()
}
}
impl Components {
pub fn new() -> Self {
Self {
inner: ComponentsInner::new(),
}
}
pub fn new_with_refreshed_list() -> Self {
let mut components = Self::new();
components.refresh(true);
components
}
pub fn list(&self) -> &[Component] {
self.inner.list()
}
pub fn list_mut(&mut self) -> &mut [Component] {
self.inner.list_mut()
}
pub fn refresh(&mut self, remove_not_listed_components: bool) {
self.inner.refresh();
if remove_not_listed_components {
self.inner.components.retain_mut(|c| {
if !c.inner.updated {
return false;
}
c.inner.updated = false;
true
});
}
}
}
pub struct Component {
pub(crate) inner: ComponentInner,
}
impl Component {
pub fn temperature(&self) -> Option<f32> {
self.inner.temperature()
}
pub fn max(&self) -> Option<f32> {
self.inner.max()
}
pub fn critical(&self) -> Option<f32> {
self.inner.critical()
}
pub fn label(&self) -> &str {
self.inner.label()
}
pub fn id(&self) -> Option<&str> {
self.inner.id()
}
pub fn refresh(&mut self) {
self.inner.refresh()
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn test_components_mac_m1() {
let mut components = Components::new();
components.refresh(false);
components.refresh(false);
}
}