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