Skip to content

Commit

Permalink
improves error handling and display
Browse files Browse the repository at this point in the history
- don't assume report file exists (unwrap -> ?)
- context with report file opening
- display causes when skipping
  • Loading branch information
wookietreiber committed Nov 16, 2023
1 parent 2289c11 commit b311ce8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ fn run(dir: &Path, config: &Config) {
log::debug(format!("running {} ...", dir.display()), config);
if let Err(error) = usage::run(dir, config) {
let dir = dir.display();
log::error(format!("skipping directory {dir}: {error}"));
log::error(format!("skipping directory {dir}: {error:#}"));
}
}
28 changes: 21 additions & 7 deletions src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, BufReader};
use std::io::BufReader;
use std::ops::AddAssign;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn run(dir: &Path, config: &Config) -> Result<()> {
}
}

fn sum(dir: &Path, report: &Path, config: &Config) -> io::Result<()> {
fn sum(dir: &Path, report: &Path, config: &Config) -> Result<()> {
if let Some(depth) = config.max_depth {
let sizes = sum_depth(dir, depth, report, config)?;

Expand All @@ -118,7 +118,7 @@ fn sum(dir: &Path, report: &Path, config: &Config) -> io::Result<()> {
}
}
} else {
let Acc { inodes, bytes } = sum_total(report).unwrap();
let Acc { inodes, bytes } = sum_total(report)?;
output(dir, inodes, bytes, config);
};

Expand All @@ -130,8 +130,15 @@ fn sum_depth(
depth: usize,
report: &Path,
config: &Config,
) -> io::Result<BTreeMap<PathBuf, Acc>> {
let report = File::open(report)?;
) -> Result<BTreeMap<PathBuf, Acc>> {
let report = File::open(report).with_context(|| {
format!(
"opening report {} (this is likely because applying a \
filter didn't return any results)",
report.display()
)
})?;

let report = BufReader::new(report);

let mut dir_sums = BTreeMap::new();
Expand Down Expand Up @@ -170,10 +177,17 @@ fn sum_depth(
Ok(dir_sums)
}

fn sum_total(report: &Path) -> io::Result<Acc> {
fn sum_total(report: &Path) -> Result<Acc> {
let mut sum = Acc::default();

let report = File::open(report)?;
let report = File::open(report).with_context(|| {
format!(
"opening report {} (this is likely because applying a \
filter didn't return any results)",
report.display()
)
})?;

let report = BufReader::new(report);

for line in report.byte_lines() {
Expand Down

0 comments on commit b311ce8

Please sign in to comment.