distant_protocol/common/
version.rs1use serde::{Deserialize, Serialize};
2
3use crate::semver;
4
5#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct Version {
8 pub server_version: semver::Version,
10
11 pub protocol_version: semver::Version,
13
14 #[serde(default, skip_serializing_if = "Vec::is_empty")]
16 pub capabilities: Vec<String>,
17}
18
19impl Version {
20 pub const CAP_EXEC: &'static str = "exec";
22
23 pub const CAP_FS_IO: &'static str = "fs_io";
25
26 pub const CAP_FS_PERM: &'static str = "fs_perm";
28
29 pub const CAP_FS_SEARCH: &'static str = "fs_search";
31
32 pub const CAP_FS_WATCH: &'static str = "fs_watch";
34
35 pub const CAP_SYS_INFO: &'static str = "sys_info";
43
44 pub const fn capabilities() -> &'static [&'static str] {
45 &[
46 Self::CAP_EXEC,
47 Self::CAP_FS_IO,
48 Self::CAP_FS_PERM,
49 Self::CAP_FS_SEARCH,
50 Self::CAP_FS_WATCH,
51 Self::CAP_SYS_INFO,
54 ]
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 use semver::Version as SemVer;
62
63 #[test]
64 fn should_be_able_to_serialize_to_json() {
65 let version = Version {
66 server_version: "123.456.789-rc+build".parse().unwrap(),
67 protocol_version: SemVer::new(1, 2, 3),
68 capabilities: vec![String::from("cap")],
69 };
70
71 let value = serde_json::to_value(version).unwrap();
72 assert_eq!(
73 value,
74 serde_json::json!({
75 "server_version": "123.456.789-rc+build",
76 "protocol_version": "1.2.3",
77 "capabilities": ["cap"]
78 })
79 );
80 }
81
82 #[test]
83 fn should_be_able_to_deserialize_from_json() {
84 let value = serde_json::json!({
85 "server_version": "123.456.789-rc+build",
86 "protocol_version": "1.2.3",
87 "capabilities": ["cap"]
88 });
89
90 let version: Version = serde_json::from_value(value).unwrap();
91 assert_eq!(
92 version,
93 Version {
94 server_version: "123.456.789-rc+build".parse().unwrap(),
95 protocol_version: SemVer::new(1, 2, 3),
96 capabilities: vec![String::from("cap")],
97 }
98 );
99 }
100
101 #[test]
102 fn should_be_able_to_serialize_to_msgpack() {
103 let version = Version {
104 server_version: "123.456.789-rc+build".parse().unwrap(),
105 protocol_version: SemVer::new(1, 2, 3),
106 capabilities: vec![String::from("cap")],
107 };
108
109 let _ = rmp_serde::encode::to_vec_named(&version).unwrap();
114 }
115
116 #[test]
117 fn should_be_able_to_deserialize_from_msgpack() {
118 let buf = rmp_serde::encode::to_vec_named(&Version {
123 server_version: "123.456.789-rc+build".parse().unwrap(),
124 protocol_version: SemVer::new(1, 2, 3),
125 capabilities: vec![String::from("cap")],
126 })
127 .unwrap();
128
129 let version: Version = rmp_serde::decode::from_slice(&buf).unwrap();
130 assert_eq!(
131 version,
132 Version {
133 server_version: "123.456.789-rc+build".parse().unwrap(),
134 protocol_version: SemVer::new(1, 2, 3),
135 capabilities: vec![String::from("cap")],
136 }
137 );
138 }
139}