1use anyhow::{Result, bail};
2use std::fs;
3use std::path::{Path, PathBuf};
4
5pub fn normalize_existing_file(path: &Path) -> Result<PathBuf> {
6 let absolute = if path.is_absolute() {
7 path.to_path_buf()
8 } else {
9 std::env::current_dir()?.join(path)
10 };
11 if !absolute.exists() {
12 bail!("file '{}' does not exist", absolute.display());
13 }
14 if !absolute.is_file() {
15 bail!("path '{}' is not a file", absolute.display());
16 }
17 Ok(fs::canonicalize(&absolute).unwrap_or(absolute))
18}
19
20pub fn normalize_destination_path(path: &Path) -> Result<PathBuf> {
21 let absolute = if path.is_absolute() {
22 path.to_path_buf()
23 } else {
24 std::env::current_dir()?.join(path)
25 };
26 if let Some(parent) = absolute.parent()
27 && !parent.exists()
28 {
29 bail!(
30 "destination directory '{}' does not exist",
31 parent.display()
32 );
33 }
34 Ok(absolute)
35}