From 324a333e671ed521138ad50fe463ed187874176e Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 6 Nov 2021 14:51:55 +0530 Subject: introducing "streaming" option to check, fix and single --- bin/src/main.rs | 85 ++++----------------------------------------------------- 1 file changed, 5 insertions(+), 80 deletions(-) (limited to 'bin/src/main.rs') 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; mod lint; mod traits; -use std::io::{self, BufRead}; - -use crate::{ - err::{FixErr, SingleFixErr, StatixErr}, - traits::WriteDiagnostic, -}; +use crate::err::StatixErr; 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::>() - .join("\n") - }; - - let path_id = if let Some(path) = &single_config.target { - path.display().to_string() - } else { - "".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) - } + SubCommand::Check(config) => lint::main::main(config), + SubCommand::Fix(config) => fix::main::all(config), + SubCommand::Single(config) => fix::main::single(config), + SubCommand::Explain(config) => explain::main::main(config), } - Ok(()) } fn main() { -- cgit v1.2.3