diff options
author | Akshay <[email protected]> | 2021-10-23 08:11:52 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-23 08:11:52 +0100 |
commit | c2f0582d1907dbef69e9ad42ba9d4301337fe1e8 (patch) | |
tree | aacf975dfeb8bd416b70abfe8a21a2ce7c325d0c /bin/src/main.rs | |
parent | dfcdaf91674461a5150902cb3fdb8f198367ff20 (diff) |
initial implementation of multipass code fixer
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r-- | bin/src/main.rs | 33 |
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 @@ | |||
1 | mod config; | 1 | mod config; |
2 | mod err; | 2 | mod err; |
3 | mod fix; | ||
3 | mod lint; | 4 | mod lint; |
4 | mod traits; | 5 | mod traits; |
5 | 6 | ||
6 | use std::io; | 7 | use std::io; |
7 | 8 | ||
8 | use crate::{err::StatixErr, traits::WriteDiagnostic}; | 9 | use crate::{ |
10 | err::{FixErr, StatixErr}, | ||
11 | traits::WriteDiagnostic, | ||
12 | }; | ||
9 | 13 | ||
10 | use clap::Clap; | 14 | use clap::Clap; |
11 | use config::{LintConfig, Opts, SubCommand}; | 15 | use config::{FixConfig, LintConfig, Opts, SubCommand}; |
16 | use similar::TextDiff; | ||
12 | 17 | ||
13 | fn _main() -> Result<(), StatixErr> { | 18 | fn _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(); |