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 web UI, in a browser (P3-18): never in a client's `Hello`, only
22    /// on the admin API's sessions the web UI signed in.
23    Web,
24    /// A platform this build does not know, from a newer peer.
25    #[serde(other)]
26    Unknown,
27}
28
29impl Platform {
30    /// The name on the wire and in the server's database.
31    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    /// The platform named `name` ([`Platform::as_str`]); a name this build
44    /// does not know is [`Platform::Unknown`], as on the wire.
45    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    /// What a device or session list calls a device running it.
58    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}