aboutsummaryrefslogtreecommitdiff
path: root/bin/src/main.rs
blob: 4cd525a21823251d89d5e963ef503a6223654d5c (plain)
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
mod config;
mod err;
mod fix;
mod lint;
mod traits;

use std::io;

use crate::{
    err::{FixErr, StatixErr},
    traits::WriteDiagnostic,
};

use clap::Clap;
use config::{FixConfig, LintConfig, Opts, SubCommand};
use similar::TextDiff;

fn _main() -> Result<(), StatixErr> {
    let opts = Opts::parse();
    match opts.subcmd {
        Some(SubCommand::Fix(_)) => {
            let fix_config = FixConfig::from_opts(opts)?;
            let vfs = fix_config.vfs()?;
            for entry in vfs.iter() {
                match fix::fix(entry.contents) {
                    Ok(fix_result) => {
                        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)
                        );
                    }
                    Err(e) => eprintln!("{}", FixErr::Parse(entry.file_path.to_path_buf(), e)),
                }
            }
        }
        None => {
            let lint_config = LintConfig::from_opts(opts)?;
            let vfs = lint_config.vfs()?;
            let (lints, errors): (Vec<_>, Vec<_>) =
                vfs.iter().map(lint::lint).partition(Result::is_ok);
            let lint_results = lints.into_iter().map(Result::unwrap);
            let errors = errors.into_iter().map(Result::unwrap_err);

            let mut stderr = io::stderr();
            lint_results.for_each(|r| {
                stderr.write(&r, &vfs).unwrap();
            });
            errors.for_each(|e| {
                eprintln!("{}", e);
            });
        }
    }
    Ok(())
}

fn main() {
    match _main() {
        Err(e) => eprintln!("{}", e),
        _ => (),
    }
}