Skip to content
Permalink
Browse files

Format code using 'cargo fmt' (#399)

  • Loading branch information...
Atul9 authored and XAMPPRocky committed Oct 30, 2019
1 parent 04a460e commit fcd555ad9f1d80078c4b2ed1f6fdabff29d82bab
Showing with 359 additions and 340 deletions.
  1. +25 −24 build.rs
  2. +13 −13 src/cli.rs
  3. +55 −34 src/cli_utils.rs
  4. +3 −6 src/config.rs
  5. +2 −6 src/input.rs
  6. +40 −52 src/language/language_type.rs
  7. +13 −14 src/language/languages.rs
  8. +4 −15 src/language/mod.rs
  9. +23 −23 src/language/syntax.rs
  10. +15 −10 src/lib.rs
  11. +6 −10 src/main.rs
  12. +2 −5 src/sort.rs
  13. +13 −15 src/stats.rs
  14. +4 −4 src/utils/ext.rs
  15. +112 −85 src/utils/fs.rs
  16. +16 −16 src/utils/macros.rs
  17. +1 −1 src/utils/mod.rs
  18. +12 −7 tests/accuracy.rs
@@ -2,14 +2,14 @@ extern crate handlebars;
extern crate ignore;
extern crate serde_json;

use std::{cmp, env, error};
use std::ffi::OsStr;
use std::fs::{self, File};
use std::path::Path;
use std::{cmp, env, error};

use handlebars::Handlebars;
use serde_json::Value;
use ignore::Walk;
use serde_json::Value;

fn main() -> Result<(), Box<dyn error::Error>> {
let out_dir = env::var_os("OUT_DIR").expect("No OUT_DIR variable.");
@@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
Ok(())
}

fn generate_languages(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
fn generate_languages(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
let handlebars = {
let mut h = Handlebars::new();
h.register_escape_fn(handlebars::no_escape);
@@ -28,11 +28,12 @@ fn generate_languages(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {

let mut json: Value = serde_json::from_reader(File::open(&"languages.json")?)?;

for (_key, ref mut item) in json.get_mut("languages")
.unwrap()
.as_object_mut()
.unwrap()
.iter_mut()
for (_key, ref mut item) in json
.get_mut("languages")
.unwrap()
.as_object_mut()
.unwrap()
.iter_mut()
{
macro_rules! sort_prop {
($prop:expr) => {{
@@ -41,7 +42,7 @@ fn generate_languages(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
.unwrap()
.sort_unstable_by(compare_json_str_len)
}
}}
}};
}

sort_prop!("quotes");
@@ -52,9 +53,7 @@ fn generate_languages(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
let mut source_template = File::open(&"src/language/language_type.hbs.rs")?;
let mut output_file = File::create(&output)?;

handlebars.render_template_source_to_write(&mut source_template,
&json,
&mut output_file)?;
handlebars.render_template_source_to_write(&mut source_template, &json, &mut output_file)?;
Ok(())
}

@@ -73,17 +72,15 @@ fn generate_tests(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
const INITIAL_BUFFER_SIZE: usize = 989 * 130;
let mut string = String::with_capacity(INITIAL_BUFFER_SIZE);

let walker = Walk::new("./tests/data/").filter(|p| {
match p {
&Ok(ref p) => {
if let Ok(ref p) = p.metadata() {
p.is_file()
} else {
false
}
},
_ => false,
let walker = Walk::new("./tests/data/").filter(|p| match p {
&Ok(ref p) => {
if let Ok(ref p) = p.metadata() {
p.is_file()
} else {
false
}
}
_ => false,
});

for path in walker {
@@ -92,7 +89,8 @@ fn generate_tests(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {

let name = path.file_stem().unwrap().to_str().unwrap().to_lowercase();

string.push_str(&format!(r#"
string.push_str(&format!(
r#"
#[test]
fn {0}() {{
let mut languages = Languages::new();
@@ -116,7 +114,10 @@ fn generate_tests(out_dir: &OsStr) -> Result<(), Box<dyn error::Error>> {
assert_eq!(get_digit!(BLANKS, contents), language.blanks);
println!("{{}} BLANKS MATCH", name);
}}
"#, name, path.display()));
"#,
name,
path.display()
));
}

Ok(fs::write(Path::new(&out_dir).join("tests.rs"), string)?)
@@ -74,7 +74,8 @@ impl<'a> Cli<'a> {
1: to show unknown file extensions,
2: reserved for future debugging,
3: enable file level trace. Not recommended on multiple files")
).get_matches();
)
.get_matches();

let columns = matches.value_of("columns").map(parse_or_exit::<usize>);
let files = matches.is_present("files");
@@ -86,9 +87,9 @@ impl<'a> Cli<'a> {
let verbose = matches.occurrences_of("verbose");
let types = matches.value_of("types").map(|e| {
e.split(',')
.map(|t| t.parse::<LanguageType>())
.filter_map(Result::ok)
.collect()
.map(|t| t.parse::<LanguageType>())
.filter_map(Result::ok)
.collect()
});

// Sorting category should be restricted by clap but parse before we do
@@ -97,8 +98,7 @@ impl<'a> Cli<'a> {
// Format category is overly accepting by clap (so the user knows what
// is supported) but this will fail if support is not compiled in and
// give a useful error to the user.
let output = matches.value_of("output")
.map(parse_or_exit::<Format>);
let output = matches.value_of("output").map(parse_or_exit::<Format>);

crate::cli_utils::setup_logger(verbose);

@@ -122,7 +122,7 @@ impl<'a> Cli<'a> {
cli
}

pub fn file_input(&self) -> Option<&str> {
pub fn file_input(&self) -> Option<&str> {
self.matches.value_of("file_input")
}

@@ -191,7 +191,8 @@ impl<'a> Cli<'a> {

let not_supported = Format::not_supported();
if !not_supported.is_empty() {
eprintln!("
eprintln!(
"
This version of tokei was compiled without serialization support for the following formats:
{not_supported}
@@ -204,11 +205,10 @@ Or use the 'all' feature:
cargo install tokei --features all
\n",
not_supported = not_supported.join(", "),
// no space after comma to ease copypaste
all = self::Format::all_feature_names().join(",")
);
not_supported = not_supported.join(", "),
// no space after comma to ease copypaste
all = self::Format::all_feature_names().join(",")
);
}
}
}

@@ -7,8 +7,8 @@ use std::{

use clap::crate_version;

use tokei::{Language, LanguageType};
use crate::input::Format;
use tokei::{Language, LanguageType};

pub const FALLBACK_ROW_LEN: usize = 79;
const NO_LANG_HEADER_ROW_LEN: usize = 67;
@@ -18,7 +18,10 @@ const IDENT_INACCURATE: &str = "(!)";

pub fn crate_version() -> String {
if Format::supported().is_empty() {
format!("{} compiled without serialization formats.", crate_version!())
format!(
"{} compiled without serialization formats.",
crate_version!()
)
} else {
format!(
"{} compiled with serialization support: {}",
@@ -45,35 +48,42 @@ pub fn setup_logger(verbose_option: u64) {
}

pub fn parse_or_exit<T>(s: &str) -> T
where T: FromStr,
T::Err: fmt::Display
where
T: FromStr,
T::Err: fmt::Display,
{
T::from_str(s).unwrap_or_else(|e| {
eprintln!("Error:\n{}", e);
process::exit(1);
})
}

pub fn print_header<W: Write>(sink: &mut W, row: &str, columns: usize)
-> io::Result<()>
{
pub fn print_header<W: Write>(sink: &mut W, row: &str, columns: usize) -> io::Result<()> {
writeln!(sink, "{}", row)?;
writeln!(sink, " {:<6$} {:>12} {:>12} {:>12} {:>12} {:>12}",
"Language",
"Files",
"Lines",
"Code",
"Comments",
"Blanks",
columns - NO_LANG_HEADER_ROW_LEN)?;
writeln!(
sink,
" {:<6$} {:>12} {:>12} {:>12} {:>12} {:>12}",
"Language",
"Files",
"Lines",
"Code",
"Comments",
"Blanks",
columns - NO_LANG_HEADER_ROW_LEN
)?;

writeln!(sink, "{}", row)
}

pub fn print_results<'a, I, W>(sink: &mut W, row: &str, languages: I, list_files: bool)
-> io::Result<()>
where I: Iterator<Item = (&'a LanguageType, &'a Language)>,
W: Write,
pub fn print_results<'a, I, W>(
sink: &mut W,
row: &str,
languages: I,
list_files: bool,
) -> io::Result<()>
where
I: Iterator<Item = (&'a LanguageType, &'a Language)>,
W: Write,
{
let path_len = row.len() - NO_LANG_ROW_LEN_NO_SPACES;
let columns = row.len();
@@ -96,12 +106,14 @@ pub fn isnt_empty(&(_, language): &(&LanguageType, &Language)) -> bool {
!language.is_empty()
}

pub fn print_language<W>(sink: &mut W,
columns: usize,
language: &Language,
name: &str)
-> io::Result<()>
where W: Write,
pub fn print_language<W>(
sink: &mut W,
columns: usize,
language: &Language,
name: &str,
) -> io::Result<()>
where
W: Write,
{
let mut lang_section_len = columns - NO_LANG_ROW_LEN;
if language.inaccurate {
@@ -118,15 +130,24 @@ pub fn print_language<W>(sink: &mut W,
write!(sink, "{}", IDENT_INACCURATE)?;
};
write!(sink, " ")?;
writeln!(sink,
"{:>6} {:>12} {:>12} {:>12} {:>12}",
language.stats.len(),
language.lines,
language.code,
language.comments,
language.blanks)
writeln!(
sink,
"{:>6} {:>12} {:>12} {:>12} {:>12}",
language.stats.len(),
language.lines,
language.code,
language.comments,
language.blanks
)
}

pub fn print_inaccuracy_warning<W>(sink: &mut W) -> io::Result<()> where W: Write {
writeln!(sink, "Note: results can be inaccurate for languages marked with '{}'", IDENT_INACCURATE)
pub fn print_inaccuracy_warning<W>(sink: &mut W) -> io::Result<()>
where
W: Write,
{
writeln!(
sink,
"Note: results can be inaccurate for languages marked with '{}'",
IDENT_INACCURATE
)
}
@@ -1,8 +1,4 @@
use std::{
env,
fs,
path::PathBuf,
};
use std::{env, fs, path::PathBuf};

use crate::language::LanguageType;

@@ -89,7 +85,8 @@ impl Config {
Config {
columns: current_dir.columns.or(conf_dir.columns),
//languages: current_dir.languages.or(conf_dir.languages),
treat_doc_strings_as_comments: current_dir.treat_doc_strings_as_comments
treat_doc_strings_as_comments: current_dir
.treat_doc_strings_as_comments
.or(conf_dir.treat_doc_strings_as_comments),
types: current_dir.types.or(conf_dir.types),
no_ignore: current_dir.no_ignore.or(conf_dir.no_ignore),
@@ -1,10 +1,6 @@
use std::{
collections::BTreeMap,
error::Error,
str::FromStr,
};
use std::{collections::BTreeMap, error::Error, str::FromStr};

use tokei::{Language, Languages, LanguageType};
use tokei::{Language, LanguageType, Languages};

type LanguageMap = BTreeMap<LanguageType, Language>;

0 comments on commit fcd555a

Please sign in to comment.
You can’t perform that action at this time.