diff --git a/src/main.rs b/src/main.rs index c098100..d9a5d1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,7 +44,7 @@ mod oxrl; mod validation; use std::{ - env, + env::{self, current_dir}, error::Error, ffi::OsStr, fs, @@ -61,6 +61,26 @@ macro_rules! main_err { }; } +/** + * Returns the path to the parent dir of the argument, + * if it has one. + * We use this rather complex way of doing it, + * because with a simple `file_path.parent()?.exists()`, + * we would get `false` in case of `"bla.txt"`, + * even if CWD is an existing directory. + */ +fn get_parent

(file_path: P) -> Option +where + P: AsRef, +{ + let file_path_val = file_path.as_ref(); + let parent = file_path_val.parent()?; + if parent.components().next().is_none() { + return current_dir().ok(); + } + Some(parent.to_owned()) +} + fn convert( input_path: IP, output_path: Option, @@ -82,7 +102,7 @@ where main_err!("input is a file, so output would have to be too, but is not"); } } else { - let out_parent = output_path_val.as_ref().parent(); + let out_parent = get_parent(output_path_val.as_ref()); if let Some(out_parent_val) = out_parent { if !out_parent_val.exists() { main_err!(format!(