From a4389de1f3d16e8e185db81fa1fdd061d53ad02f Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Fri, 25 Feb 2022 22:38:18 -0500 Subject: [PATCH] refactor: mark as pub(crate) --- src/alejandra_cli/src/cli.rs | 92 +++++++++++++++++++++++++++++++++-- src/alejandra_cli/src/find.rs | 2 +- src/alejandra_cli/src/main.rs | 81 +----------------------------- 3 files changed, 89 insertions(+), 86 deletions(-) diff --git a/src/alejandra_cli/src/cli.rs b/src/alejandra_cli/src/cli.rs index 75b5507..f170b9f 100644 --- a/src/alejandra_cli/src/cli.rs +++ b/src/alejandra_cli/src/cli.rs @@ -1,10 +1,10 @@ #[derive(Clone)] -pub struct FormattedPath { +pub(crate) struct FormattedPath { pub path: String, pub status: alejandra_engine::format::Status, } -pub fn parse(args: Vec) -> clap::ArgMatches { +pub(crate) fn parse(args: Vec) -> clap::ArgMatches { clap::Command::new("Alejandra") .about("The Uncompromising Nix Code Formatter.") .version(alejandra_engine::version::VERSION) @@ -51,7 +51,7 @@ pub fn parse(args: Vec) -> clap::ArgMatches { .get_matches_from(args) } -pub fn stdin() -> FormattedPath { +pub(crate) fn stdin() -> FormattedPath { use std::io::Read; let mut before = String::new(); @@ -69,7 +69,7 @@ pub fn stdin() -> FormattedPath { FormattedPath { path, status } } -pub fn simple(paths: Vec) -> Vec { +pub(crate) fn simple(paths: Vec) -> Vec { use rayon::prelude::*; eprintln!("Formatting: {} files", paths.len()); @@ -90,7 +90,7 @@ pub fn simple(paths: Vec) -> Vec { .collect() } -pub fn tui(paths: Vec) -> std::io::Result> { +pub(crate) fn tui(paths: Vec) -> std::io::Result> { use rayon::prelude::*; use termion::{input::TermRead, raw::IntoRawMode}; @@ -305,3 +305,85 @@ pub fn tui(paths: Vec) -> std::io::Result> { Ok(formatted_paths.iter().cloned().collect()) } + +pub fn main() -> std::io::Result<()> { + let matches = crate::cli::parse(std::env::args().collect()); + + let check = matches.is_present("check"); + + let formatted_paths = match matches.values_of("include") { + Some(include) => { + let include = include.collect(); + let exclude = match matches.values_of("exclude") { + Some(exclude) => exclude.collect(), + None => vec![], + }; + + let paths: Vec = crate::find::nix_files(include, exclude); + + if atty::is(atty::Stream::Stderr) + && atty::is(atty::Stream::Stdin) + && atty::is(atty::Stream::Stdout) + { + crate::cli::tui(paths)? + } else { + crate::cli::simple(paths) + } + } + None => vec![crate::cli::stdin()], + }; + + let errors = formatted_paths + .iter() + .filter(|formatted_path| { + matches!( + formatted_path.status, + alejandra_engine::format::Status::Error(_) + ) + }) + .count(); + + if errors > 0 { + eprintln!(); + eprintln!( + "Failed! We encountered {} error{} at:", + errors, + if errors > 0 { "s" } else { "" } + ); + for formatted_path in formatted_paths { + if let alejandra_engine::format::Status::Error(error) = + formatted_path.status + { + eprintln!(" {}: {}", formatted_path.path, &error); + } + } + std::process::exit(1); + } + + let changed = formatted_paths + .iter() + .filter(|formatted_path| match formatted_path.status { + alejandra_engine::format::Status::Changed(changed) => changed, + _ => false, + }) + .count(); + + if changed > 0 { + eprintln!(); + eprintln!( + "Success! {} file{} {} changed", + changed, + if changed > 0 { "s" } else { "" }, + if changed > 0 { "were" } else { "was" }, + ); + if check { + std::process::exit(2); + } else { + std::process::exit(0); + } + } + + eprintln!(); + eprintln!("Success! Your code complies the Alejandra style"); + std::process::exit(0); +} diff --git a/src/alejandra_cli/src/find.rs b/src/alejandra_cli/src/find.rs index 69ffe2d..80833ba 100644 --- a/src/alejandra_cli/src/find.rs +++ b/src/alejandra_cli/src/find.rs @@ -1,4 +1,4 @@ -pub fn nix_files(include: Vec<&str>, exclude: Vec<&str>) -> Vec { +pub(crate) fn nix_files(include: Vec<&str>, exclude: Vec<&str>) -> Vec { let include: std::collections::HashSet = include.iter().flat_map(nix_files_in_path).collect(); let exclude: std::collections::HashSet = diff --git a/src/alejandra_cli/src/main.rs b/src/alejandra_cli/src/main.rs index a9ace90..4434c63 100644 --- a/src/alejandra_cli/src/main.rs +++ b/src/alejandra_cli/src/main.rs @@ -1,82 +1,3 @@ fn main() -> std::io::Result<()> { - let matches = alejandra_cli::cli::parse(std::env::args().collect()); - - let check = matches.is_present("check"); - - let formatted_paths = match matches.values_of("include") { - Some(include) => { - let include = include.collect(); - let exclude = match matches.values_of("exclude") { - Some(exclude) => exclude.collect(), - None => vec![], - }; - - let paths: Vec = - alejandra_cli::find::nix_files(include, exclude); - - if atty::is(atty::Stream::Stderr) - && atty::is(atty::Stream::Stdin) - && atty::is(atty::Stream::Stdout) - { - alejandra_cli::cli::tui(paths)? - } else { - alejandra_cli::cli::simple(paths) - } - } - None => vec![alejandra_cli::cli::stdin()], - }; - - let errors = formatted_paths - .iter() - .filter(|formatted_path| { - matches!( - formatted_path.status, - alejandra_engine::format::Status::Error(_) - ) - }) - .count(); - - if errors > 0 { - eprintln!(); - eprintln!( - "Failed! We encountered {} error{} at:", - errors, - if errors > 0 { "s" } else { "" } - ); - for formatted_path in formatted_paths { - if let alejandra_engine::format::Status::Error(error) = - formatted_path.status - { - eprintln!(" {}: {}", formatted_path.path, &error); - } - } - std::process::exit(1); - } - - let changed = formatted_paths - .iter() - .filter(|formatted_path| match formatted_path.status { - alejandra_engine::format::Status::Changed(changed) => changed, - _ => false, - }) - .count(); - - if changed > 0 { - eprintln!(); - eprintln!( - "Success! {} file{} {} changed", - changed, - if changed > 0 { "s" } else { "" }, - if changed > 0 { "were" } else { "was" }, - ); - if check { - std::process::exit(2); - } else { - std::process::exit(0); - } - } - - eprintln!(); - eprintln!("Success! Your code complies the Alejandra style"); - std::process::exit(0); + alejandra_cli::cli::main() }