sinf 0.1.0

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

fn machine_brand(machine: &str) -> &'static str {
    let mut parts = machine.split(',');
    match (parts.next().unwrap_or(""), parts.next()) {
        ("iPhone1", Some("1" | "2")) => "S5L8900",
        ("iPhone2", Some("1")) => "S5L8920",
        ("iPhone3", Some("1" | "2" | "3")) => "A4",
        ("iPhone4", Some("1" | "2")) => "A5",
        ("iPhone5", Some("1" | "2" | "3" | "4")) => "A6",
        ("iPhone6", Some("1" | "2")) => "A7",
        ("iPhone7", Some("1" | "2")) => "A8",
        ("iPhone8", Some("1" | "2" | "4")) => "A9",
        ("iPhone9", Some("1" | "2" | "3" | "4")) => "A10 Fusion",
        ("iPhone10", Some("1" | "2" | "3" | "4" | "5" | "6")) => "A11 Bionic",
        ("iPhone11", Some("1" | "2" | "4" | "6" | "8")) => "A12 Bionic",
        ("iPhone12", Some("1" | "3" | "5" | "8")) => "A13 Bionic",
        ("iPhone13", Some("1" | "2" | "3" | "4")) => "A14 Bionic",
        ("iPhone14", Some("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8")) => "A15 Bionic",
        ("iPhone15", Some("2" | "3" | "4" | "5")) => "A16 Bionic",
        ("iPhone16", Some("1" | "2")) => "A17 Pro",
        ("iPhone17", Some("1" | "2")) => "A18 Pro",
        ("iPhone17", Some("3" | "4")) => "A18",
        ("iPad1", Some("1" | "2")) => "A4",
        ("iPad2", Some("1" | "2" | "3" | "4" | "5" | "6" | "7")) => "A5",
        ("iPad3", Some("1" | "2" | "3")) => "A5X",
        ("iPad3", Some("4" | "5" | "6")) => "A6X",
        ("iPad4", Some("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) => "A7",
        ("iPad5", Some("1" | "2")) => "A8",
        ("iPad5", Some("3" | "4")) => "A8X",
        ("iPad6", Some("3" | "4" | "7" | "8")) => "A9X",
        ("iPad6", Some("11" | "12")) => "A9",
        ("iPad7", Some("1" | "2" | "3" | "4")) => "A10X Fusion",
        ("iPad7", Some("5" | "6" | "11" | "12")) => "A10 Fusion",
        ("iPad8", Some("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8")) => "A12X Bionic",
        ("iPad8", Some("9" | "10" | "11" | "12")) => "A12Z Bionic",
        ("iPad11", Some("1" | "2" | "3" | "4" | "6" | "7")) => "A12 Bionic",
        ("iPad12", Some("1" | "2")) => "A13 Bionic",
        ("iPad13", Some("1" | "2" | "18" | "19")) => "A14 Bionic",
        ("iPad13", Some("4" | "5" | "6" | "7" | "8" | "9" | "10" | "11" | "16" | "17")) => "M1",
        ("iPad14", Some("1" | "2")) => "A15 Bionic",
        ("iPad14", Some("3" | "4" | "5" | "6" | "10" | "11")) => "M2",
        ("iPad14", Some("8" | "9")) => "M1",
        ("iPad16", Some("3" | "4" | "5" | "6")) => "M3",
        _ => "unknown",
    }
}

pub(crate) fn get_vendor_id_and_brand() -> (String, String) {
    // On Apple M1, `sysctl machdep.cpu.vendor` returns "", so fall back to "Apple".
    let vendor = get_sysctl_str(b"machdep.cpu.vendor\0").unwrap_or_else(|| "Apple".to_owned());

    if let Some(brand) = get_sysctl_str(b"machdep.cpu.brand_string\0") {
        return (vendor, brand);
    }

    let machine = get_sysctl_str(b"hw.machine\0").unwrap_or_default();
    (vendor, machine_brand(&machine).to_owned())
}