sinf 0.1.0

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

use super::ffi::{SMBIOSSystemInformation, get_smbios_table, parse_smbios, string_value};

pub(crate) struct ProductInner;

impl ProductInner {
    fn field(index: impl FnOnce(&SMBIOSSystemInformation) -> u8) -> Option<String> {
        let table = get_smbios_table()?;
        let (info, strings) = parse_smbios::<SMBIOSSystemInformation>(&table, 1)?;
        string_value(&strings, index(&info))
    }

    pub(crate) fn family() -> Result<String, Error> {
        Self::field(|info| info.family)
            .ok_or_else(|| Error::Other("failed to retrieve product family".into()))
    }

    pub(crate) fn name() -> Result<String, Error> {
        Self::field(|info| info.product_name)
            .ok_or_else(|| Error::Other("failed to retrieve product name".into()))
    }

    pub(crate) fn serial_number() -> Result<String, Error> {
        Self::field(|info| info.serial_number)
            .ok_or_else(|| Error::Other("failed to retrieve product serial number".into()))
    }

    pub(crate) fn stock_keeping_unit() -> Result<String, Error> {
        Self::field(|info| info.sku_number)
            .ok_or_else(|| Error::Other("failed to retrieve product stock keeping unit".into()))
    }

    pub(crate) fn uuid() -> Result<String, Error> {
        let table = get_smbios_table()
            .ok_or_else(|| Error::Other("failed to retrieve product uuid".into()))?;
        let (info, _) = parse_smbios::<SMBIOSSystemInformation>(&table, 1)
            .ok_or_else(|| Error::Other("failed to retrieve product uuid".into()))?;
        Ok(info.uuid.to_string())
    }

    pub(crate) fn version() -> Result<String, Error> {
        Self::field(|info| info.version)
            .ok_or_else(|| Error::Other("failed to retrieve product version".into()))
    }

    pub(crate) fn vendor_name() -> Result<String, Error> {
        Self::field(|info| info.manufacturer)
            .ok_or_else(|| Error::Other("failed to retrieve product vendor name".into()))
    }
}