aboutsummaryrefslogtreecommitdiff
path: root/bin/src/main.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-10-23 08:11:52 +0100
committerAkshay <[email protected]>2021-10-23 08:11:52 +0100
commitc2f0582d1907dbef69e9ad42ba9d4301337fe1e8 (patch)
treeaacf975dfeb8bd416b70abfe8a21a2ce7c325d0c /bin/src/main.rs
parentdfcdaf91674461a5150902cb3fdb8f198367ff20 (diff)
initial implementation of multipass code fixer
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r--bin/src/main.rs33
1 files changed, 28 insertions, 5 deletions
diff --git a/bin/src/main.rs b/bin/src/main.rs
index 161dcab..4cd525a 100644
--- a/bin/src/main.rs
+++ b/bin/src/main.rs
@@ -1,27 +1,50 @@
1mod config; 1mod config;
2mod err; 2mod err;
3mod fix;
3mod lint; 4mod lint;
4mod traits; 5mod traits;
5 6
6use std::io; 7use std::io;
7 8
8use crate::{err::StatixErr, traits::WriteDiagnostic}; 9use crate::{
10 err::{FixErr, StatixErr},
11 traits::WriteDiagnostic,
12};
9 13
10use clap::Clap; 14use clap::Clap;
11use config::{LintConfig, Opts, SubCommand}; 15use config::{FixConfig, LintConfig, Opts, SubCommand};
16use similar::TextDiff;
12 17
13fn _main() -> Result<(), StatixErr> { 18fn _main() -> Result<(), StatixErr> {
14 let opts = Opts::parse(); 19 let opts = Opts::parse();
15 match opts.subcmd { 20 match opts.subcmd {
16 Some(SubCommand::Fix(_)) => { 21 Some(SubCommand::Fix(_)) => {
17 eprintln!("`fix` not yet supported"); 22 let fix_config = FixConfig::from_opts(opts)?;
23 let vfs = fix_config.vfs()?;
24 for entry in vfs.iter() {
25 match fix::fix(entry.contents) {
26 Ok(fix_result) => {
27 let text_diff = TextDiff::from_lines(entry.contents, &fix_result.src);
28 let old_file = format!("{}", entry.file_path.display());
29 let new_file = format!("{} [fixed]", entry.file_path.display());
30 println!(
31 "{}",
32 text_diff
33 .unified_diff()
34 .context_radius(4)
35 .header(&old_file, &new_file)
36 );
37 }
38 Err(e) => eprintln!("{}", FixErr::Parse(entry.file_path.to_path_buf(), e)),
39 }
40 }
18 } 41 }
19 None => { 42 None => {
20 let lint_config = LintConfig::from_opts(opts)?; 43 let lint_config = LintConfig::from_opts(opts)?;
21 let vfs = lint_config.vfs()?; 44 let vfs = lint_config.vfs()?;
22 let (reports, errors): (Vec<_>, Vec<_>) = 45 let (lints, errors): (Vec<_>, Vec<_>) =
23 vfs.iter().map(lint::lint).partition(Result::is_ok); 46 vfs.iter().map(lint::lint).partition(Result::is_ok);
24 let lint_results = reports.into_iter().map(Result::unwrap); 47 let lint_results = lints.into_iter().map(Result::unwrap);
25 let errors = errors.into_iter().map(Result::unwrap_err); 48 let errors = errors.into_iter().map(Result::unwrap_err);
26 49
27 let mut stderr = io::stderr(); 50 let mut stderr = io::stderr();