sinf 0.1.0

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

use crate::hardware::NetworkDevice;

use super::helpers::read_link_name;

pub(super) fn load() -> Vec<NetworkDevice> {
    let Ok(entries) = fs::read_dir("/sys/class/net") else {
        return Vec::new();
    };
    let mut devices: Vec<NetworkDevice> = entries
        .flatten()
        .filter_map(|entry| {
            let path = entry.path();
            let name = path.file_name()?.to_string_lossy().into_owned();
            let device = path.join("device");
            let physical = device.exists();
            let pci_address = fs::canonicalize(&device)
                .ok()
                .and_then(|path| path.file_name().map(|n| n.to_string_lossy().into_owned()))
                .filter(|name| name.contains(':'));
            Some(NetworkDevice {
                name,
                driver: read_link_name(&device.join("driver")),
                pci_address,
                virtual_device: !physical,
            })
        })
        .collect();
    devices.sort_by(|a, b| a.name.cmp(&b.name));
    devices
}