10 use crate::{dialogs::Confirmation, extension::CompressionFormat, file::File};
13 #[cfg(debug_assertions)]
21 #[cfg(not(debug_assertions))]
24 std::convert::identity($x)
28 pub(crate) fn ensure_exists<'a, P>(path: P) -> crate::Result<()>
32 let exists = path.as_ref().exists();
34 return Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref())));
39 pub(crate) fn check_for_multiple_files(
41 format: &CompressionFormat,
42 ) -> crate::Result<()> {
44 eprintln!("{}: cannot compress multiple files directly to {:#?}.\n Try using an intermediate archival method such as Tar.\n Example: filename.tar{}", "[ERROR]".red(), format, format);
45 return Err(crate::Error::InvalidInput);
51 pub(crate) fn create_path_if_non_existent(path: &Path) -> crate::Result<()> {
54 "{}: attempting to create folder {:?}.",
58 std::fs::create_dir_all(path)?;
60 "{}: directory {:#?} created.",
62 fs::canonicalize(&path)?
68 pub(crate) fn get_destination_path<'a>(dest: &'a Option<File>) -> &'a Path {
70 Some(output_file) => {
71 // Must be None according to the way command-line arg. parsing in Ouch works
72 assert_eq!(output_file.extension, None);
73 Path::new(&output_file.path)
75 None => Path::new("."),
79 pub(crate) fn change_dir_and_return_parent(filename: &Path) -> crate::Result<PathBuf> {
80 let previous_location = env::current_dir()?;
82 let parent = if let Some(parent) = filename.parent() {
85 return Err(crate::Error::CompressingRootFolder);
88 env::set_current_dir(parent)
90 .ok_or(crate::Error::CompressingRootFolder)?;
95 pub fn permission_for_overwriting(
98 confirm: &Confirmation,
99 ) -> crate::Result<bool> {
100 match (flags.is_present("yes"), flags.is_present("false")) {
102 unreachable!("This shoul've been cutted out in the ~/src/cli.rs filter flags function.")
104 (true, _) => return Ok(true),
105 (_, true) => return Ok(false),
109 let file_path_str = to_utf(path);
110 confirm.ask(Some(&file_path_str))
113 pub fn to_utf(os_str: impl AsRef<OsStr>) -> String {
114 let text = format!("{:?}", os_str.as_ref());
115 text.trim_matches('"').to_string()