sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use crate::{ComponentsInner, Error};

use super::Component;

/// A collection of components.
#[doc = include_str!("../../docs/snippets/component.md")]
pub struct Components {
    pub(crate) inner: ComponentsInner,
}

impl_collection_method!(Components, Component, inner, list, list_mut);

impl Components {
    /// Creates an empty [`Components`].
    pub fn new() -> Result<Self, Error> {
        Ok(Self {
            inner: ComponentsInner::new()?,
        })
    }

    impl_new_with_refreshed_list!(Components, true);

    /// Refreshes the component list.
    pub fn refresh(&mut self, remove_not_listed_components: bool) {
        self.inner.refresh();
        if remove_not_listed_components {
            // Remove components that are no longer present.
            self.inner.components.retain_mut(|c| {
                if !c.inner.updated {
                    return false;
                }
                c.inner.updated = false;
                true
            });
        } else {
            for c in self.inner.components.iter_mut() {
                c.inner.updated = false;
            }
        }
    }
}