From 9d14acbe0ae3df18f7861f1a0bf4b16d52394126 Mon Sep 17 00:00:00 2001 From: rvcas Date: Sat, 5 Nov 2022 16:23:46 -0400 Subject: [PATCH] fix: when formatting and add some methods to Project::Error --- crates/cli/src/cmd/fmt.rs | 2 +- crates/cli/src/lib.rs | 5 +---- crates/lang/src/format.rs | 9 ++++----- crates/lang/src/parser.rs | 1 + crates/project/src/error.rs | 29 ++++++++++++++++++++++++++++- crates/project/src/format.rs | 9 ++++----- examples/sample/src/scripts/swap.ak | 2 +- 7 files changed, 40 insertions(+), 17 deletions(-) diff --git a/crates/cli/src/cmd/fmt.rs b/crates/cli/src/cmd/fmt.rs index 9706a3a3..99ec1789 100644 --- a/crates/cli/src/cmd/fmt.rs +++ b/crates/cli/src/cmd/fmt.rs @@ -24,7 +24,7 @@ pub fn exec( if let Err(err) = aiken_project::format::run(stdin, check, files) { err.report(); - miette::bail!("failed: {} error(s)", err.total()); + miette::bail!("failed: {} error(s)", err.len()); }; Ok(()) diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 8813aecf..9994ca2e 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -30,10 +30,7 @@ where if let Err(err) = build_result { err.report(); - miette::bail!( - "failed: {} error(s), {warning_count} warning(s)", - err.total(), - ); + miette::bail!("failed: {} error(s), {warning_count} warning(s)", err.len(),); }; println!("finished with {warning_count} warning(s)"); diff --git a/crates/lang/src/format.rs b/crates/lang/src/format.rs index a3c2640e..82c15f73 100644 --- a/crates/lang/src/format.rs +++ b/crates/lang/src/format.rs @@ -165,9 +165,9 @@ impl<'comments> Formatter<'comments> { } 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 // with any statement, and are moved to the bottom of the module. let doc_comments = join( @@ -196,7 +196,7 @@ impl<'comments> Formatter<'comments> { nil() }; - let non_empty = vec![module_comments, groups, doc_comments, comments] + let non_empty = vec![module_comments, defs, doc_comments, comments] .into_iter() .filter(|doc| !doc.is_empty()); @@ -753,10 +753,9 @@ impl<'comments> Formatter<'comments> { subjects.iter().map(|s| self.wrap_expr(s)), break_(",", ", "), )) - .append(break_(" is", " is ")) .nest(INDENT) .append(break_("", " ")) - .append("{") + .append("is {") .group(); let clauses_doc = concat( diff --git a/crates/lang/src/parser.rs b/crates/lang/src/parser.rs index fa57ef84..0e0560a2 100644 --- a/crates/lang/src/parser.rs +++ b/crates/lang/src/parser.rs @@ -226,6 +226,7 @@ pub fn fn_parser() -> impl Parser 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 { diff --git a/crates/project/src/format.rs b/crates/project/src/format.rs index 6d8716b7..d4c64f3d 100644 --- a/crates/project/src/format.rs +++ b/crates/project/src/format.rs @@ -77,7 +77,7 @@ fn format_files(files: Vec) -> Result<(), Error> { fn unformatted_files(files: Vec) -> Result, 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) -> Result, 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) } } diff --git a/examples/sample/src/scripts/swap.ak b/examples/sample/src/scripts/swap.ak index 8910d81d..d20fa281 100644 --- a/examples/sample/src/scripts/swap.ak +++ b/examples/sample/src/scripts/swap.ak @@ -9,7 +9,7 @@ pub type Redeemer { } pub fn spend(datum: Datum, rdmr: Redeemer, ctx: spend.ScriptContext) -> Bool { - when rdmr is { + when rdmr is { Buy -> True Sell -> datum.something == "Aiken" }