Skip to main content

imessage_database/util/
platform.rs

1/*!
2 Platform detection for Messages database paths.
3*/
4
5use std::{fmt::Display, path::Path};
6
7use crate::{
8    error::table::{TableConnectError, TableError},
9    tables::table::DEFAULT_PATH_IOS,
10};
11
12/// Platform that produced the Messages database.
13#[derive(PartialEq, Eq, Debug)]
14pub enum Platform {
15    /// macOS Messages database.
16    #[allow(non_camel_case_types)]
17    macOS,
18    /// iOS backup containing a Messages database.
19    #[allow(non_camel_case_types)]
20    iOS,
21}
22
23impl Platform {
24    /// Detect whether a path points to an iOS backup root or a macOS database.
25    pub fn determine(db_path: &Path) -> Result<Self, TableError> {
26        // iOS inputs must be backup roots, not the nested database path.
27        if db_path.ends_with(DEFAULT_PATH_IOS) {
28            return Err(TableError::CannotConnect(TableConnectError::NotBackupRoot));
29        }
30
31        if db_path.join(DEFAULT_PATH_IOS).exists() {
32            return Ok(Self::iOS);
33        } else if db_path.is_file() {
34            return Ok(Self::macOS);
35        }
36        // Connection setup reports the missing path later.
37        Ok(Self::default())
38    }
39
40    /// Parse a platform name from CLI input.
41    #[must_use]
42    pub fn from_cli(platform: &str) -> Option<Self> {
43        match platform.to_lowercase().as_str() {
44            "macos" => Some(Self::macOS),
45            "ios" => Some(Self::iOS),
46            _ => None,
47        }
48    }
49}
50
51impl Default for Platform {
52    /// The default Platform is [`Platform::macOS`].
53    fn default() -> Self {
54        Self::macOS
55    }
56}
57
58impl Display for Platform {
59    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            Platform::macOS => write!(fmt, "macOS"),
62            Platform::iOS => write!(fmt, "iOS"),
63        }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use crate::{tables::table::DEFAULT_PATH_IOS, util::platform::Platform};
70
71    #[test]
72    fn can_parse_macos_any_case() {
73        assert!(matches!(Platform::from_cli("macos"), Some(Platform::macOS)));
74        assert!(matches!(Platform::from_cli("MACOS"), Some(Platform::macOS)));
75        assert!(matches!(Platform::from_cli("MacOS"), Some(Platform::macOS)));
76    }
77
78    #[test]
79    fn can_parse_ios_any_case() {
80        assert!(matches!(Platform::from_cli("ios"), Some(Platform::iOS)));
81        assert!(matches!(Platform::from_cli("IOS"), Some(Platform::iOS)));
82        assert!(matches!(Platform::from_cli("iOS"), Some(Platform::iOS)));
83    }
84
85    #[test]
86    fn cant_parse_invalid() {
87        assert!(Platform::from_cli("mac").is_none());
88        assert!(Platform::from_cli("iphone").is_none());
89        assert!(Platform::from_cli("").is_none());
90    }
91
92    #[test]
93    fn cant_build_ends_with_ios_backup() {
94        let path = std::path::PathBuf::from(DEFAULT_PATH_IOS);
95        assert!(Platform::determine(&path).is_err());
96    }
97}