rizlib/
platform.rs

1//! A module containing an enum representing a platform that Rizline can run on.
2
3use serde::{Deserialize, Serialize};
4use std::fmt::{self, Display};
5
6/// An enum representing a platform that Rizline can run on.
7///
8/// This enum can be used for downloading assets, as the URL that the assets are downloaded from includes the name of the platform that the game runs on.
9/// Currently, only two valid platforms are known: [`Android`][Platform::Android] and [`iOS`][Platform::IOS].
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
11pub enum Platform {
12    #[serde(rename = "Android")]
13    Android,
14    #[serde(rename = "iOS")]
15    IOS,
16}
17
18impl Platform {
19    /// All valid platforms.
20    pub const ALL_PLATFORMS: [Platform; 2] = [Platform::Android, Platform::IOS];
21
22    /// All valid platform names, as strings.
23    pub const ALL_PLATFORM_NAMES: [&str; 2] = ["Android", "iOS"];
24
25    /// Name of the platform in a URL.
26    ///
27    /// This function returns the name of the platform as it is used in the URL that the assets can be downloaded from.
28    /// For [`Platform::Android`] this is "Android", and for [`Platform::IOS`] this is "iOS".
29    ///
30    /// **Note:** [`Self::to_string`][ToString::to_string], [`Self::platform_name_in_url`] and [`Self::platform_name_in_directory`] currently return the same value, however this may change in the future.
31    pub fn platform_name_in_url(&self) -> String {
32        match self {
33            Platform::Android => "Android",
34            Platform::IOS => "iOS",
35        }
36        .to_string()
37    }
38
39    /// Name of the platform as a directory name.
40    ///
41    /// This function returns the name of the platform as a valid directory name, with no special symbols.
42    /// For [`Platform::Android`] this is "Android", and for [`Platform::IOS`] this is "iOS".
43    ///
44    /// **Note:** [`Self::to_string`][ToString::to_string], [`Self::platform_name_in_url`] and [`Self::platform_name_in_directory`] currently return the same value, however this may change in the future.
45    pub fn platform_name_in_directory(&self) -> String {
46        match self {
47            Platform::Android => "Android",
48            Platform::IOS => "iOS",
49        }
50        .to_string()
51    }
52}
53
54impl Display for Platform {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        f.write_str(match self {
57            Platform::Android => "Android",
58            Platform::IOS => "iOS",
59        })
60    }
61}
62
63impl TryFrom<&str> for Platform {
64    type Error = ();
65    /// Try to convert a platform name to a [`Platform`].
66    fn try_from(value: &str) -> Result<Self, Self::Error> {
67        match value {
68            "Android" => Ok(Self::Android),
69            "iOS" => Ok(Self::IOS),
70            _ => Err(()),
71        }
72    }
73}