imessage_database/util/
platform.rs

1/*!
2 Contains data structures used to describe database platforms.
3*/
4
5use std::{fmt::Display, path::Path};
6
7use crate::tables::table::DEFAULT_PATH_IOS;
8
9/// Represents the platform that created the database this library connects to
10#[derive(PartialEq, Eq, Debug)]
11pub enum Platform {
12    /// macOS-sourced data
13    #[allow(non_camel_case_types)]
14    macOS,
15    /// iOS-sourced data
16    #[allow(non_camel_case_types)]
17    iOS,
18}
19
20impl Platform {
21    /// Try to determine the current platform, defaulting to macOS.
22    pub fn determine(db_path: &Path) -> Self {
23        if db_path.join(DEFAULT_PATH_IOS).exists() {
24            return Self::iOS;
25        } else if db_path.is_file() {
26            return Self::macOS;
27        }
28        // If we get here, the database is missing; that error is handled in the connection lifecycle
29        Self::default()
30    }
31
32    /// Given user's input, return a variant if the input matches one
33    pub fn from_cli(platform: &str) -> Option<Self> {
34        match platform.to_lowercase().as_str() {
35            "macos" => Some(Self::macOS),
36            "ios" => Some(Self::iOS),
37            _ => None,
38        }
39    }
40}
41
42impl Default for Platform {
43    /// The default Platform is [`Platform::macOS`].
44    fn default() -> Self {
45        Self::macOS
46    }
47}
48
49impl Display for Platform {
50    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        match self {
52            Platform::macOS => write!(fmt, "macOS"),
53            Platform::iOS => write!(fmt, "iOS"),
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use crate::util::platform::Platform;
61
62    #[test]
63    fn can_parse_macos_any_case() {
64        assert!(matches!(Platform::from_cli("macos"), Some(Platform::macOS)));
65        assert!(matches!(Platform::from_cli("MACOS"), Some(Platform::macOS)));
66        assert!(matches!(Platform::from_cli("MacOS"), Some(Platform::macOS)));
67    }
68
69    #[test]
70    fn can_parse_ios_any_case() {
71        assert!(matches!(Platform::from_cli("ios"), Some(Platform::iOS)));
72        assert!(matches!(Platform::from_cli("IOS"), Some(Platform::iOS)));
73        assert!(matches!(Platform::from_cli("iOS"), Some(Platform::iOS)));
74    }
75
76    #[test]
77    fn cant_parse_invalid() {
78        assert!(Platform::from_cli("mac").is_none());
79        assert!(Platform::from_cli("iphone").is_none());
80        assert!(Platform::from_cli("").is_none());
81    }
82}