use std::{
borrow::Cow,
env::{home_dir, var_os},
path::PathBuf,
};
use crate::tables::table::DEFAULT_PATH_MACOS;
#[must_use]
pub fn default_db_path() -> Option<PathBuf> {
home_dir().map(|home| home.join(DEFAULT_PATH_MACOS))
}
#[must_use]
pub fn default_backup_roots() -> Vec<PathBuf> {
let mut roots = Vec::new();
if cfg!(target_os = "macos")
&& let Some(home) = home_dir()
{
roots.push(
home.join("Library")
.join("Application Support")
.join("MobileSync")
.join("Backup"),
);
}
if cfg!(target_os = "windows") {
if let Some(appdata) = var_os("APPDATA") {
roots.push(
PathBuf::from(appdata)
.join("Apple Computer")
.join("MobileSync")
.join("Backup"),
);
}
if let Some(home) = home_dir() {
roots.push(home.join("Apple").join("MobileSync").join("Backup"));
}
}
roots
}
#[must_use]
pub fn expand_home(path: &str) -> Cow<'_, str> {
match (path.strip_prefix('~'), home_dir()) {
(Some(rest), Some(home)) => Cow::Owned(format!("{}{rest}", home.display())),
_ => Cow::Borrowed(path),
}
}
#[cfg(test)]
mod tests {
use std::{borrow::Cow, env::home_dir};
use crate::{
tables::table::DEFAULT_PATH_MACOS, util::dirs::default_db_path, util::dirs::expand_home,
};
#[test]
fn default_db_path_is_under_home() {
let home = home_dir().unwrap();
assert_eq!(default_db_path().unwrap(), home.join(DEFAULT_PATH_MACOS));
}
#[test]
fn expand_home_replaces_leading_tilde() {
let home = home_dir().unwrap();
assert_eq!(
expand_home("~/Library/Messages"),
format!("{}/Library/Messages", home.display())
);
}
#[test]
fn expand_home_leaves_other_paths_borrowed() {
assert!(matches!(
expand_home("/Library/Messages"),
Cow::Borrowed("/Library/Messages")
));
assert!(matches!(expand_home("a/~/b"), Cow::Borrowed("a/~/b")));
}
}