node_js_release_info/
os.rs1use crate::error::NodeJSRelInfoError;
2#[cfg(feature = "json")]
3use serde::{Deserialize, Serialize};
4use std::env::consts::OS;
5use std::fmt::{Display, Formatter};
6use std::str::FromStr;
7
8#[derive(Clone, Debug, PartialEq)]
9#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
10pub enum NodeJSOS {
11 #[cfg_attr(feature = "json", serde(rename = "linux"))]
12 Linux,
13 #[cfg_attr(feature = "json", serde(rename = "darwin"))]
14 Darwin,
15 #[cfg_attr(feature = "json", serde(rename = "win"))]
16 Windows,
17 #[cfg_attr(feature = "json", serde(rename = "aix"))]
18 AIX,
19}
20
21impl Default for NodeJSOS {
22 fn default() -> Self {
23 NodeJSOS::new()
24 }
25}
26
27impl NodeJSOS {
28 pub fn new() -> NodeJSOS {
29 NodeJSOS::Linux
30 }
31
32 pub fn from_env() -> Result<NodeJSOS, NodeJSRelInfoError> {
33 NodeJSOS::from_str(OS)
34 }
35}
36
37impl Display for NodeJSOS {
38 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39 let os = match self {
40 NodeJSOS::Linux => "linux",
41 NodeJSOS::Darwin => "darwin",
42 NodeJSOS::Windows => "win",
43 NodeJSOS::AIX => "aix",
44 };
45
46 write!(f, "{}", os)
47 }
48}
49
50impl FromStr for NodeJSOS {
51 type Err = NodeJSRelInfoError;
52
53 fn from_str(s: &str) -> Result<NodeJSOS, NodeJSRelInfoError> {
54 match s {
55 "linux" => Ok(NodeJSOS::Linux),
56 "darwin" | "macos" => Ok(NodeJSOS::Darwin),
57 "windows" | "win" => Ok(NodeJSOS::Windows),
58 "aix" => Ok(NodeJSOS::AIX),
59 _ => Err(NodeJSRelInfoError::UnrecognizedOs(s.to_string())),
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn it_initializes() {
70 let os = NodeJSOS::new();
71 assert_eq!(os, NodeJSOS::Linux);
72 }
73
74 #[test]
75 fn it_initializes_with_defaults() {
76 let os = NodeJSOS::default();
77 assert_eq!(os, NodeJSOS::Linux);
78 }
79
80 #[test]
81 fn it_initializes_from_str() {
82 let os = NodeJSOS::from_str("linux").unwrap();
83
84 assert_eq!(os, NodeJSOS::Linux);
85
86 let os = NodeJSOS::from_str("darwin").unwrap();
87
88 assert_eq!(os, NodeJSOS::Darwin);
89
90 let os = NodeJSOS::from_str("macos").unwrap();
91
92 assert_eq!(os, NodeJSOS::Darwin);
93
94 let os = NodeJSOS::from_str("windows").unwrap();
95
96 assert_eq!(os, NodeJSOS::Windows);
97
98 let os = NodeJSOS::from_str("win").unwrap();
99
100 assert_eq!(os, NodeJSOS::Windows);
101
102 let os = NodeJSOS::from_str("aix").unwrap();
103
104 assert_eq!(os, NodeJSOS::AIX);
105 }
106
107 #[test]
108 fn it_serializes_to_str() {
109 let text = format!("{}", NodeJSOS::Linux);
110
111 assert_eq!(text, "linux");
112
113 let text = format!("{}", NodeJSOS::Darwin);
114
115 assert_eq!(text, "darwin");
116
117 let text = format!("{}", NodeJSOS::Windows);
118
119 assert_eq!(text, "win");
120
121 let text = format!("{}", NodeJSOS::AIX);
122
123 assert_eq!(text, "aix");
124 }
125
126 #[test]
127 fn it_initializes_using_current_environment() {
128 NodeJSOS::from_env().unwrap();
129 }
130
131 #[test]
132 #[should_panic(
133 expected = "called `Result::unwrap()` on an `Err` value: UnrecognizedOs(\"NOPE!\")"
134 )]
135 fn it_fails_when_os_cannot_be_determined_from_str() {
136 NodeJSOS::from_str("NOPE!").unwrap();
137 }
138
139 #[test]
140 fn it_serializes_and_deserializes() {
141 let os_json = serde_json::to_string(&NodeJSOS::Darwin).unwrap();
142 let os: NodeJSOS = serde_json::from_str(&os_json).unwrap();
143 assert_eq!(os, NodeJSOS::Darwin);
144 }
145}