distant_protocol/common/
system.rs1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct SystemInfo {
8 pub family: String,
11
12 pub os: String,
15
16 pub arch: String,
19
20 pub current_dir: PathBuf,
22
23 pub main_separator: char,
26
27 pub username: String,
29
30 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 let _ = rmp_serde::encode::to_vec_named(&info).unwrap();
109 }
110
111 #[test]
112 fn should_be_able_to_deserialize_from_msgpack() {
113 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}