Skip to main content

spoolframe_types/
platform.rs

1//! What kind of client a device runs: said in its `Hello`, kept per
2//! device by the server, and shown in device and session lists ("Linux
3//! app", "Apple TV app") in place of a bare "App".
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub enum Platform {
10    /// The Linux desktop app (`spoolframe-linux`).
11    Linux,
12    /// The Apple TV app.
13    Tvos,
14    /// The iPhone and iPad app.
15    Ios,
16    /// The Mac app.
17    Macos,
18    /// A command-line or scripted client of the SDK (its examples, tools),
19    /// or, for an admin API session, the server's own CLI.
20    Cli,
21    /// The Android app, on phones, tablets and Android TV.
22    Android,
23    /// The Xbox app.
24    Xbox,
25    /// A client in a browser: a web player's `Hello`, or an admin API
26    /// session the web UI signed in.
27    Web,
28    /// A platform this build does not know, from a newer peer.
29    #[serde(other)]
30    Unknown,
31}
32
33impl Platform {
34    /// The name on the wire.
35    pub fn as_str(self) -> &'static str {
36        match self {
37            Platform::Linux => "linux",
38            Platform::Tvos => "tvos",
39            Platform::Ios => "ios",
40            Platform::Macos => "macos",
41            Platform::Cli => "cli",
42            Platform::Android => "android",
43            Platform::Xbox => "xbox",
44            Platform::Web => "web",
45            Platform::Unknown => "unknown",
46        }
47    }
48
49    /// The platform named `name` ([`Platform::as_str`]); a name this build
50    /// does not know is [`Platform::Unknown`], as on the wire.
51    pub fn from_name(name: &str) -> Self {
52        match name {
53            "linux" => Platform::Linux,
54            "tvos" => Platform::Tvos,
55            "ios" => Platform::Ios,
56            "macos" => Platform::Macos,
57            "cli" => Platform::Cli,
58            "android" => Platform::Android,
59            "xbox" => Platform::Xbox,
60            "web" => Platform::Web,
61            _ => Platform::Unknown,
62        }
63    }
64
65    /// What a device or session list calls a device running it.
66    pub fn label(self) -> &'static str {
67        match self {
68            Platform::Linux => "Linux app",
69            Platform::Tvos => "Apple TV app",
70            Platform::Ios => "iPhone or iPad app",
71            Platform::Macos => "Mac app",
72            Platform::Cli => "Command-line client",
73            Platform::Android => "Android app",
74            Platform::Xbox => "Xbox app",
75            Platform::Web => "Web",
76            Platform::Unknown => "App",
77        }
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    const ALL: [Platform; 9] = [
86        Platform::Linux,
87        Platform::Tvos,
88        Platform::Ios,
89        Platform::Macos,
90        Platform::Cli,
91        Platform::Android,
92        Platform::Xbox,
93        Platform::Web,
94        Platform::Unknown,
95    ];
96
97    #[test]
98    fn the_stored_name_is_the_wire_name_and_reads_back() {
99        for platform in ALL {
100            let wire = serde_json::to_value(platform).unwrap();
101            assert_eq!(wire, platform.as_str(), "{platform:?}");
102            assert_eq!(Platform::from_name(platform.as_str()), platform);
103        }
104    }
105
106    #[test]
107    fn a_platform_from_a_newer_peer_is_unknown() {
108        assert_eq!(Platform::from_name("vision_os"), Platform::Unknown);
109        let read: Platform = serde_json::from_str("\"vision_os\"").unwrap();
110        assert_eq!(read, Platform::Unknown);
111    }
112}