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