From 0610cd073119fb19f826315e0b5e79d7dff59778 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Tue, 6 Jun 2023 14:02:07 +0200 Subject: [PATCH] feat(export): second move method --- crates/pacdef_core/src/core.rs | 88 +++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 29 deletions(-) diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index 042808d..633b5c0 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -1,6 +1,6 @@ use std::collections::{HashMap, HashSet}; use std::env::current_dir; -use std::fs::{remove_file, rename, File}; +use std::fs::{copy, remove_file, rename, File}; use std::os::unix::fs::symlink; use std::path::{Path, PathBuf}; @@ -12,11 +12,11 @@ use crate::backend::{Backend, Backends, ToDoPerBackend}; use crate::cmd::run_edit_command; use crate::env::get_single_var; use crate::path::{binary_in_path, get_absolutized_file_paths, get_group_dir}; -use crate::review; use crate::search; use crate::ui::get_user_confirmation; use crate::Config; use crate::Group; +use crate::{review, Error}; /// Most data that is required during runtime of the program. /// `args` is an `Option` so that we can take ownership later without cloning. @@ -399,25 +399,17 @@ impl Pacdef { Ok(()) } - fn export_groups(&self, groups: &[String]) -> Result<()> { - let groups: Vec<_> = groups - .iter() - .map(|group_name| { - self.groups - .iter() - .find(|group| group.name == *group_name) - .unwrap() - }) - .collect(); - + fn export_groups(&self, names: &[String]) -> Result<()> { + let groups = find_groups_by_name(names, &self.groups)?; let output_dir = current_dir()?; for group in &groups { let mut exported_path = output_dir.clone(); exported_path.push(PathBuf::from(&group.name)); - // TODO: check if output exists - // TODO: check if this was successful, resort to the other method otherwise - rename(&group.path, &exported_path)?; + + ensure!(!exported_path.exists(), "{exported_path:?} already exists"); + + move_file(&group.path, &exported_path)?; symlink(&exported_path, &group.path)?; } @@ -425,6 +417,53 @@ impl Pacdef { } } +fn move_file
(from: P, to: Q) -> Result<()>
+where
+ P: AsRef