Skip to main content

os_identifier/model/
mod.rs

1mod windows;
2pub(crate) use windows::*;
3
4const ERR_UNKNOWN_OS: &str = "Unknown operating system:";
5
6// Public interface
7pub struct OS(OperatingSystem);
8
9enum OperatingSystem {
10    Windows(Windows),
11}
12
13// Public interface
14pub struct Windows(windows::Windows);
15
16impl OS {
17    pub fn parse(label: &str) -> Result<OS, String> {
18        let os = OperatingSystem::try_from(label)?;
19
20        Ok(OS(os))
21    }
22
23    pub fn vendor(&self) -> String {
24        match &self.0 {
25            OperatingSystem::Windows(w) => w.vendor(),
26        }
27    }
28
29    pub fn product(&self) -> String {
30        match &self.0 {
31            OperatingSystem::Windows(w) => w.product(),
32        }
33    }
34}
35
36impl Windows {
37    pub fn parse(label: &str) -> Result<Windows, String> {
38        let windows = windows::Windows::try_from(label)?;
39
40        Ok(Windows(windows))
41    }
42
43    pub fn vendor(&self) -> String {
44        self.0.vendor()
45    }
46
47    pub fn product(&self) -> String {
48        self.0.product()
49    }
50}
51
52impl TryFrom<&str> for OperatingSystem {
53    type Error = String;
54
55    fn try_from(value: &str) -> Result<Self, Self::Error> {
56        if let Ok(windows) = windows::Windows::try_from(value) {
57            Ok(OperatingSystem::Windows(Windows(windows)))
58        } else {
59            Err(format!("{} \"{}\"", ERR_UNKNOWN_OS, value))
60        }
61    }
62}
63
64impl std::fmt::Display for OS {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(f, "{}", self.0.to_string())
67    }
68}
69
70impl std::fmt::Display for OperatingSystem {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        match self {
73            OperatingSystem::Windows(os) => {
74                write!(f, "{}", os.to_string())
75            }
76        }
77    }
78}
79
80impl std::fmt::Display for Windows {
81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82        // todo: remove debug :?
83        write!(f, "{:?}", self.0.to_string())
84    }
85}