Skip to main content

os_identifier/model/
mod.rs

1mod linux;
2pub(crate) use linux::*;
3
4mod windows;
5pub(crate) use windows::*;
6
7const ERR_UNKNOWN_OS: &str = "Unknown operating system:";
8
9// Public interface
10#[derive(Debug)]
11pub struct OS(OperatingSystem);
12
13#[derive(Debug)]
14enum OperatingSystem {
15    Linux(Linux),
16    Windows(Windows),
17}
18
19// Public interface
20#[derive(Debug)]
21pub struct Linux(linux::Linux);
22
23// Public interface
24#[derive(Debug)]
25pub struct Windows(windows::Windows);
26
27impl OS {
28    pub fn parse(label: &str) -> Result<OS, String> {
29        let os = OperatingSystem::try_from(label)?;
30
31        Ok(OS(os))
32    }
33
34    pub fn vendor(&self) -> String {
35        match &self.0 {
36            OperatingSystem::Linux(l) => l.vendor(),
37            OperatingSystem::Windows(w) => w.vendor(),
38        }
39    }
40
41    pub fn product(&self) -> String {
42        match &self.0 {
43            OperatingSystem::Linux(l) => l.product(),
44            OperatingSystem::Windows(w) => w.product(),
45        }
46    }
47
48    pub fn release(&self) -> String {
49        match &self.0 {
50            OperatingSystem::Linux(l) => l.release(),
51            OperatingSystem::Windows(w) => w.release(),
52        }
53    }
54    
55    pub fn is_enterprise(&self) -> bool {
56        match &self.0 {
57            OperatingSystem::Linux(l) => l.is_enterprise(),
58            OperatingSystem::Windows(w) => w.is_enterprise(),
59        }
60    }
61
62    pub fn is_lts(&self) -> bool {
63        match &self.0 {
64            OperatingSystem::Linux(l) => l.is_lts(),
65            OperatingSystem::Windows(w) => w.is_lts(),
66        }
67    }
68    
69    pub fn to_string(&self) -> Vec<String> {
70        match &self.0 {
71            OperatingSystem::Linux(os) => {
72                os.to_string()
73            },
74            OperatingSystem::Windows(os) => {
75                os.to_string()
76            },
77        }
78    }
79}
80
81impl Linux {
82    pub fn parse(label: &str) -> Result<Linux, String> {
83        let linux = linux::Linux::try_from(label)?;
84
85        Ok(Linux(linux))
86    }
87
88    pub fn vendor(&self) -> String {
89        self.0.vendor()
90    }
91
92    pub fn product(&self) -> String {
93        self.0.product()
94    }
95
96    pub fn release(&self) -> String {
97        self.0.release()
98    }
99    
100    pub fn is_enterprise(&self) -> bool {
101        self.0.is_enterprise()
102    }
103
104    pub fn is_lts(&self) -> bool {
105        self.0.is_lts()
106    }
107    
108    pub fn to_string(&self) -> Vec<String> {
109        self.0.to_string()
110    }
111}
112
113impl Windows {
114    pub fn parse(label: &str) -> Result<Windows, String> {
115        let windows = windows::Windows::try_from(label)?;
116
117        Ok(Windows(windows))
118    }
119
120    pub fn vendor(&self) -> String {
121        self.0.vendor()
122    }
123
124    pub fn product(&self) -> String {
125        self.0.product()
126    }
127
128    pub fn release(&self) -> String {
129        self.0.release()
130    }
131    
132    pub fn is_enterprise(&self) -> bool {
133        self.0.is_enterprise()
134    }
135
136    pub fn is_lts(&self) -> bool {
137        self.0.is_lts()
138    }
139    
140    pub fn to_string(&self) -> Vec<String> {
141        self.0.to_string()
142    }
143}
144
145impl TryFrom<&str> for OperatingSystem {
146    type Error = String;
147
148    fn try_from(value: &str) -> Result<Self, Self::Error> {
149        if let Ok(windows) = windows::Windows::try_from(value) {
150            Ok(OperatingSystem::Windows(Windows(windows)))
151        } else if let Ok(linux) = linux::Linux::try_from(value) {
152            Ok(OperatingSystem::Linux(Linux(linux)))
153        } else {
154            Err(format!("{} \"{}\"", ERR_UNKNOWN_OS, value))
155        }
156    }
157}
158
159/*
160impl std::fmt::Display for OS {
161    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
162        write!(f, "{}", self.0.to_string())
163    }
164}
165*/
166
167/*
168impl std::fmt::Display for OperatingSystem {
169    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170        match self {
171            OperatingSystem::Windows(os) => {
172                write!(f, "{}", os.to_string())
173            }
174        }
175    }
176}
177*/
178
179/*
180impl std::fmt::Display for Windows {
181    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
182        // todo: remove debug :?
183        write!(f, "{:?}", self.0.to_string())
184    }
185}
186*/