From 553bbac36bdc483135a7053ca64507e01397e5e1 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 9 Sep 2024 23:03:49 +0200 Subject: add package manager recognition --- src/pathinfo.rs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'src/pathinfo.rs') diff --git a/src/pathinfo.rs b/src/pathinfo.rs index 8b1ca2f..03b8a6b 100644 --- a/src/pathinfo.rs +++ b/src/pathinfo.rs @@ -120,7 +120,8 @@ impl PathInfo { let old_path = modified_backup.get_absolute_file_location(config, &last_file.rel_location); let new_path = format!("{location_root}/{rel_location}"); - let mut old = File::open(old_path)?; let mut new = File::open(new_path)?; + let mut old = File::open(old_path)?; + let mut new = File::open(new_path)?; let old_len = old.metadata()?.len(); let new_len = new.metadata()?.len(); @@ -195,12 +196,9 @@ impl PathInfo { return Err(Error::InvalidDirectory(value.to_string())); }; if split.0.starts_with('~') { - if config.user.len() != 1 { - return Err(Error::MultiUser); - } return Ok(( split.1.to_string(), - LocationRoot::User(config.user[0].clone()), + LocationRoot::User, )); }; Ok(( @@ -212,18 +210,22 @@ impl PathInfo { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub enum LocationRoot { - User(String), + User, Custom(String), - SystemSettings, + SystemConfig, + UserConfig, Root, } impl Display for LocationRoot { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - LocationRoot::User(user) => write!(f, "/home/{user}"), + LocationRoot::User => write!(f, "{}", dirs::home_dir().unwrap().to_string_lossy()), LocationRoot::Custom(loc) => write!(f, "{loc}"), - LocationRoot::SystemSettings => write!(f, "/etc"), + LocationRoot::SystemConfig => write!(f, "/etc"), + LocationRoot::UserConfig => { + write!(f, "{}", dirs::config_local_dir().unwrap().to_string_lossy()) + } LocationRoot::Root => write!(f, "/"), } } @@ -236,8 +238,9 @@ impl LocationRoot { return Err(Error::NoIndex); }; match split_op.0 { - "u" => Ok(Self::User(split_op.1.to_string())), - "s" => Ok(Self::SystemSettings), + "u" => Ok(Self::User), + "s" => Ok(Self::SystemConfig), + "d" => Ok(Self::UserConfig), "r" => Ok(Self::Root), "c" => Ok(Self::Custom( config @@ -262,7 +265,6 @@ mod tests { backup::Backup, config::Config, error::{Error, Result}, - packages::{pacman::Pacman, PackageManager}, }; use super::LocationRoot; @@ -276,8 +278,8 @@ mod tests { .insert("test".to_string(), "/usr/local/test".to_string()); let mut values_ok: Vec<(&str, LocationRoot)> = Vec::new(); - values_ok.push(("u:test", LocationRoot::User("test".to_string()))); - values_ok.push(("s:", LocationRoot::SystemSettings)); + values_ok.push(("u:test", LocationRoot::User)); + values_ok.push(("s:", LocationRoot::SystemConfig)); values_ok.push(("r:", LocationRoot::Root)); values_ok.push(( "c:test", @@ -321,7 +323,6 @@ mod tests { #[test] fn parse_location() -> Result<()> { let mut config = Config::default(); - config.user.push("test".to_string()); config .custom_directories .insert("test".to_string(), "/usr/local/test".to_string()); @@ -331,14 +332,14 @@ mod tests { "~/.config/nvim", ( ".config/nvim".to_string(), - LocationRoot::User("test".to_string()), + LocationRoot::User, ), )); values_ok.push(( "u:test/.config/nvim", ( ".config/nvim".to_string(), - LocationRoot::User("test".to_string()), + LocationRoot::User, ), )); values_ok.push(( @@ -351,7 +352,7 @@ mod tests { )); values_ok.push(( "s:/.config/nvim", - (".config/nvim".to_string(), LocationRoot::SystemSettings), + (".config/nvim".to_string(), LocationRoot::SystemConfig), )); values_ok.push(( "c:test/.config/nvim", @@ -385,9 +386,7 @@ mod tests { let mut f = File::create("./backup-test-dir/nothing.txt")?; f.write_all("unmodified".as_bytes())?; - let pacman = Pacman; - let pkgs = pacman.get_installed()?; - let backup = Backup::create(&config, pkgs)?; + let backup = Backup::create(&config, None)?; backup.save(&config)?; let mut f = File::create("./backup-test-dir/size.txt")?; -- cgit v1.2.3