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
/*!
 Contains data structures used to describe database platforms
*/

use std::fmt::Display;

/// Represents the platform that created the database this library connects to
pub enum Platform {
    /// MacOS-sourced data
    MacOS,
    /// iOS-sourced data
    #[allow(non_camel_case_types)]
    iOS,
}

impl Platform {
    /// Given user's input, return a variant if the input matches one
    pub fn from_cli(platform: &str) -> Option<Self> {
        match platform.to_lowercase().as_str() {
            "macos" => Some(Self::MacOS),
            "ios" => Some(Self::iOS),
            _ => None,
        }
    }
}

impl Default for Platform {
    /// The default Platform is [Platform::MacOS].
    fn default() -> Self {
        Self::MacOS
    }
}

impl Display for Platform {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Platform::MacOS => write!(fmt, "MacOS"),
            Platform::iOS => write!(fmt, "iOS"),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::util::platform::Platform;

    #[test]
    fn can_parse_macos_any_case() {
        assert!(matches!(Platform::from_cli("macos"), Some(Platform::MacOS)));
        assert!(matches!(Platform::from_cli("MACOS"), Some(Platform::MacOS)));
        assert!(matches!(Platform::from_cli("MacOS"), Some(Platform::MacOS)));
    }

    #[test]
    fn can_parse_ios_any_case() {
        assert!(matches!(Platform::from_cli("ios"), Some(Platform::iOS)));
        assert!(matches!(Platform::from_cli("IOS"), Some(Platform::iOS)));
        assert!(matches!(Platform::from_cli("iOS"), Some(Platform::iOS)));
    }

    #[test]
    fn cant_parse_invalid() {
        assert!(matches!(Platform::from_cli("mac"), None));
        assert!(matches!(Platform::from_cli("iphone"), None));
        assert!(matches!(Platform::from_cli(""), None));
    }
}