From 21f93c84b1d65ae8dc5ea6475333c42d01ce0a99 Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta Date: Sat, 22 Apr 2023 17:21:14 +0530 Subject: [PATCH] Use XDG conventions on macOS too --- Cargo.lock | 21 +++++++++++++++++++- Cargo.toml | 2 +- src/filesystem.rs | 49 ++++++++++++++++++++++++++++++++--------------- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b5e698..bbdffee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -281,6 +281,16 @@ dependencies = [ "libc", ] +[[package]] +name = "etcetera" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51822eedc6129d8c4d96cec86d56b785e983f943c9ce9fb892e0c2a99a7f47a0" +dependencies = [ + "cfg-if", + "home", +] + [[package]] name = "fs_at" version = "0.1.4" @@ -325,6 +335,15 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" +[[package]] +name = "home" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "747309b4b440c06d57b0b25f2aee03ee9b5e5397d288c60e21fc709bb98a7408" +dependencies = [ + "winapi", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -434,10 +453,10 @@ dependencies = [ "anyhow", "clap", "crossterm", - "directories-next", "dns_common", "dns_common_derive", "edit", + "etcetera", "lazy_static", "regex", "remove_dir_all 0.8.2", diff --git a/Cargo.toml b/Cargo.toml index 8f5d321..a9dc3d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ regex = { version = "1.7.3", default-features = false, features = ["std", "unico clap = { version = "4.2.1", features = ["derive", "cargo"] } crossterm = "0.26.1" lazy_static = "1.4.0" -directories-next = "2.0.0" +etcetera = "0.7.1" walkdir = "2.3.3" shellwords = "1.1.0" anyhow = "1.0.70" diff --git a/src/filesystem.rs b/src/filesystem.rs index d5f2962..6340f82 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -4,7 +4,7 @@ use crate::parser::Parser; use crate::prelude::*; use crate::structures::fetcher; -use directories_next::BaseDirs; +use etcetera::BaseStrategy; use regex::Regex; use std::cell::RefCell; @@ -46,8 +46,20 @@ fn compiled_default_path(path: Option<&str>) -> Option { } pub fn default_cheat_pathbuf() -> Result { - let base_dirs = BaseDirs::new().ok_or_else(|| anyhow!("Unable to get base dirs"))?; - let mut pathbuf = PathBuf::from(base_dirs.data_dir()); + if cfg!(target_os = "macos") { + let base_dirs = etcetera::base_strategy::Apple::new()?; + + let mut pathbuf = base_dirs.data_dir(); + pathbuf.push("navi"); + pathbuf.push("cheats"); + if pathbuf.exists() { + return Ok(pathbuf); + } + } + + let base_dirs = etcetera::choose_base_strategy()?; + + let mut pathbuf = base_dirs.data_dir(); pathbuf.push("navi"); pathbuf.push("cheats"); if !pathbuf.exists() { @@ -59,9 +71,20 @@ pub fn default_cheat_pathbuf() -> Result { } pub fn default_config_pathbuf() -> Result { - let base_dirs = BaseDirs::new().ok_or_else(|| anyhow!("Unable to get base dirs"))?; + if cfg!(target_os = "macos") { + let base_dirs = etcetera::base_strategy::Apple::new()?; - let mut pathbuf = PathBuf::from(base_dirs.config_dir()); + let mut pathbuf = base_dirs.config_dir(); + pathbuf.push("navi"); + pathbuf.push("config.yaml"); + if pathbuf.exists() { + return Ok(pathbuf); + } + } + + let base_dirs = etcetera::choose_base_strategy()?; + + let mut pathbuf = base_dirs.config_dir(); pathbuf.push("navi"); pathbuf.push("config.yaml"); if !pathbuf.exists() { @@ -132,13 +155,13 @@ impl fetcher::Fetcher for Fetcher { let folders = paths_from_path_param(&interpolated_paths); let home_regex = Regex::new(r"^~").unwrap(); - let home = BaseDirs::new().map(|b| b.home_dir().to_string()); + let home = etcetera::home_dir().ok(); // parser.filter = self.tag_rules.as_ref().map(|r| gen_lists(r.as_str())); for folder in folders { let interpolated_folder = match &home { - Some(h) => home_regex.replace(folder, h).to_string(), + Some(h) => home_regex.replace(folder, h.to_string_lossy()).to_string(), None => folder.to_string(), }; let folder_pathbuf = PathBuf::from(interpolated_folder); @@ -228,12 +251,10 @@ mod tests { #[test] fn test_default_config_pathbuf() { - let base_dirs = BaseDirs::new() - .ok_or_else(|| anyhow!("bad")) - .expect("could not determine base directories"); + let base_dirs = etcetera::choose_base_strategy().expect("could not determine base directories"); let expected = { - let mut e = base_dirs.config_dir().to_path_buf(); + let mut e = base_dirs.config_dir(); e.push("navi"); e.push("config.yaml"); e.to_string_lossy().to_string() @@ -246,12 +267,10 @@ mod tests { #[test] fn test_default_cheat_pathbuf() { - let base_dirs = BaseDirs::new() - .ok_or_else(|| anyhow!("bad")) - .expect("could not determine base directories"); + let base_dirs = etcetera::choose_base_strategy().expect("could not determine base directories"); let expected = { - let mut e = base_dirs.data_dir().to_path_buf(); + let mut e = base_dirs.data_dir(); e.push("navi"); e.push("cheats"); e.to_string_lossy().to_string()