aboutsummaryrefslogtreecommitdiff
path: root/bin/src/main.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-10-25 17:38:52 +0100
committerAkshay <[email protected]>2021-10-25 17:38:52 +0100
commit5f0a1e67c64082c848418daa2b51020eb42c5c12 (patch)
tree1d6d7db9ee5532e76d23b9f509a8299d0d34dc52 /bin/src/main.rs
parent781c42cc9ce2e6a3f1024ea1f4e3f071cc8f2dd4 (diff)
rework cli, use subcommands instead
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r--bin/src/main.rs72
1 files changed, 40 insertions, 32 deletions
diff --git a/bin/src/main.rs b/bin/src/main.rs
index d0f69a0..9c57d91 100644
--- a/bin/src/main.rs
+++ b/bin/src/main.rs
@@ -6,50 +6,58 @@ mod traits;
6 6
7use std::io; 7use std::io;
8 8
9use crate::{err::{StatixErr, FixErr}, traits::WriteDiagnostic}; 9use crate::{err::{StatixErr, FixErr, SingleFixErr}, traits::WriteDiagnostic};
10 10
11use clap::Clap; 11use clap::Clap;
12use config::{FixConfig, LintConfig, Opts}; 12use config::{Opts, SubCommand};
13use similar::TextDiff; 13use similar::TextDiff;
14 14
15fn _main() -> Result<(), StatixErr> { 15fn _main() -> Result<(), StatixErr> {
16 let opts = Opts::parse(); 16 let opts = Opts::parse();
17 if opts.fix { 17 match opts.cmd {
18 let fix_config = FixConfig::from_opts(opts)?; 18 SubCommand::Check(check_config) => {
19 let vfs = fix_config.vfs()?; 19 let vfs = check_config.vfs()?;
20 for entry in vfs.iter() { 20 let (lints, errors): (Vec<_>, Vec<_>) = vfs.iter().map(lint::lint).partition(Result::is_ok);
21 if let Some(fix_result) = fix::fix(entry.contents) { 21 let lint_results = lints.into_iter().map(Result::unwrap);
22 if fix_config.diff_only { 22 let errors = errors.into_iter().map(Result::unwrap_err);
23 let text_diff = TextDiff::from_lines(entry.contents, &fix_result.src); 23
24 let old_file = format!("{}", entry.file_path.display()); 24 let mut stdout = io::stdout();
25 let new_file = format!("{} [fixed]", entry.file_path.display()); 25 lint_results.for_each(|r| {
26 println!( 26 stdout.write(&r, &vfs, check_config.format).unwrap();
27 "{}", 27 });
28 text_diff 28 errors.for_each(|e| {
29 eprintln!("{}", e);
30 });
31 },
32 SubCommand::Fix(fix_config) => {
33 let vfs = fix_config.vfs()?;
34 for entry in vfs.iter() {
35 if let Some(fix_result) = fix::all(entry.contents) {
36 if fix_config.diff_only {
37 let text_diff = TextDiff::from_lines(entry.contents, &fix_result.src);
38 let old_file = format!("{}", entry.file_path.display());
39 let new_file = format!("{} [fixed]", entry.file_path.display());
40 println!(
41 "{}",
42 text_diff
29 .unified_diff() 43 .unified_diff()
30 .context_radius(4) 44 .context_radius(4)
31 .header(&old_file, &new_file) 45 .header(&old_file, &new_file)
32 ); 46 );
33 } else { 47 } else {
34 let path = entry.file_path; 48 let path = entry.file_path;
35 std::fs::write(path, &*fix_result.src).map_err(FixErr::InvalidPath)?; 49 std::fs::write(path, &*fix_result.src).map_err(FixErr::InvalidPath)?;
50 }
36 } 51 }
37 } 52 }
53 },
54 SubCommand::Single(single_config) => {
55 let path = single_config.target;
56 let src = std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)?;
57 let (line, col) = single_config.position;
58 let single_result = fix::single(line, col, &src)?;
59 std::fs::write(&path, &*single_result.src).map_err(SingleFixErr::InvalidPath)?;
38 } 60 }
39 } else {
40 let lint_config = LintConfig::from_opts(opts)?;
41 let vfs = lint_config.vfs()?;
42 let (lints, errors): (Vec<_>, Vec<_>) = vfs.iter().map(lint::lint).partition(Result::is_ok);
43 let lint_results = lints.into_iter().map(Result::unwrap);
44 let errors = errors.into_iter().map(Result::unwrap_err);
45
46 let mut stdout = io::stdout();
47 lint_results.for_each(|r| {
48 stdout.write(&r, &vfs, lint_config.format).unwrap();
49 });
50 errors.for_each(|e| {
51 eprintln!("{}", e);
52 });
53 } 61 }
54 Ok(()) 62 Ok(())
55} 63}