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
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
7 changed files with 40 additions and 17 deletions

View File

@ -24,7 +24,7 @@ pub fn exec(
if let Err(err) = aiken_project::format::run(stdin, check, files) { if let Err(err) = aiken_project::format::run(stdin, check, files) {
err.report(); err.report();
miette::bail!("failed: {} error(s)", err.total()); miette::bail!("failed: {} error(s)", err.len());
}; };
Ok(()) Ok(())

View File

@ -30,10 +30,7 @@ where
if let Err(err) = build_result { if let Err(err) = build_result {
err.report(); err.report();
miette::bail!( miette::bail!("failed: {} error(s), {warning_count} warning(s)", err.len(),);
"failed: {} error(s), {warning_count} warning(s)",
err.total(),
);
}; };
println!("finished with {warning_count} warning(s)"); println!("finished with {warning_count} warning(s)");

View File

@ -165,9 +165,9 @@ impl<'comments> Formatter<'comments> {
} }
fn module<'a>(&mut self, module: &'a UntypedModule) -> Document<'a> { fn module<'a>(&mut self, module: &'a UntypedModule) -> Document<'a> {
let groups = self.definitions(&module.definitions); let defs = self.definitions(&module.definitions);
// Now that `groups` has been collected, only freestanding comments (//) // Now that `defs` has been collected, only freestanding comments (//)
// and doc comments (///) remain. Freestanding comments aren't associated // and doc comments (///) remain. Freestanding comments aren't associated
// with any statement, and are moved to the bottom of the module. // with any statement, and are moved to the bottom of the module.
let doc_comments = join( let doc_comments = join(
@ -196,7 +196,7 @@ impl<'comments> Formatter<'comments> {
nil() nil()
}; };
let non_empty = vec![module_comments, groups, doc_comments, comments] let non_empty = vec![module_comments, defs, doc_comments, comments]
.into_iter() .into_iter()
.filter(|doc| !doc.is_empty()); .filter(|doc| !doc.is_empty());
@ -753,10 +753,9 @@ impl<'comments> Formatter<'comments> {
subjects.iter().map(|s| self.wrap_expr(s)), subjects.iter().map(|s| self.wrap_expr(s)),
break_(",", ", "), break_(",", ", "),
)) ))
.append(break_(" is", " is "))
.nest(INDENT) .nest(INDENT)
.append(break_("", " ")) .append(break_("", " "))
.append("{") .append("is {")
.group(); .group();
let clauses_doc = concat( let clauses_doc = concat(

View File

@ -226,6 +226,7 @@ pub fn fn_parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseEr
.then( .then(
fn_param_parser() fn_param_parser()
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen)) .delimited_by(just(Token::LeftParen), just(Token::RightParen))
.map_with_span(|arguments, span| (arguments, span)), .map_with_span(|arguments, span| (arguments, span)),
) )

View File

@ -53,7 +53,7 @@ pub enum Error {
} }
impl Error { impl Error {
pub fn total(&self) -> usize { pub fn len(&self) -> usize {
match self { match self {
Error::List(errors) => errors.len(), Error::List(errors) => errors.len(),
_ => 1, _ => 1,
@ -84,6 +84,33 @@ impl Error {
Error::List(errors) 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 { 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> { fn unformatted_files(files: Vec<String>) -> Result<Vec<Unformatted>, Error> {
let mut problem_files = Vec::with_capacity(files.len()); 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 { for file_path in files {
let path = PathBuf::from_str(&file_path).unwrap(); 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() { if path.is_dir() {
for path in aiken_files_excluding_gitignore(&path) { for path in aiken_files_excluding_gitignore(&path) {
if let Err(err) = format_file(&mut problem_files, 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) { } else if let Err(err) = format_file(&mut problem_files, path) {
println!("{:?}", err); errors = errors.append(err);
errors.push(err);
} }
} }
if errors.is_empty() { if errors.is_empty() {
Ok(problem_files) Ok(problem_files)
} else { } else {
Err(Error::List(errors)) Err(errors)
} }
} }