rcpufetch 0.0.5

[ALPHA] A rusty crossplatform, but simple CLI binutil for reading CPU information.
//! RISC-V detection. There is no CPUID-equivalent instruction usable from userspace, so
//! (mirroring upstream cpufetch) detection relies on text: the device-tree "uarch"/
//! compatible string exposed via `/proc/cpuinfo`, or `marchid` when available.

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

pub fn detect_from_uarch_str(s: &str) -> ArchInfo {
    match riscv_uarch::RISCV_UARCH_STR
        .iter()
        .find(|&&(needle, _, _)| needle == s)
    {
        Some(&(_, name, vendor)) => ArchInfo {
            vendor: Some(vendor.to_string()),
            uarch: Some(name.to_string()),
            soc: None,
            process_nm: None,
            features: Vec::new(),
        },
        None => ArchInfo::default(),
    }
}

pub fn detect_from_marchid(marchid: u64) -> ArchInfo {
    match riscv_uarch::RISCV_UARCH_MARCHID
        .iter()
        .find(|&&(id, _, _)| id == marchid)
    {
        Some(&(_, name, vendor)) => ArchInfo {
            vendor: Some(vendor.to_string()),
            uarch: Some(name.to_string()),
            soc: None,
            process_nm: None,
            features: Vec::new(),
        },
        None => ArchInfo::default(),
    }
}

pub fn soc_from_compatible(compatible: &str) -> Option<&'static str> {
    let lower = compatible.to_ascii_lowercase();
    riscv_uarch::RISCV_SOCS
        .iter()
        .find(|(needle, _)| lower.contains(needle))
        .map(|(_, name)| *name)
}