node_js_release_info/
arch.rs

1use crate::error::NodeJSRelInfoError;
2#[cfg(feature = "json")]
3use serde::{Deserialize, Serialize};
4use std::env::consts::ARCH;
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 NodeJSArch {
11    #[cfg_attr(feature = "json", serde(rename = "x64"))]
12    X64,
13    #[cfg_attr(feature = "json", serde(rename = "x86"))]
14    X86,
15    #[cfg_attr(feature = "json", serde(rename = "arm64"))]
16    ARM64,
17    #[cfg_attr(feature = "json", serde(rename = "armv7l"))]
18    ARMV7L,
19    #[cfg_attr(feature = "json", serde(rename = "ppc64"))]
20    PPC64,
21    #[cfg_attr(feature = "json", serde(rename = "ppc64le"))]
22    PPC64LE,
23    #[cfg_attr(feature = "json", serde(rename = "s390x"))]
24    S390X,
25}
26
27impl Default for NodeJSArch {
28    fn default() -> Self {
29        NodeJSArch::new()
30    }
31}
32
33impl NodeJSArch {
34    pub fn new() -> NodeJSArch {
35        NodeJSArch::X64
36    }
37
38    pub fn from_env() -> Result<NodeJSArch, NodeJSRelInfoError> {
39        NodeJSArch::from_str(ARCH)
40    }
41}
42
43impl Display for NodeJSArch {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        let arch = match self {
46            NodeJSArch::X64 => "x64",
47            NodeJSArch::X86 => "x86",
48            NodeJSArch::ARM64 => "arm64",
49            NodeJSArch::ARMV7L => "armv7l",
50            NodeJSArch::PPC64 => "ppc64",
51            NodeJSArch::PPC64LE => "ppc64le",
52            NodeJSArch::S390X => "s390x",
53        };
54
55        write!(f, "{}", arch)
56    }
57}
58
59impl FromStr for NodeJSArch {
60    type Err = NodeJSRelInfoError;
61
62    fn from_str(s: &str) -> Result<NodeJSArch, NodeJSRelInfoError> {
63        match s {
64            "x64" | "x86_64" => Ok(NodeJSArch::X64),
65            "x86" => Ok(NodeJSArch::X86),
66            "arm64" | "aarch64" => Ok(NodeJSArch::ARM64),
67            "arm" | "armv7l" => Ok(NodeJSArch::ARMV7L),
68            "ppc64" | "powerpc64" => Ok(NodeJSArch::PPC64),
69            "ppc64le" => Ok(NodeJSArch::PPC64LE),
70            "s390x" => Ok(NodeJSArch::S390X),
71            _ => Err(NodeJSRelInfoError::UnrecognizedArch(s.to_string())),
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn it_initializes() {
82        let arch = NodeJSArch::new();
83        assert_eq!(arch, NodeJSArch::X64);
84    }
85
86    #[test]
87    fn it_initializes_with_defaults() {
88        let arch = NodeJSArch::default();
89        assert_eq!(arch, NodeJSArch::X64);
90    }
91
92    #[test]
93    fn it_initializes_from_str() {
94        let arch = NodeJSArch::from_str("x64").unwrap();
95
96        assert_eq!(arch, NodeJSArch::X64);
97
98        let arch = NodeJSArch::from_str("x86_64").unwrap();
99
100        assert_eq!(arch, NodeJSArch::X64);
101
102        let arch = NodeJSArch::from_str("x86").unwrap();
103
104        assert_eq!(arch, NodeJSArch::X86);
105
106        let arch = NodeJSArch::from_str("arm64").unwrap();
107
108        assert_eq!(arch, NodeJSArch::ARM64);
109
110        let arch = NodeJSArch::from_str("aarch64").unwrap();
111
112        assert_eq!(arch, NodeJSArch::ARM64);
113
114        let arch = NodeJSArch::from_str("arm").unwrap();
115
116        assert_eq!(arch, NodeJSArch::ARMV7L);
117
118        let arch = NodeJSArch::from_str("ppc64").unwrap();
119
120        assert_eq!(arch, NodeJSArch::PPC64);
121
122        let arch = NodeJSArch::from_str("ppc64le").unwrap();
123
124        assert_eq!(arch, NodeJSArch::PPC64LE);
125
126        let arch = NodeJSArch::from_str("powerpc64").unwrap();
127
128        assert_eq!(arch, NodeJSArch::PPC64);
129
130        let arch = NodeJSArch::from_str("s390x").unwrap();
131
132        assert_eq!(arch, NodeJSArch::S390X);
133    }
134
135    #[test]
136    fn it_serializes_to_str() {
137        let text = format!("{}", NodeJSArch::X64);
138
139        assert_eq!(text, "x64");
140
141        let text = format!("{}", NodeJSArch::X86);
142
143        assert_eq!(text, "x86");
144
145        let text = format!("{}", NodeJSArch::ARM64);
146
147        assert_eq!(text, "arm64");
148
149        let text = format!("{}", NodeJSArch::ARMV7L);
150
151        assert_eq!(text, "armv7l");
152
153        let text = format!("{}", NodeJSArch::PPC64);
154
155        assert_eq!(text, "ppc64");
156
157        let text = format!("{}", NodeJSArch::PPC64LE);
158
159        assert_eq!(text, "ppc64le");
160
161        let text = format!("{}", NodeJSArch::S390X);
162
163        assert_eq!(text, "s390x");
164    }
165
166    #[test]
167    fn it_initializes_using_current_environment() {
168        NodeJSArch::from_env().unwrap();
169    }
170
171    #[test]
172    #[should_panic(
173        expected = "called `Result::unwrap()` on an `Err` value: UnrecognizedArch(\"NOPE!\")"
174    )]
175    fn it_fails_when_arch_is_unrecognized() {
176        NodeJSArch::from_str("NOPE!").unwrap();
177    }
178
179    #[test]
180    fn it_serializes_and_deserializes() {
181        let arch_json = serde_json::to_string(&NodeJSArch::X64).unwrap();
182        let arch: NodeJSArch = serde_json::from_str(&arch_json).unwrap();
183        assert_eq!(arch, NodeJSArch::X64);
184    }
185}