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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::fmt::{self, Display};

#[derive(Debug, Deserialize, Clone, Copy, Hash, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Platform {
    #[serde(alias = "osx")]
    MacOs,
    Linux,
    Win,
}

impl Platform {
    pub fn current() -> Self {
        if cfg!(windows) {
            Platform::Win
        } else if cfg!(target_os = "macos") {
            Platform::MacOs
        } else if cfg!(target_os = "linux") {
            Platform::Linux
        } else {
            panic!("uvm doens't compile for this platform")
        }
    }
}

impl Display for Platform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Platform::MacOs => write!(f, "osx"),
            Platform::Linux => write!(f, "linux"),
            Platform::Win => write!(f, "win"),
        }
    }
}

impl Default for Platform {
    fn default() -> Self {
        Self::current()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_creates_sytem_default_platform() {
        let system_platform = if cfg!(windows) {
            Platform::Win
        } else if cfg!(target_os = "macos") {
            Platform::MacOs
        } else if cfg!(target_os = "linux") {
            Platform::Linux
        } else {
            panic!("uvm doens't compile for this platform")
        };

        assert_eq!(system_platform, Platform::default());
    }

    #[test]
    fn can_be_printed_as_string() {
        assert_eq!(&format!("{}",Platform::MacOs), "osx");
        assert_eq!(&format!("{}",Platform::Win), "win");
        assert_eq!(&format!("{}",Platform::Linux), "linux");
    }

    #[derive(Debug, Deserialize)]
    struct TestData {
        field: Platform,
    }

    #[test]
    fn macos_can_be_deserialized() {
        let data = r#"
        {
            "field": "macos"
        }"#;

        let test:TestData = serde_json::from_str(data).unwrap();
        assert_eq!(test.field, Platform::MacOs);
    }

    #[test]
    fn win_can_be_deserialized() {
        let data = r#"
        {
            "field": "win"
        }"#;

        let test:TestData = serde_json::from_str(data).unwrap();
        assert_eq!(test.field, Platform::Win);
    }

    #[test]
    fn linux_can_be_deserialized() {
        let data = r#"
        {
            "field": "linux"
        }"#;

        let test:TestData = serde_json::from_str(data).unwrap();
        assert_eq!(test.field, Platform::Linux);
    }

    #[test]
    fn osx_can_be_deserialized() {
        let data = r#"
        {
            "field": "osx"
        }"#;

        let test:TestData = serde_json::from_str(data).unwrap();
        assert_eq!(test.field, Platform::MacOs);
    }
}