diff options
author | Akshay <[email protected]> | 2021-11-06 09:21:55 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-11-06 09:21:55 +0000 |
commit | 324a333e671ed521138ad50fe463ed187874176e (patch) | |
tree | 8f294a109955881a14dbbb2bc088f14255fad687 /bin/src/main.rs | |
parent | 63eba9d5c697e466d952588568719d0de1244ae4 (diff) |
introducing "streaming" option to check, fix and single
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r-- | bin/src/main.rs | 85 |
1 files changed, 5 insertions, 80 deletions
diff --git a/bin/src/main.rs b/bin/src/main.rs index 3adf42e..fabc509 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs | |||
@@ -6,94 +6,19 @@ mod fix; | |||
6 | mod lint; | 6 | mod lint; |
7 | mod traits; | 7 | mod traits; |
8 | 8 | ||
9 | use std::io::{self, BufRead}; | 9 | use crate::err::StatixErr; |
10 | |||
11 | use crate::{ | ||
12 | err::{FixErr, SingleFixErr, StatixErr}, | ||
13 | traits::WriteDiagnostic, | ||
14 | }; | ||
15 | 10 | ||
16 | use clap::Clap; | 11 | use clap::Clap; |
17 | use config::{Opts, SubCommand}; | 12 | use config::{Opts, SubCommand}; |
18 | use similar::TextDiff; | ||
19 | 13 | ||
20 | fn _main() -> Result<(), StatixErr> { | 14 | fn _main() -> Result<(), StatixErr> { |
21 | let opts = Opts::parse(); | 15 | let opts = Opts::parse(); |
22 | match opts.cmd { | 16 | match opts.cmd { |
23 | SubCommand::Check(check_config) => { | 17 | SubCommand::Check(config) => lint::main::main(config), |
24 | let vfs = check_config.vfs()?; | 18 | SubCommand::Fix(config) => fix::main::all(config), |
25 | let mut stderr = io::stderr(); | 19 | SubCommand::Single(config) => fix::main::single(config), |
26 | vfs.iter().map(lint::lint).for_each(|r| { | 20 | SubCommand::Explain(config) => explain::main::main(config), |
27 | stderr.write(&r, &vfs, check_config.format).unwrap(); | ||
28 | }); | ||
29 | } | ||
30 | SubCommand::Fix(fix_config) => { | ||
31 | let vfs = fix_config.vfs()?; | ||
32 | for entry in vfs.iter() { | ||
33 | if let Some(fix_result) = fix::all(entry.contents) { | ||
34 | if fix_config.diff_only { | ||
35 | let text_diff = TextDiff::from_lines(entry.contents, &fix_result.src); | ||
36 | let old_file = format!("{}", entry.file_path.display()); | ||
37 | let new_file = format!("{} [fixed]", entry.file_path.display()); | ||
38 | println!( | ||
39 | "{}", | ||
40 | text_diff | ||
41 | .unified_diff() | ||
42 | .context_radius(4) | ||
43 | .header(&old_file, &new_file) | ||
44 | ); | ||
45 | } else { | ||
46 | let path = entry.file_path; | ||
47 | std::fs::write(path, &*fix_result.src).map_err(FixErr::InvalidPath)?; | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | // FIXME: this block nasty, configure in/out streams in `impl Single` maybe | ||
53 | SubCommand::Single(single_config) => { | ||
54 | let src = if let Some(path) = &single_config.target { | ||
55 | std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)? | ||
56 | } else { | ||
57 | io::stdin() | ||
58 | .lock() | ||
59 | .lines() | ||
60 | .map(|l| l.unwrap()) | ||
61 | .collect::<Vec<String>>() | ||
62 | .join("\n") | ||
63 | }; | ||
64 | |||
65 | let path_id = if let Some(path) = &single_config.target { | ||
66 | path.display().to_string() | ||
67 | } else { | ||
68 | "<unknown>".to_owned() | ||
69 | }; | ||
70 | |||
71 | let (line, col) = single_config.position; | ||
72 | let single_fix_result = fix::single(line, col, &src)?; | ||
73 | if single_config.diff_only { | ||
74 | let text_diff = TextDiff::from_lines(src.as_str(), &single_fix_result.src); | ||
75 | let old_file = path_id.to_string(); | ||
76 | let new_file = format!("{} [fixed]", path_id); | ||
77 | println!( | ||
78 | "{}", | ||
79 | text_diff | ||
80 | .unified_diff() | ||
81 | .context_radius(4) | ||
82 | .header(&old_file, &new_file) | ||
83 | ); | ||
84 | } else if let Some(path) = single_config.target { | ||
85 | std::fs::write(&path, &*single_fix_result.src) | ||
86 | .map_err(SingleFixErr::InvalidPath)?; | ||
87 | } else { | ||
88 | print!("{}", &*single_fix_result.src) | ||
89 | } | ||
90 | } | ||
91 | SubCommand::Explain(explain_config) => { | ||
92 | let explanation = explain::explain(explain_config.target)?; | ||
93 | println!("{}", explanation) | ||
94 | } | ||
95 | } | 21 | } |
96 | Ok(()) | ||
97 | } | 22 | } |
98 | 23 | ||
99 | fn main() { | 24 | fn main() { |