fix: when formatting and add some methods to Project::Error

This commit is contained in:
rvcas
2022-11-05 16:23:46 -04:00
parent 14724f924c
commit 9d14acbe0a
7 changed files with 40 additions and 17 deletions

View File

@@ -53,7 +53,7 @@ pub enum Error {
}
impl Error {
pub fn total(&self) -> usize {
pub fn len(&self) -> usize {
match self {
Error::List(errors) => errors.len(),
_ => 1,
@@ -84,6 +84,33 @@ impl Error {
Error::List(errors)
}
pub fn append(self, next: Self) -> Self {
match (self, next) {
(Error::List(mut errors), Error::List(mut next_errors)) => {
errors.append(&mut next_errors);
Error::List(errors)
}
(Error::List(mut errors), rest) => {
errors.push(rest);
Error::List(errors)
}
(rest, Error::List(mut next_errors)) => {
let mut errors = vec![rest];
errors.append(&mut next_errors);
Error::List(errors)
}
(error, next_error) => Error::List(vec![error, next_error]),
}
}
pub fn is_empty(&self) -> bool {
matches!(self, Error::List(errors) if errors.is_empty())
}
}
impl Debug for Error {

View File

@@ -77,7 +77,7 @@ fn format_files(files: Vec<String>) -> Result<(), Error> {
fn unformatted_files(files: Vec<String>) -> Result<Vec<Unformatted>, Error> {
let mut problem_files = Vec::with_capacity(files.len());
let mut errors = Vec::new();
let mut errors = Error::List(vec![]);
for file_path in files {
let path = PathBuf::from_str(&file_path).unwrap();
@@ -85,19 +85,18 @@ fn unformatted_files(files: Vec<String>) -> Result<Vec<Unformatted>, Error> {
if path.is_dir() {
for path in aiken_files_excluding_gitignore(&path) {
if let Err(err) = format_file(&mut problem_files, path) {
errors.push(err);
errors = errors.append(err);
};
}
} else if let Err(err) = format_file(&mut problem_files, path) {
println!("{:?}", err);
errors.push(err);
errors = errors.append(err);
}
}
if errors.is_empty() {
Ok(problem_files)
} else {
Err(Error::List(errors))
Err(errors)
}
}