aboutsummaryrefslogtreecommitdiff
path: root/bin/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'bin/src/main.rs')
-rw-r--r--bin/src/main.rs85
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;
6mod lint; 6mod lint;
7mod traits; 7mod traits;
8 8
9use std::io::{self, BufRead}; 9use crate::err::StatixErr;
10
11use crate::{
12 err::{FixErr, SingleFixErr, StatixErr},
13 traits::WriteDiagnostic,
14};
15 10
16use clap::Clap; 11use clap::Clap;
17use config::{Opts, SubCommand}; 12use config::{Opts, SubCommand};
18use similar::TextDiff;
19 13
20fn _main() -> Result<(), StatixErr> { 14fn _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
99fn main() { 24fn main() {