1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::fmt;

#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};

/// Enum describing supported Purposed platforms.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "lowercase"))]
pub enum Platform {
    Windows,
    Darwin,
    Linux,
    Unknown,
}

impl Platform {
    /// Detect the current platform.
    ///
    /// Defaults to `Platform::Unknown` if the platform is unsupported by the Rood library.
    pub fn detect() -> Platform {
        if cfg!(windows) {
            Platform::Windows
        } else if cfg!(unix) {
            Platform::Linux
        } else if cfg!(macos) {
            Platform::Darwin
        } else {
            Platform::Unknown
        }
    }

    /// Returns the platform formatted as string.
    pub fn value(&self) -> String {
        match &self {
            Platform::Windows => String::from("windows"),
            Platform::Darwin => String::from("darwin"),
            Platform::Linux => String::from("linux"),
            Platform::Unknown => String::from("unknown"),
        }
    }
}
impl fmt::Display for Platform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value())
    }
}

impl From<&str> for Platform {
    fn from(v: &str) -> Platform {
        match v {
            "windows" => Platform::Windows,
            "darwin" => Platform::Darwin,
            "linux" => Platform::Linux,
            _ => Platform::Unknown,
        }
    }
}