Skip to content

Commit

Permalink
feat: improve errors for removal command
Browse files Browse the repository at this point in the history
  • Loading branch information
domenicocinque committed Jan 4, 2025
1 parent 0fe351d commit 6acdf5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum AppError {
SerdeError(serde_json::Error),
InvalidDirectory(PathBuf),
FileNotFound(String),
InvalidDeduplicationReport(String),
}

impl From<io::Error> for AppError {
Expand Down Expand Up @@ -38,6 +39,9 @@ impl fmt::Display for AppError {
AppError::FileNotFound(dir) => {
write!(f, "{} File `{}` not found", error_prefix, dir)
}
AppError::InvalidDeduplicationReport(msg) => {
write!(f, "{} Invalid deduplication report: {}", error_prefix, msg)
}
}
}
}
8 changes: 6 additions & 2 deletions src/removal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ use serde_json;
/// Run the removal process for the given results file and creates the
/// deduplicated output in the given output directory.
pub fn run(results_file: &str, output_dir: &str) -> Result<(), AppError> {
let results_file = std::fs::read_to_string(results_file)?;
let results: DeduplicationReport = serde_json::from_str(&results_file)?;
let results_file = std::fs::read_to_string(results_file).map_err(
|_| AppError::FileNotFound(results_file.to_string()),
)?;
let results: DeduplicationReport = serde_json::from_str(&results_file).map_err(
|err| AppError::InvalidDeduplicationReport(err.to_string()),
)?;

let output_dir = std::path::Path::new(output_dir);
if !output_dir.exists() {
Expand Down

0 comments on commit 6acdf5b

Please sign in to comment.