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