sinf 0.1.0

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

mod connection;

use self::connection::Connection;

pub(crate) struct ComponentInner {
    temperature: f32,
    max: f32,
    critical: Option<f32>,
    label: String,
    connection: Option<Connection>,
    pub(crate) updated: bool,
}

impl ComponentInner {
    /// Creates a `ComponentInner` with the given information.
    fn new() -> Option<Self> {
        let mut c = Connection::new()
            .and_then(|x| x.create_instance())
            .and_then(|x| x.connect_server())
            .and_then(|x| x.set_proxy_blanket())
            .and_then(|x| x.exec_query())?;

        c.temperature(true)
            .map(|(temperature, critical)| ComponentInner {
                temperature,
                label: "Computer".to_owned(),
                max: temperature,
                critical,
                connection: Some(c),
                updated: true,
            })
    }

    pub(crate) fn temperature(&self) -> Option<f32> {
        Some(self.temperature)
    }

    pub(crate) fn max(&self) -> Option<f32> {
        Some(self.max)
    }

    pub(crate) fn critical(&self) -> Option<f32> {
        self.critical
    }

    pub(crate) fn label(&self) -> &str {
        &self.label
    }

    pub(crate) fn id(&self) -> Option<&str> {
        Some(&self.label)
    }

    pub(crate) fn refresh(&mut self) {
        if self.connection.is_none() {
            self.connection = Connection::new()
                .and_then(|x| x.create_instance())
                .and_then(|x| x.connect_server())
                .and_then(|x| x.set_proxy_blanket());
        }
        self.connection = if let Some(x) = self.connection.take() {
            x.exec_query()
        } else {
            None
        };
        if let Some(ref mut connection) = self.connection
            && let Some((temperature, _)) = connection.temperature(false)
        {
            self.temperature = temperature;
            if self.temperature > self.max {
                self.max = self.temperature;
            }
        }
    }
}

pub(crate) struct ComponentsInner {
    pub(crate) components: Vec<Component>,
}

impl ComponentsInner {
    pub(crate) fn new() -> Result<Self, Error> {
        Ok(Self {
            components: Vec::new(),
        })
    }

    pub(crate) fn list(&self) -> &[Component] {
        &self.components
    }

    pub(crate) fn list_mut(&mut self) -> &mut [Component] {
        &mut self.components
    }

    pub(crate) fn refresh(&mut self) {
        if self.components.is_empty() {
            self.components = match ComponentInner::new() {
                Some(c) => vec![Component { inner: c }],
                None => Vec::new(),
            };
        } else {
            // Only one entry is expected here, but iterate regardless.
            for c in self.components.iter_mut() {
                c.refresh();
                c.inner.updated = true;
            }
        }
    }
}