1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use core::fmt::{Display, Formatter, Result};
use std::path::Path;
use std::fs;

#[cfg(target_os = "windows")]
use winreg::{enums::HKEY_LOCAL_MACHINE, RegKey};

pub struct Environment;

#[derive(Debug, PartialEq)]
pub enum OperatingSystem {
    Linux,
    Android,
    FreeBSD,
    DragonFlyBSD,
    NetBSD,
    OpenBSD,
    Solaris,
    MacOS,
    Windows,
    Unknown,
}

#[derive(Debug, PartialEq)]
pub enum Architecture {
    X86,
    X86_64,
    Arm,
    Aarch64,
    Loongarch64,
    M68k,
    Csky,
    Mips,
    Mips64,
    Powerpc,
    Powerpc64,
    Riscv64,
    S390x,
    Sparc64,
    Unknown,
}

pub trait LinuxSystem {
    fn get_distro(self) -> String;
    fn get_linux_version(self) -> String;
    fn is_subsystem_env(self) -> bool;
    fn cpuinfo_cores(self) -> u32;
    fn cpuinfo_model(self) -> String;
}

pub trait WindowsSystem {
    fn get_edition(self) -> String;
}

pub trait CrossPlatform {
    fn get_os(self) -> OperatingSystem;
    fn get_arch(self) -> Architecture;
}

pub trait Parser {
    fn select(path: &'static str, env_var: &'static str, elem: char) -> String;
}

impl Parser for String {
    fn select(path: &'static str, env_var: &'static str, elem: char) -> String {
        let contents = fs::read_to_string(path).expect("Failed to read file");
        
        let capture = contents
        .lines()
        .find(|line| line.starts_with(env_var))
        .expect("Failed to find the specified environment variable")
        .split(elem)
        .nth(1)
        .expect("Failed to parse environment variable")
        .trim_matches('"')
        .to_string();
    capture
}
}

impl LinuxSystem for Environment {
    fn get_distro(self) -> String {
        String::select("/etc/os-release", "NAME", '=')
    }

    fn get_linux_version(self) -> String {
        String::select("/etc/os-release", "VERSION_ID", '=')
    }
    
    fn is_subsystem_env(self) -> bool {
        Path::new("/proc/sys/fs/binfmt_misc/WSLInterop").exists()
    }

    fn cpuinfo_cores(self) -> u32 {
        String::select("/proc/cpuinfo", "cpu cores", ':')
            .trim()
            .parse::<u32>()
            .expect("Failed to parse String to unsigned int")
    }

    fn cpuinfo_model(self) -> String {
        String::select("/proc/cpuinfo", "model name", ':')
            .trim()
            .to_string()
    }
}

impl WindowsSystem for Environment {
    fn get_edition(self) -> String {
        let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
        let subkey = hklm
            .open_subkey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
            .expect("Failed to open subkey");
        
        let edition = subkey
            .get_value::<String, _>("EditionID")
            .expect("Failed to get value");
        edition
    }
}

impl CrossPlatform for Environment {
    fn get_os(self) -> OperatingSystem {
        match std::env::consts::OS {
            "linux" => OperatingSystem::Linux,
            "android" => OperatingSystem::Android,
            "freebsd" => OperatingSystem::FreeBSD,
            "dragonfly" => OperatingSystem::DragonFlyBSD,
            "netbsd" => OperatingSystem::NetBSD,
            "openbsd" => OperatingSystem::OpenBSD,
            "solaris" => OperatingSystem::Solaris,
            "macos" => OperatingSystem::MacOS,
            "windows" => OperatingSystem::Windows,
            _ => OperatingSystem::Unknown,
        }
    }

    fn get_arch(self) -> Architecture {
        match std::env::consts::ARCH {
            "x86" => Architecture::X86,
            "x86_64" => Architecture::X86_64,
            "arm" => Architecture::Arm,
            "aarch64" => Architecture::Aarch64,
            "loongarch64" => Architecture::Loongarch64,
            "m68k" => Architecture::M68k,
            "csky" => Architecture::Csky,
            "mips" => Architecture::Mips,
            "mips64" => Architecture::Mips64,
            "powerpc" => Architecture::Powerpc,
            "powerpc64" => Architecture::Powerpc64,
            "riscv64" => Architecture::Riscv64,
            "s390x" => Architecture::S390x,
            "sparc64" => Architecture::Sparc64,
            _ => Architecture::Unknown,
        }
    }
}

// impl_display: Implements the Display trait for OperatingSystem and Architecture
macro_rules! impl_display {
    ($type:ident) => {
        impl Display for $type {
            fn fmt(&self, f: &mut Formatter<'_>) -> Result {
                write!(f, "{:?}", self)
            }
        }
    };
}

impl_display!(OperatingSystem);
impl_display!(Architecture);

#[cfg(test)]
mod tests {
    // Note(may not be immediately obvious): Test depends on my local environment for 100% pass.
    // Why? Testing for the correct behavior, not necessarily the value...
    // i.e., if the test fails on your machine, it is not inherently a bug.
    // To prevent unnecessary Tickets, read the error message beforehand.
    use super::*;     

    #[cfg(target_os = "linux")]
    #[test]
    fn test_get_distro() {
        let distro = Environment.get_distro();
        assert_eq!(distro, "Fedora Linux");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_get_linux_version() {
        let version = Environment.get_linux_version();
        assert_eq!(version, "39");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_is_subsystem_env() {
        let is_subsystem = Environment.is_subsystem_env();
        assert_eq!(is_subsystem, false);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_cpuinfo_cores() {
        let cores = Environment.cpuinfo_cores();
        assert_eq!(cores, 8);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_cpuinfo_model() {
        let model = Environment.cpuinfo_model();
        assert_eq!(model, "Ryzen 7 5700x 8-Core Processor");
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn test_get_edition() {
        let edition = Environment.get_edition();
        assert_eq!(edition, "Professional");
        println!("Edition: {}", edition);
    }

    #[test]
    fn test_get_os() {
        let os = Environment.get_os();
        assert_eq!(os, OperatingSystem::Windows);
        println!("Operating System: {}", os);
    }

    #[test]
    fn test_get_arch() {
        let arch = Environment.get_arch();
        assert_eq!(arch, Architecture::X86_64);
        println!("Architecture: {}", arch);
    }
}