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 and tablets; on a TV, from an app
23    /// before it said [`Platform::AndroidTv`], too.
24    Android,
25    /// The Android app on Android TV and Google TV (wire `android_tv`).
26    /// A peer before it reads it as [`Platform::Unknown`].
27    AndroidTv,
28    /// The Xbox app.
29    Xbox,
30    /// A client in a browser: a web player's `Hello`, or an admin API
31    /// session the web UI signed in.
32    Web,
33    /// A platform this build does not know, from a newer peer.
34    #[serde(other)]
35    Unknown,
36}
37
38impl Platform {
39    /// The name on the wire.
40    pub fn as_str(self) -> &'static str {
41        match self {
42            Platform::Linux => "linux",
43            Platform::Tvos => "tvos",
44            Platform::Ios => "ios",
45            Platform::Macos => "macos",
46            Platform::Cli => "cli",
47            Platform::Android => "android",
48            Platform::AndroidTv => "android_tv",
49            Platform::Xbox => "xbox",
50            Platform::Web => "web",
51            Platform::Unknown => "unknown",
52        }
53    }
54
55    /// The platform named `name` ([`Platform::as_str`]); a name this build
56    /// does not know is [`Platform::Unknown`], as on the wire.
57    pub fn from_name(name: &str) -> Self {
58        match name {
59            "linux" => Platform::Linux,
60            "tvos" => Platform::Tvos,
61            "ios" => Platform::Ios,
62            "macos" => Platform::Macos,
63            "cli" => Platform::Cli,
64            "android" => Platform::Android,
65            "android_tv" => Platform::AndroidTv,
66            "xbox" => Platform::Xbox,
67            "web" => Platform::Web,
68            _ => Platform::Unknown,
69        }
70    }
71
72    /// What a device or session list calls a device running it.
73    pub fn label(self) -> &'static str {
74        match self {
75            Platform::Linux => "Linux app",
76            Platform::Tvos => "Apple TV app",
77            Platform::Ios => "iPhone or iPad app",
78            Platform::Macos => "Mac app",
79            Platform::Cli => "Command-line client",
80            Platform::Android => "Android app",
81            Platform::AndroidTv => "Android TV app",
82            Platform::Xbox => "Xbox app",
83            Platform::Web => "Web",
84            Platform::Unknown => "App",
85        }
86    }
87
88    /// A TV box (Apple TV, Android TV): on the couch, at home, with a
89    /// remote, and so playing Original by default.
90    pub fn is_tv(self) -> bool {
91        matches!(self, Platform::Tvos | Platform::AndroidTv)
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    const ALL: [Platform; 10] = [
100        Platform::Linux,
101        Platform::Tvos,
102        Platform::Ios,
103        Platform::Macos,
104        Platform::Cli,
105        Platform::Android,
106        Platform::AndroidTv,
107        Platform::Xbox,
108        Platform::Web,
109        Platform::Unknown,
110    ];
111
112    #[test]
113    fn android_tv_reads_as_android_tv_and_is_a_tv() {
114        let wire = serde_json::to_value(Platform::AndroidTv).unwrap();
115        assert_eq!(wire, "android_tv");
116        assert_eq!(Platform::AndroidTv.label(), "Android TV app");
117        assert!(Platform::AndroidTv.is_tv());
118        assert!(Platform::Tvos.is_tv());
119        assert!(!Platform::Android.is_tv());
120    }
121
122    #[test]
123    fn the_stored_name_is_the_wire_name_and_reads_back() {
124        for platform in ALL {
125            let wire = serde_json::to_value(platform).unwrap();
126            assert_eq!(wire, platform.as_str(), "{platform:?}");
127            assert_eq!(Platform::from_name(platform.as_str()), platform);
128        }
129    }
130
131    #[test]
132    fn a_platform_from_a_newer_peer_is_unknown() {
133        assert_eq!(Platform::from_name("vision_os"), Platform::Unknown);
134        let read: Platform = serde_json::from_str("\"vision_os\"").unwrap();
135        assert_eq!(read, Platform::Unknown);
136    }
137}