Skip to content

Commit

Permalink
fix(cli): Report paths with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 18, 2023
1 parent e003ac8 commit 104ebdc
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions crates/typos-cli/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,14 @@ fn read_file(
) -> Result<(Vec<u8>, content_inspector::ContentType), std::io::Error> {
let buffer = if path == std::path::Path::new("-") {
let mut buffer = Vec::new();
report_result(std::io::stdin().read_to_end(&mut buffer), reporter)?;
report_result(
std::io::stdin().read_to_end(&mut buffer),
Some(path),
reporter,
)?;
buffer
} else {
report_result(std::fs::read(path), reporter)?
report_result(std::fs::read(path), Some(path), reporter)?
};

let content_type = content_inspector::inspect(&buffer);
Expand All @@ -460,7 +464,7 @@ fn read_file(
encoding_rs::DecoderResult::InputEmpty => Ok(decoded),
_ => Err(format!("invalid UTF-16LE encoding at byte {} in {}", written, path.display())),
};
let buffer = report_result(decoded, reporter)?;
let buffer = report_result(decoded, Some(path), reporter)?;
(buffer.into_bytes(), content_type)
}
content_inspector::ContentType::UTF_16BE => {
Expand All @@ -473,7 +477,7 @@ fn read_file(
encoding_rs::DecoderResult::InputEmpty => Ok(decoded),
_ => Err(format!("invalid UTF-16BE encoding at byte {} in {}", written, path.display())),
};
let buffer = report_result(decoded, reporter)?;
let buffer = report_result(decoded, Some(path), reporter)?;
(buffer.into_bytes(), content_type)
},
};
Expand All @@ -496,7 +500,7 @@ fn write_file(
| content_inspector::ContentType::UTF_8
| content_inspector::ContentType::UTF_8_BOM => buffer,
content_inspector::ContentType::UTF_16LE => {
let buffer = report_result(String::from_utf8(buffer), reporter)?;
let buffer = report_result(String::from_utf8(buffer), Some(path), reporter)?;
if buffer.is_empty() {
// Error occurred, don't clear out the file
return Ok(());
Expand All @@ -509,7 +513,7 @@ fn write_file(
encoded.into_owned()
}
content_inspector::ContentType::UTF_16BE => {
let buffer = report_result(String::from_utf8(buffer), reporter)?;
let buffer = report_result(String::from_utf8(buffer), Some(path), reporter)?;
if buffer.is_empty() {
// Error occurred, don't clear out the file
return Ok(());
Expand All @@ -524,9 +528,9 @@ fn write_file(
};

if path == std::path::Path::new("-") {
report_result(std::io::stdout().write_all(&buffer), reporter)?;
report_result(std::io::stdout().write_all(&buffer), Some(path), reporter)?;
} else {
report_result(std::fs::write(path, buffer), reporter)?;
report_result(std::fs::write(path, buffer), Some(path), reporter)?;
}

Ok(())
Expand All @@ -547,20 +551,26 @@ fn check_bytes<'a>(

fn report_result<T: Default, E: ToString>(
value: Result<T, E>,
path: Option<&std::path::Path>,
reporter: &dyn report::Report,
) -> Result<T, std::io::Error> {
let buffer = match value {
Ok(value) => value,
Err(err) => {
report_error(err, reporter)?;
report_error(err, path, reporter)?;
Default::default()
}
};
Ok(buffer)
}

fn report_error<E: ToString>(err: E, reporter: &dyn report::Report) -> Result<(), std::io::Error> {
let msg = report::Error::new(err.to_string());
fn report_error<E: ToString>(
err: E,
path: Option<&std::path::Path>,
reporter: &dyn report::Report,
) -> Result<(), std::io::Error> {
let mut msg = report::Error::new(err.to_string());
msg.context = path.map(|path| report::Context::Path(report::PathContext { path }));
reporter.report(msg.into())?;
Ok(())
}
Expand Down Expand Up @@ -672,7 +682,7 @@ fn walk_entry(
let entry = match entry {
Ok(entry) => entry,
Err(err) => {
report_error(err, reporter)?;
report_error(err, None, reporter)?;
return Ok(());
}
};
Expand Down

0 comments on commit 104ebdc

Please sign in to comment.