1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use serde::{Deserialize, Serialize};
use std::convert::{Infallible, TryInto};
use std::fmt;
use std::fmt::Formatter;
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Hash)]
#[serde(transparent)]
pub struct OperatingSystem(pub String);
impl TryInto<bool> for OperatingSystem {
type Error = Infallible;
fn try_into(self) -> Result<bool, Self::Error> {
let OperatingSystem(expected) = self;
tracing::trace!(
os = std::env::consts::OS,
%expected,
"checking if current operating system matches expected",
);
Ok(expected == std::env::consts::OS)
}
}
impl fmt::Display for OperatingSystem {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let OperatingSystem(os) = self;
write!(f, "OPERATING SYSTEM == {os}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_correct_os() {
let os = OperatingSystem(std::env::consts::OS.to_owned());
let is_os: bool = os.try_into().expect("failed to check operating system");
assert!(is_os);
}
#[test]
#[cfg(not(target_os = "windows"))]
fn test_incorrect_os() {
let os = OperatingSystem(String::from("windows"));
let is_os: bool = os.try_into().expect("failed to check operating system");
assert!(!is_os);
}
#[test]
#[cfg(target_os = "windows")]
fn test_incorrect_os() {
let os = OperatingSystem(String::from("linux"));
let is_os: bool = os.try_into().expect("failed to check operating system");
assert!(!is_os);
}
}