spoolframe_types/
platform.rs1use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub enum Platform {
10 Linux,
12 Tvos,
14 Ios,
16 Macos,
18 Cli,
21 Web,
24 #[serde(other)]
26 Unknown,
27}
28
29impl Platform {
30 pub fn as_str(self) -> &'static str {
32 match self {
33 Platform::Linux => "linux",
34 Platform::Tvos => "tvos",
35 Platform::Ios => "ios",
36 Platform::Macos => "macos",
37 Platform::Cli => "cli",
38 Platform::Web => "web",
39 Platform::Unknown => "unknown",
40 }
41 }
42
43 pub fn from_name(name: &str) -> Self {
46 match name {
47 "linux" => Platform::Linux,
48 "tvos" => Platform::Tvos,
49 "ios" => Platform::Ios,
50 "macos" => Platform::Macos,
51 "cli" => Platform::Cli,
52 "web" => Platform::Web,
53 _ => Platform::Unknown,
54 }
55 }
56
57 pub fn label(self) -> &'static str {
59 match self {
60 Platform::Linux => "Linux app",
61 Platform::Tvos => "Apple TV app",
62 Platform::Ios => "iPhone or iPad app",
63 Platform::Macos => "Mac app",
64 Platform::Cli => "Command-line client",
65 Platform::Web => "Web",
66 Platform::Unknown => "App",
67 }
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 const ALL: [Platform; 7] = [
76 Platform::Linux,
77 Platform::Tvos,
78 Platform::Ios,
79 Platform::Macos,
80 Platform::Cli,
81 Platform::Web,
82 Platform::Unknown,
83 ];
84
85 #[test]
86 fn the_stored_name_is_the_wire_name_and_reads_back() {
87 for platform in ALL {
88 let wire = serde_json::to_value(platform).unwrap();
89 assert_eq!(wire, platform.as_str(), "{platform:?}");
90 assert_eq!(Platform::from_name(platform.as_str()), platform);
91 }
92 }
93
94 #[test]
95 fn a_platform_from_a_newer_peer_is_unknown() {
96 assert_eq!(Platform::from_name("vision_os"), Platform::Unknown);
97 let read: Platform = serde_json::from_str("\"vision_os\"").unwrap();
98 assert_eq!(read, Platform::Unknown);
99 }
100}