diff --git a/src/config.rs b/src/config.rs index 05338cc..cee2261 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,3 @@ - - use clap::ArgMatches; use error::{CliError, CliResult}; @@ -34,9 +32,11 @@ impl<'a> Config<'a> { if let Some(ext_vec) = m.values_of("language") { for e in ext_vec { if let None = Language::from_ext(e) { - return Err(CliError::UnknownExt(format!("unsupported source code extension \ - '{}'", - e.to_owned()))); + return Err(CliError::UnknownExt(format!( + "unsupported source code extension \ + '{}'", + e.to_owned() + ))); } } } diff --git a/src/count/counts.rs b/src/count/counts.rs index 7a26dea..16948b2 100644 --- a/src/count/counts.rs +++ b/src/count/counts.rs @@ -1,5 +1,3 @@ - - use comment::Comment; use config::{Config, Utf8Rule}; use count::Count; @@ -54,11 +52,13 @@ impl<'c> Counts<'c> { for path in &self.cfg.to_count { debugln!("iter; path={:?};", path); let mut files = vec![]; - fsutil::get_all_files(&mut files, - path, - &self.cfg.exclude, - self.cfg.follow_links, - &gitignore); + fsutil::get_all_files( + &mut files, + path, + &self.cfg.exclude, + self.cfg.follow_links, + &gitignore, + ); for file in files { debugln!("iter; file={:?};", file); @@ -200,15 +200,19 @@ impl<'c> Counts<'c> { debugln!("There is a keyword"); debugln!("line={:?}", line); if is_in_unsafe { - debugln!("It didn't contain the keyword, but we are still in \ - unsafe"); + debugln!( + "It didn't contain the keyword, but we are still in \ + unsafe" + ); count.usafe += 1; bracket_count = Counts::count_brackets(line, Some(bracket_count)); is_in_unsafe = bracket_count > 0; - debugln!("after counting brackets; is_in_unsafe={:?}; \ - bracket_count={:?}", - is_in_unsafe, - bracket_count); + debugln!( + "after counting brackets; is_in_unsafe={:?}; \ + bracket_count={:?}", + is_in_unsafe, + bracket_count + ); } else if let Some(caps) = re.captures(line) { let mut should_count = true; if let Some(before) = caps.at(1) { @@ -221,8 +225,9 @@ impl<'c> Counts<'c> { } } if let Some(multi) = count.lang.multi_start() { - if before.contains(multi) && - !before.contains(count.lang.multi_end().unwrap()) { + if before.contains(multi) + && !before.contains(count.lang.multi_end().unwrap()) + { should_count = false; } } @@ -234,10 +239,12 @@ impl<'c> Counts<'c> { debugln!("after_usafe={:?}", after); bracket_count = Counts::count_brackets(after, None); is_in_unsafe = bracket_count > 0; - debugln!("after counting brackets; is_in_unsafe={:?}; \ - bracket_count={:?}", - is_in_unsafe, - bracket_count); + debugln!( + "after counting brackets; is_in_unsafe={:?}; \ + bracket_count={:?}", + is_in_unsafe, + bracket_count + ); } } } else { @@ -269,12 +276,16 @@ impl<'c> Counts<'c> { pub fn write_results(&mut self) -> CliResult<()> { let mut w = TabWriter::new(vec![]); - cli_try!(write!(w, - "\tLanguage\tFiles\tLines\tBlanks\tComments\tCode{}\n", - if self.cfg.usafe { "\tUnsafe (%)" } else { "" })); - cli_try!(write!(w, - "\t--------\t-----\t-----\t------\t--------\t----{}\n", - if self.cfg.usafe { "\t----------" } else { "" })); + cli_try!(write!( + w, + "\tLanguage\tFiles\tLines\tBlanks\tComments\tCode{}\n", + if self.cfg.usafe { "\tUnsafe (%)" } else { "" } + )); + cli_try!(write!( + w, + "\t--------\t-----\t-----\t------\t--------\t----{}\n", + if self.cfg.usafe { "\t----------" } else { "" } + )); for count in &self.counts { if self.cfg.usafe { let usafe_per = if count.code != 0 { @@ -282,53 +293,67 @@ impl<'c> Counts<'c> { } else { 0f64 }; - cli_try!(write!(w, - "\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n", - count.lang.name(), - count.total_files(), - count.lines(), - count.blanks(), - count.comments(), - count.code(), - if (usafe_per - 00f64).abs() < f64::EPSILON { - "".to_owned() - } else { - format!("{} ({:.2}%)", count.usafe(), usafe_per) - })); + cli_try!(write!( + w, + "\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n", + count.lang.name(), + count.total_files(), + count.lines(), + count.blanks(), + count.comments(), + count.code(), + if (usafe_per - 00f64).abs() < f64::EPSILON { + "".to_owned() + } else { + format!("{} ({:.2}%)", count.usafe(), usafe_per) + } + )); } else { cli_try!(write!(w, "\t{}\n", count)); } } - cli_try!(write!(w, - "\t--------\t-----\t-----\t------\t--------\t----{}\n", - if self.cfg.usafe { "\t----------" } else { "" })); - cli_try!(write!(w, - "{}\t\t{}\t{}\t{}\t{}\t{}{}\n", - "Totals:", - fmt::format_number(self.tot as u64, self.cfg.thousands), - fmt::format_number(self.tot_lines, self.cfg.thousands), - fmt::format_number(self.tot_blanks, self.cfg.thousands), - fmt::format_number(self.tot_comments, self.cfg.thousands), - fmt::format_number(self.tot_code, self.cfg.thousands), - if self.cfg.usafe { - format!("\t{} ({:.2}%)", - fmt::format_number(self.tot_usafe, self.cfg.thousands), - (self.tot_usafe as f64 / self.tot_code as f64) * 100.00f64) - } else { - "".to_owned() - })); + cli_try!(write!( + w, + "\t--------\t-----\t-----\t------\t--------\t----{}\n", + if self.cfg.usafe { "\t----------" } else { "" } + )); + cli_try!(write!( + w, + "{}\t\t{}\t{}\t{}\t{}\t{}{}\n", + "Totals:", + fmt::format_number(self.tot as u64, self.cfg.thousands), + fmt::format_number(self.tot_lines, self.cfg.thousands), + fmt::format_number(self.tot_blanks, self.cfg.thousands), + fmt::format_number(self.tot_comments, self.cfg.thousands), + fmt::format_number(self.tot_code, self.cfg.thousands), + if self.cfg.usafe { + format!( + "\t{} ({:.2}%)", + fmt::format_number(self.tot_usafe, self.cfg.thousands), + (self.tot_usafe as f64 / self.tot_code as f64) * 100.00f64 + ) + } else { + "".to_owned() + } + )); cli_try!(w.flush()); - verboseln!(self.cfg, - "{} {}", - Format::Good("Displaying"), - "the results:"); + verboseln!( + self.cfg, + "{} {}", + Format::Good("Displaying"), + "the results:" + ); if self.tot > 0 { - write!(io::stdout(), - "{}", - String::from_utf8(w.unwrap()).ok().expect("failed to get valid UTF-8 String")) - .expect("failed to write output"); + write!( + io::stdout(), + "{}", + String::from_utf8(w.unwrap()) + .ok() + .expect("failed to get valid UTF-8 String") + ) + .expect("failed to write output"); } else { println!("\n\tNo source files were found matching the specified criteria"); } diff --git a/src/count/mod.rs b/src/count/mod.rs index d242043..e281cbf 100644 --- a/src/count/mod.rs +++ b/src/count/mod.rs @@ -1,9 +1,8 @@ mod counts; - +pub use self::counts::Counts; use fmt; use language::Language; -pub use self::counts::Counts; use std::fmt as StdFmt; use std::ops::Deref; @@ -73,13 +72,15 @@ impl Deref for Count { impl StdFmt::Display for Count { fn fmt(&self, f: &mut StdFmt::Formatter) -> StdFmt::Result { - write!(f, - "{}\t{}\t{}\t{}\t{}\t{}", - self.lang, - self.total_files(), - self.lines(), - self.blanks(), - self.comments(), - self.code()) + write!( + f, + "{}\t{}\t{}\t{}\t{}\t{}", + self.lang, + self.total_files(), + self.lines(), + self.blanks(), + self.comments(), + self.code() + ) } } diff --git a/src/error.rs b/src/error.rs index c3a7401..c4003e5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,8 +1,7 @@ - - -use fmt::Format;use std::error::Error; -use std::fmt::{Display, Formatter}; +use fmt::Format; +use std::error::Error; use std::fmt::Result as FmtResult; +use std::fmt::{Display, Formatter}; pub type CliResult = Result; @@ -51,7 +50,9 @@ impl Error for CliError { match *self { CliError::Generic(ref d) => &*d, CliError::UnknownExt(ref d) => &*d, - CliError::Unknown => "An unknown fatal error has occurred, please consider filing a bug-report!", + CliError::Unknown => { + "An unknown fatal error has occurred, please consider filing a bug-report!" + } } } diff --git a/src/fmt.rs b/src/fmt.rs index 3ba67a2..bd1d29c 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -1,9 +1,9 @@ - #[cfg(all(feature = "color", not(target_os = "windows")))] use ansi_term::ANSIString; #[cfg(all(feature = "color", not(target_os = "windows")))] -use ansi_term::Colour::{Green, Red, Yellow};use std::fmt; +use ansi_term::Colour::{Green, Red, Yellow}; +use std::fmt; #[allow(dead_code)] pub enum Format { diff --git a/src/fsutil.rs b/src/fsutil.rs index ce2a959..94877ed 100644 --- a/src/fsutil.rs +++ b/src/fsutil.rs @@ -1,19 +1,23 @@ - use gitignore::File; -use glob;use std::fs; +use glob; +use std::fs; use std::io::Result; use std::path::PathBuf; -pub fn get_all_files(v: &mut Vec, - path: &PathBuf, - exclude: &[PathBuf], - follow_links: bool, - gitignore: &Option) { - debugln!("executing; get_all_files; path={:?}; exclude={:?}; all={:?}", - path, - exclude, - all); +pub fn get_all_files( + v: &mut Vec, + path: &PathBuf, + exclude: &[PathBuf], + follow_links: bool, + gitignore: &Option, +) { + debugln!( + "executing; get_all_files; path={:?}; exclude={:?}; all={:?}", + path, + exclude, + all + ); if exclude.contains(path) { return; } @@ -33,19 +37,22 @@ pub fn get_all_files(v: &mut Vec, for entry in dir { let entry = entry.unwrap(); let file_path = entry.path(); - get_all_files(v, - &file_path.to_path_buf(), - exclude, - follow_links, - gitignore); + get_all_files( + v, + &file_path.to_path_buf(), + exclude, + follow_links, + gitignore, + ); } } else { debugln!("It's a file"); v.push(path.clone()); } } else { - for path_buf in glob::glob(path.to_str().unwrap_or("")) - .expect("failed to get files from glob") { + for path_buf in + glob::glob(path.to_str().unwrap_or("")).expect("failed to get files from glob") + { if let Ok(file_path) = path_buf { if let Ok(result) = get_metadata(&file_path, follow_links) { if result.is_dir() { @@ -54,11 +61,13 @@ pub fn get_all_files(v: &mut Vec, for entry in dir { let entry = entry.unwrap(); let file_path = entry.path(); - get_all_files(v, - &file_path.to_path_buf(), - exclude, - follow_links, - gitignore); + get_all_files( + v, + &file_path.to_path_buf(), + exclude, + follow_links, + gitignore, + ); } } else { debugln!("It's a file"); diff --git a/src/language.rs b/src/language.rs index bf37841..8892887 100644 --- a/src/language.rs +++ b/src/language.rs @@ -1,5 +1,3 @@ - - use comment::Comment; use std::fmt as StdFmt; @@ -47,8 +45,8 @@ impl Language { "pl" => Some(Language::Perl), "go" => Some(Language::Go), "agc" | "asm" | "a51" | "inc" | "nasm" | "s" | "ms" => Some(Language::Assembly), - "ps1" | "psd1" | "psm1" | "sh" | "bash" | "bats" | "cgi" | "command" | "fcgi" | - "ksh" | "sh.in" | "tmux" | "tool" | "zsh" | "tcsh" | "csh" | "fish" => { + "ps1" | "psd1" | "psm1" | "sh" | "bash" | "bats" | "cgi" | "command" | "fcgi" + | "ksh" | "sh.in" | "tmux" | "tool" | "zsh" | "tcsh" | "csh" | "fish" => { Some(Language::Shell) } "d" | "di" => Some(Language::D), @@ -84,8 +82,13 @@ impl Language { pub fn is_unsafe(&self) -> bool { match *self { - Language::C | Language::Cpp | Language::Hpp | Language::Header | Language::Rust | - Language::Assembly | Language::Nim => true, + Language::C + | Language::Cpp + | Language::Hpp + | Language::Header + | Language::Rust + | Language::Assembly + | Language::Nim => true, _ => false, } } @@ -104,28 +107,46 @@ impl StdFmt::Display for Language { } } - impl Comment for Language { type Rep = &'static str; fn single(&self) -> Option::Rep>> { match *self { - Language::C | Language::Cpp | Language::Hpp | Language::Header | Language::Css | - Language::Java | Language::JavaScript | Language::Rust | Language::Go | Language::D => { - Some(vec!["//"]) - } + Language::C + | Language::Cpp + | Language::Hpp + | Language::Header + | Language::Css + | Language::Java + | Language::JavaScript + | Language::Rust + | Language::Go + | Language::D => Some(vec!["//"]), Language::Php => Some(vec!["//", "#"]), Language::Xml | Language::Html => Some(vec![""), Language::Ruby => Some("=end"), Language::Python => Some("'''"), diff --git a/src/macros.rs b/src/macros.rs index 564a4ac..75c467f 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,11 +1,11 @@ macro_rules! cli_try { - ($t:expr) => ({ - use ::std::error::Error; + ($t:expr) => {{ + use std::error::Error; match $t { Ok(o) => o, - Err(e) => return Err(CliError::Generic(e.description().to_owned())) + Err(e) => return Err(CliError::Generic(e.description().to_owned())), } - }) + }}; } macro_rules! wlnerr( ($($arg:tt)*) => ({ @@ -53,12 +53,12 @@ macro_rules! debug { #[cfg(not(feature = "debug"))] macro_rules! debugln { - ($fmt:expr) => (); - ($fmt:expr, $($arg:tt)*) => (); + ($fmt:expr) => {}; + ($fmt:expr, $($arg:tt)*) => {}; } #[cfg(not(feature = "debug"))] macro_rules! debug { - ($fmt:expr) => (); - ($fmt:expr, $($arg:tt)*) => (); + ($fmt:expr) => {}; + ($fmt:expr, $($arg:tt)*) => {}; } diff --git a/src/main.rs b/src/main.rs index 39df45b..fd75834 100644 --- a/src/main.rs +++ b/src/main.rs @@ -169,23 +169,29 @@ #![cfg_attr(feature = "lints", allow(should_implement_trait))] #![cfg_attr(feature = "lints", allow(unstable_features))] #![cfg_attr(feature = "lints", deny(warnings))] -#![cfg_attr(not(any(feature = "nightly", feature = "unstable")), deny(unstable_features))] -#![deny(missing_docs, - missing_debug_implementations, - missing_copy_implementations, - trivial_casts, trivial_numeric_casts, - unsafe_code, - unused_import_braces, - unused_qualifications)] +#![cfg_attr( + not(any(feature = "nightly", feature = "unstable")), + deny(unstable_features) +)] +#![deny( + missing_docs, + missing_debug_implementations, + missing_copy_implementations, + trivial_casts, + trivial_numeric_casts, + unsafe_code, + unused_import_braces, + unused_qualifications +)] #[macro_use] extern crate clap; #[cfg(feature = "color")] extern crate ansi_term; -extern crate tabwriter; +extern crate gitignore; extern crate glob; extern crate regex; -extern crate gitignore; +extern crate tabwriter; #[cfg(feature = "debug")] use std::env; @@ -210,8 +216,10 @@ mod language; static UTF8_RULES: [&'static str; 3] = ["strict", "lossy", "ignore"]; fn main() { - debugln!("executing; cmd=cargo-count; args={:?}", - env::args().collect::>()); + debugln!( + "executing; cmd=cargo-count; args={:?}", + env::args().collect::>() + ); let m = App::new("cargo-count") .version(concat!("v", crate_version!())) // We have to lie about our binary name since this will be a third party @@ -265,18 +273,19 @@ the current directory you could do '--exclude */test.rs'.")) fn execute(cfg: Config) -> CliResult<()> { debugln!("executing; cmd=execute;"); verboseln!(cfg, "{}: {:?}", Format::Warning("Excluding"), cfg.exclude); - verbose!(cfg, - "{}", - if cfg.exts.is_some() { - format!("{} including files with extension: {}\n", - Format::Warning("Only"), - cfg.exts - .as_ref() - .unwrap() - .join(", ")) - } else { - "".to_owned() - }); + verbose!( + cfg, + "{}", + if cfg.exts.is_some() { + format!( + "{} including files with extension: {}\n", + Format::Warning("Only"), + cfg.exts.as_ref().unwrap().join(", ") + ) + } else { + "".to_owned() + } + ); debugln!("Checking for files or dirs to count from cli"); @@ -291,9 +300,9 @@ fn single_char(s: String) -> Result<(), String> { if s.len() == 1 { Ok(()) } else { - Err( - format!( + Err(format!( "the --separator argument option only accepts a single character but found '{}'", - Format::Warning(s))) + Format::Warning(s) + )) } }