move error handling to fmt function

This commit is contained in:
Andika Demas Riyandi 2021-04-08 20:43:22 +07:00
parent d3ade342bd
commit ae7dc6a340
3 changed files with 51 additions and 58 deletions

View File

@ -15,8 +15,8 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v12
- uses: cachix/cachix-action@v8
- uses: cachix/install-nix-action@v13
- uses: cachix/cachix-action@v9
with:
name: numtide
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
@ -28,8 +28,8 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v12
- uses: cachix/cachix-action@v8
- uses: cachix/install-nix-action@v13
- uses: cachix/cachix-action@v9
with:
name: numtide
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'

View File

@ -168,7 +168,7 @@ pub fn run_treefmt(
let filtered_files: usize = matches.values().map(|x| x.len()).sum();
// Now run all the formatters and collect the formatted paths.
let new_matches = matches
let new_matches: BTreeMap<FormatterName, BTreeMap<PathBuf, Mtime>> = matches
.par_iter()
.map(|(formatter_name, path_mtime)| {
let paths: Vec<PathBuf> = path_mtime.keys().cloned().collect();
@ -181,53 +181,30 @@ pub fn run_treefmt(
} else {
let start_time = Instant::now();
match formatter.clone().fmt(&paths) {
// FIXME: do we care about the output?
Ok(out) => {
if !out.status.success() {
match out.status.code() {
Some(scode) => {
return Err(anyhow!(
"{}'s formatter failed: exit status {}",
&formatter,
scode
));
}
None => {
return Err(anyhow!(
"{}'s formatter failed: unknown formatter error",
&formatter
));
}
}
}
info!(
"{}: {} files processed in {:.2?}",
formatter.name,
paths.len(),
start_time.elapsed()
);
info!(
"{}: {} files processed in {:.2?}",
formatter.name,
paths.len(),
start_time.elapsed()
);
// Get the new mtimes and compare them to the original ones
let new_paths = paths
.clone()
.into_iter()
.fold(BTreeMap::new(), |mut sum, path| {
// unwrap: assume that the file still exists after formatting
let mtime = get_path_mtime(&path).unwrap();
sum.insert(path, mtime);
sum
});
// Get the new mtimes and compare them to the original ones
let new_paths = paths.into_iter().fold(BTreeMap::new(), |mut sum, path| {
// unwrap: assume that the file still exists after formatting
let mtime = get_path_mtime(&path).unwrap();
sum.insert(path, mtime);
sum
});
// Return the new mtimes
Ok((formatter_name.clone(), new_paths))
}
Err(err) => {
// FIXME: What is the right behaviour if a formatter has failed running?
// Assume the paths were not formatted
return Err(anyhow!("{} failed: {}", &formatter, err));
}
}
formatter.clone().fmt(&paths)?;
// Return the new mtimes
Ok((formatter_name.clone(), new_paths))
}
})
.collect::<Result<BTreeMap<FormatterName, BTreeMap<PathBuf, Mtime>>, _>>()?;
.collect::<anyhow::Result<BTreeMap<FormatterName, BTreeMap<PathBuf, Mtime>>>>()?;
timed_debug("format");
// Record the new matches in the cache

View File

@ -7,12 +7,7 @@ use log::debug;
use path_clean::PathClean;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{
fmt,
path::Path,
path::PathBuf,
process::{Command, Output},
};
use std::{fmt, path::Path, path::PathBuf, process::Command};
use which::which;
/// newtype for the formatter name
@ -89,7 +84,7 @@ pub struct Formatter {
impl Formatter {
/// Run the formatter on the given paths
// TODO: handle E2BIG
pub fn fmt(&self, paths: &[PathBuf]) -> Result<Output> {
pub fn fmt(&self, paths: &[PathBuf]) -> Result<()> {
let mut cmd_arg = Command::new(&self.command);
// Set the command to run under its working directory.
cmd_arg.current_dir(&self.work_dir);
@ -99,10 +94,31 @@ impl Formatter {
cmd_arg.args(paths);
// And run
match cmd_arg.output() {
Ok(out) => Ok(out),
Err(err) => Err(anyhow!("formatting error due to {}", err))
Ok(out) => {
if !out.status.success() {
match out.status.code() {
Some(scode) => {
return Err(anyhow!(
"{}'s formatter failed: exit status {}",
&self,
scode
));
}
None => {
return Err(anyhow!(
"{}'s formatter failed: unknown formatter error",
&self
));
}
}
}
Ok(())
}
Err(err) => {
// Assume the paths were not formatted
Err(anyhow!("{} failed: {}", &self, err))
}
}
// Ok(cmd_arg.output()?)
}
/// Returns the formatter if the path matches the formatter rules.