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
14#[derive(Debug)]
15pub struct Windows(windows::Windows);
16
17impl OS {
18    pub fn parse(label: &str) -> Result<OS, String> {
19        let os = OperatingSystem::try_from(label)?;
20
21        Ok(OS(os))
22    }
23
24    pub fn vendor(&self) -> String {
25        match &self.0 {
26            OperatingSystem::Windows(w) => w.vendor(),
27        }
28    }
29
30    pub fn product(&self) -> String {
31        match &self.0 {
32            OperatingSystem::Windows(w) => w.product(),
33        }
34    }
35
36    pub fn to_string(&self) -> Vec<String> {
37        match &self.0 {
38            OperatingSystem::Windows(os) => {
39                os.to_string()
40            }
41        }
42    }
43}
44
45impl Windows {
46    pub fn parse(label: &str) -> Result<Windows, String> {
47        let windows = windows::Windows::try_from(label)?;
48
49        Ok(Windows(windows))
50    }
51
52    pub fn vendor(&self) -> String {
53        self.0.vendor()
54    }
55
56    pub fn product(&self) -> String {
57        self.0.product()
58    }
59
60    pub fn to_string(&self) -> Vec<String> {
61        self.0.to_string()
62    }
63}
64
65impl TryFrom<&str> for OperatingSystem {
66    type Error = String;
67
68    fn try_from(value: &str) -> Result<Self, Self::Error> {
69        if let Ok(windows) = windows::Windows::try_from(value) {
70            Ok(OperatingSystem::Windows(Windows(windows)))
71        } else {
72            Err(format!("{} \"{}\"", ERR_UNKNOWN_OS, value))
73        }
74    }
75}
76
77/*
78impl std::fmt::Display for OS {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        write!(f, "{}", self.0.to_string())
81    }
82}
83*/
84
85/*
86impl std::fmt::Display for OperatingSystem {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        match self {
89            OperatingSystem::Windows(os) => {
90                write!(f, "{}", os.to_string())
91            }
92        }
93    }
94}
95*/
96
97/*
98impl std::fmt::Display for Windows {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        // todo: remove debug :?
101        write!(f, "{:?}", self.0.to_string())
102    }
103}
104*/