Skip to content

Commit

Permalink
Allow the output file to refer to a file in the CWD [fix]
Browse files Browse the repository at this point in the history
  • Loading branch information
hoijui committed Aug 13, 2023
1 parent c7e3aa4 commit 249fad1
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod oxrl;
mod validation;

use std::{
env,
env::{self, current_dir},
error::Error,
ffi::OsStr,
fs,
Expand All @@ -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<P>(file_path: P) -> Option<PathBuf>
where
P: AsRef<Path>,
{
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<IP, OP>(
input_path: IP,
output_path: Option<OP>,
Expand All @@ -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!(
Expand Down

0 comments on commit 249fad1

Please sign in to comment.