use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[serde(rename_all = "snake_case")]
pub enum Platform {
Linux,
Tvos,
Ios,
Macos,
Cli,
Android,
Xbox,
Web,
#[serde(other)]
Unknown,
}
impl Platform {
pub fn as_str(self) -> &'static str {
match self {
Platform::Linux => "linux",
Platform::Tvos => "tvos",
Platform::Ios => "ios",
Platform::Macos => "macos",
Platform::Cli => "cli",
Platform::Android => "android",
Platform::Xbox => "xbox",
Platform::Web => "web",
Platform::Unknown => "unknown",
}
}
pub fn from_name(name: &str) -> Self {
match name {
"linux" => Platform::Linux,
"tvos" => Platform::Tvos,
"ios" => Platform::Ios,
"macos" => Platform::Macos,
"cli" => Platform::Cli,
"android" => Platform::Android,
"xbox" => Platform::Xbox,
"web" => Platform::Web,
_ => Platform::Unknown,
}
}
pub fn label(self) -> &'static str {
match self {
Platform::Linux => "Linux app",
Platform::Tvos => "Apple TV app",
Platform::Ios => "iPhone or iPad app",
Platform::Macos => "Mac app",
Platform::Cli => "Command-line client",
Platform::Android => "Android app",
Platform::Xbox => "Xbox app",
Platform::Web => "Web",
Platform::Unknown => "App",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const ALL: [Platform; 9] = [
Platform::Linux,
Platform::Tvos,
Platform::Ios,
Platform::Macos,
Platform::Cli,
Platform::Android,
Platform::Xbox,
Platform::Web,
Platform::Unknown,
];
#[test]
fn the_stored_name_is_the_wire_name_and_reads_back() {
for platform in ALL {
let wire = serde_json::to_value(platform).unwrap();
assert_eq!(wire, platform.as_str(), "{platform:?}");
assert_eq!(Platform::from_name(platform.as_str()), platform);
}
}
#[test]
fn a_platform_from_a_newer_peer_is_unknown() {
assert_eq!(Platform::from_name("vision_os"), Platform::Unknown);
let read: Platform = serde_json::from_str("\"vision_os\"").unwrap();
assert_eq!(read, Platform::Unknown);
}
}