sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
#![forbid(unsafe_code)]
#![doc = include_str!("../../docs/snippets/hardware.md")]

//! Hardware inventory: firmware, PCI, USB, power, sensors, displays and storage.

mod battery;
mod display;
mod firmware;
mod network;
mod pci;
mod sensor;
mod storage;
mod usb;

pub use self::battery::Battery;
pub use self::display::Display;
pub use self::firmware::{Bios, Board, Chassis, MemoryDevice};
pub use self::network::NetworkDevice;
pub use self::pci::PciDevice;
pub use self::sensor::{Sensor, SensorKind};
pub use self::storage::StorageDevice;
pub use self::usb::UsbDevice;

use crate::Error;

/// Hardware inventory collected from the operating system.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Hardware {
    /// BIOS information.
    pub bios: Option<Bios>,
    /// Motherboard information.
    pub board: Option<Board>,
    /// Chassis information.
    pub chassis: Option<Chassis>,
    /// Installed memory modules.
    pub memory_devices: Vec<MemoryDevice>,
    /// Network interfaces.
    pub network_devices: Vec<NetworkDevice>,
    /// PCI devices.
    pub pci_devices: Vec<PciDevice>,
    /// USB devices.
    pub usb_devices: Vec<UsbDevice>,
    /// Batteries.
    pub batteries: Vec<Battery>,
    /// Non-temperature sensors.
    pub sensors: Vec<Sensor>,
    /// Connected displays.
    pub displays: Vec<Display>,
    /// Physical block devices.
    pub storage_devices: Vec<StorageDevice>,
}

impl Hardware {
    /// Creates a hardware inventory snapshot.
    pub fn new() -> Result<Self, Error> {
        crate::sys::hardware::load()
    }

    /// Reloads the hardware inventory.
    pub fn refresh(&mut self) {
        if let Ok(hardware) = crate::sys::hardware::load() {
            *self = hardware;
        }
    }
}