rcpufetch 0.0.5

[ALPHA] A rusty crossplatform, but simple CLI binutil for reading CPU information.
//! PowerPC/POWER detection from the PVR (Processor Version Register). On Linux the
//! kernel already exposes a decoded name in `/proc/cpuinfo`'s "cpu" field, so the OS
//! layer prefers that; the PVR table here is the fallback (and the only option on
//! BSDs where an equivalent decoded string isn't available).

use super::{ArchInfo, tables::ppc_uarch};

fn lookup_tag(pvr: u32) -> Option<&'static str> {
    ppc_uarch::PPC_PVR
        .iter()
        .find(|&&(mask, value, _)| (pvr & mask) == value)
        .map(|&(_, _, tag)| tag)
}

fn name_for_tag(tag: &str) -> (&'static str, Option<u32>) {
    ppc_uarch::PPC_NAMES
        .iter()
        .find(|&&(t, _, _)| t == tag)
        .map(|&(_, name, proc)| (name, proc))
        .unwrap_or(("Unknown", None))
}

pub fn detect_from_pvr(pvr: u32) -> ArchInfo {
    let tag = lookup_tag(pvr).unwrap_or("UNKNOWN");
    let (name, process_nm) = name_for_tag(tag);
    ArchInfo {
        vendor: Some("IBM".to_string()),
        uarch: Some(name.to_string()),
        soc: None,
        process_nm,
        features: Vec::new(),
    }
}

/// Best-effort match against a kernel-decoded name string (e.g. Linux `/proc/cpuinfo`'s
/// "cpu" field already says "POWER9 (raw), altivec supported").
pub fn detect_from_name_str(s: &str) -> ArchInfo {
    let lower = s.to_ascii_lowercase();
    let features = if lower.contains("altivec") {
        vec!["altivec".to_string()]
    } else {
        Vec::new()
    };
    let uarch = ppc_uarch::PPC_NAMES
        .iter()
        .find(|&&(_, name, _)| lower.contains(&name.to_ascii_lowercase()))
        .map(|&(_, name, _)| name.to_string());
    let process_nm = uarch.as_deref().and_then(|u| {
        ppc_uarch::PPC_NAMES
            .iter()
            .find(|&&(_, name, _)| name == u)
            .and_then(|&(_, _, p)| p)
    });
    ArchInfo {
        vendor: Some("IBM".to_string()),
        uarch,
        soc: None,
        process_nm,
        features,
    }
}