distant_protocol/common/
system.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5/// Represents information about a system
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct SystemInfo {
8    /// Family of the operating system as described in
9    /// https://doc.rust-lang.org/std/env/consts/constant.FAMILY.html
10    pub family: String,
11
12    /// Name of the specific operating system as described in
13    /// https://doc.rust-lang.org/std/env/consts/constant.OS.html
14    pub os: String,
15
16    /// Architecture of the CPI as described in
17    /// https://doc.rust-lang.org/std/env/consts/constant.ARCH.html
18    pub arch: String,
19
20    /// Current working directory of the running server process
21    pub current_dir: PathBuf,
22
23    /// Primary separator for path components for the current platform
24    /// as defined in https://doc.rust-lang.org/std/path/constant.MAIN_SEPARATOR.html
25    pub main_separator: char,
26
27    /// Name of the user running the server process
28    pub username: String,
29
30    /// Default shell tied to user running the server process
31    pub shell: String,
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn should_be_able_to_serialize_to_json() {
40        let info = SystemInfo {
41            family: String::from("family"),
42            os: String::from("os"),
43            arch: String::from("arch"),
44            current_dir: PathBuf::from("current-dir"),
45            main_separator: '/',
46            username: String::from("username"),
47            shell: String::from("shell"),
48        };
49
50        let value = serde_json::to_value(info).unwrap();
51        assert_eq!(
52            value,
53            serde_json::json!({
54                "family": "family",
55                "os": "os",
56                "arch": "arch",
57                "current_dir": "current-dir",
58                "main_separator": '/',
59                "username": "username",
60                "shell": "shell",
61            })
62        );
63    }
64
65    #[test]
66    fn should_be_able_to_deserialize_from_json() {
67        let value = serde_json::json!({
68            "family": "family",
69            "os": "os",
70            "arch": "arch",
71            "current_dir": "current-dir",
72            "main_separator": '/',
73            "username": "username",
74            "shell": "shell",
75        });
76
77        let info: SystemInfo = serde_json::from_value(value).unwrap();
78        assert_eq!(
79            info,
80            SystemInfo {
81                family: String::from("family"),
82                os: String::from("os"),
83                arch: String::from("arch"),
84                current_dir: PathBuf::from("current-dir"),
85                main_separator: '/',
86                username: String::from("username"),
87                shell: String::from("shell"),
88            }
89        );
90    }
91
92    #[test]
93    fn should_be_able_to_serialize_to_msgpack() {
94        let info = SystemInfo {
95            family: String::from("family"),
96            os: String::from("os"),
97            arch: String::from("arch"),
98            current_dir: PathBuf::from("current-dir"),
99            main_separator: '/',
100            username: String::from("username"),
101            shell: String::from("shell"),
102        };
103
104        // NOTE: We don't actually check the output here because it's an implementation detail
105        // and could change as we change how serialization is done. This is merely to verify
106        // that we can serialize since there are times when serde fails to serialize at
107        // runtime.
108        let _ = rmp_serde::encode::to_vec_named(&info).unwrap();
109    }
110
111    #[test]
112    fn should_be_able_to_deserialize_from_msgpack() {
113        // NOTE: It may seem odd that we are serializing just to deserialize, but this is to
114        // verify that we are not corrupting or causing issues when serializing on a
115        // client/server and then trying to deserialize on the other side. This has happened
116        // enough times with minor changes that we need tests to verify.
117        let buf = rmp_serde::encode::to_vec_named(&SystemInfo {
118            family: String::from("family"),
119            os: String::from("os"),
120            arch: String::from("arch"),
121            current_dir: PathBuf::from("current-dir"),
122            main_separator: '/',
123            username: String::from("username"),
124            shell: String::from("shell"),
125        })
126        .unwrap();
127
128        let info: SystemInfo = rmp_serde::decode::from_slice(&buf).unwrap();
129        assert_eq!(
130            info,
131            SystemInfo {
132                family: String::from("family"),
133                os: String::from("os"),
134                arch: String::from("arch"),
135                current_dir: PathBuf::from("current-dir"),
136                main_separator: '/',
137                username: String::from("username"),
138                shell: String::from("shell"),
139            }
140        );
141    }
142}