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