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::{error::table::TableError, 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) -> Result<Self, TableError> {
23        // If the path ends with the default iOS path,
24        // the user accidentally passed the full iOS path,
25        // not the root of the backup
26        if db_path.ends_with(DEFAULT_PATH_IOS) {
27            return Err(TableError::CannotConnect(
28                "The path provided points to a database inside of an iOS backup, not the root of the backup.".to_string(),
29            ));
30        }
31
32        if db_path.join(DEFAULT_PATH_IOS).exists() {
33            return Ok(Self::iOS);
34        } else if db_path.is_file() {
35            return Ok(Self::macOS);
36        }
37        // If we get here, the database is missing; that error is handled in the connection lifecycle
38        Ok(Self::default())
39    }
40
41    /// Given user's input, return a variant if the input matches one
42    #[must_use]
43    pub fn from_cli(platform: &str) -> Option<Self> {
44        match platform.to_lowercase().as_str() {
45            "macos" => Some(Self::macOS),
46            "ios" => Some(Self::iOS),
47            _ => None,
48        }
49    }
50}
51
52impl Default for Platform {
53    /// The default Platform is [`Platform::macOS`].
54    fn default() -> Self {
55        Self::macOS
56    }
57}
58
59impl Display for Platform {
60    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        match self {
62            Platform::macOS => write!(fmt, "macOS"),
63            Platform::iOS => write!(fmt, "iOS"),
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use crate::{tables::table::DEFAULT_PATH_IOS, util::platform::Platform};
71
72    #[test]
73    fn can_parse_macos_any_case() {
74        assert!(matches!(Platform::from_cli("macos"), Some(Platform::macOS)));
75        assert!(matches!(Platform::from_cli("MACOS"), Some(Platform::macOS)));
76        assert!(matches!(Platform::from_cli("MacOS"), Some(Platform::macOS)));
77    }
78
79    #[test]
80    fn can_parse_ios_any_case() {
81        assert!(matches!(Platform::from_cli("ios"), Some(Platform::iOS)));
82        assert!(matches!(Platform::from_cli("IOS"), Some(Platform::iOS)));
83        assert!(matches!(Platform::from_cli("iOS"), Some(Platform::iOS)));
84    }
85
86    #[test]
87    fn cant_parse_invalid() {
88        assert!(Platform::from_cli("mac").is_none());
89        assert!(Platform::from_cli("iphone").is_none());
90        assert!(Platform::from_cli("").is_none());
91    }
92
93    #[test]
94    fn cant_build_ends_with_ios_backup() {
95        let path = std::path::PathBuf::from(DEFAULT_PATH_IOS);
96        assert!(Platform::determine(&path).is_err());
97    }
98}