mecha_device_oem_info/
lib.rs

1use anyhow::Result;
2use mac_address::get_mac_address;
3use std::fs;
4use std::io::prelude::*;
5
6#[derive(Debug)]
7pub struct DistroInfo {
8    pub id: String,
9    pub name: String,
10    pub version: String,
11    pub version_id: String,
12    pub pretty_name: String,
13    pub distro_codename: String,
14    pub mac_address: String,
15}
16
17impl DistroInfo {
18    pub fn get_distro_info() -> Result<DistroInfo> {
19        let mac_address = match get_mac_address() {
20            Ok(mac) => mac.map(|m| m.to_string()).unwrap_or_else(String::new),
21            Err(_) => String::from("No MAC address found"),
22        };
23
24        let mut file = fs::File::open("/etc/os-release")?;
25        let mut contents = String::new();
26        file.read_to_string(&mut contents)?;
27
28        let mut info = DistroInfo {
29            id: String::new(),
30            name: String::new(),
31            version: String::new(),
32            version_id: String::new(),
33            pretty_name: String::new(),
34            distro_codename: String::new(),
35            mac_address: mac_address,
36        };
37
38        for line in contents.lines() {
39            let mut parts = line.split('=');
40            if let Some(key) = parts.next() {
41                if let Some(value) = parts.next() {
42                    match key {
43                        "ID" => info.id = value.trim().to_string(),
44                        "NAME" => info.name = value.trim().to_string(),
45                        "VERSION" => info.version = value.trim().to_string(),
46                        "VERSION_ID" => info.version_id = value.trim().to_string(),
47                        "PRETTY_NAME" => info.pretty_name = value.trim().to_string(),
48                        "DISTRO_CODENAME" => info.distro_codename = value.trim().to_string(),
49
50                        _ => {} // Ignore other keys
51                    }
52                }
53            }
54        }
55
56        Ok(info)
57    }
58}