Skip to content

Commit

Permalink
Format code using 'cargo fmt' (XAMPPRocky#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
Atul9 authored and XAMPPRocky committed Oct 30, 2019
1 parent 04a460e commit fcd555a
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 340 deletions.
49 changes: 25 additions & 24 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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);
Expand All @@ -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) => {{
Expand All @@ -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");
Expand All @@ -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(())
}

Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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)?)
Expand Down
26 changes: 13 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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")
}

Expand Down Expand Up @@ -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}
Expand All @@ -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(",")
);
}
}
}

89 changes: 55 additions & 34 deletions src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: {}",
Expand All @@ -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();
Expand All @@ -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 {
Expand All @@ -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
)
}
9 changes: 3 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
env,
fs,
path::PathBuf,
};
use std::{env, fs, path::PathBuf};

use crate::language::LanguageType;

Expand Down Expand Up @@ -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),
Expand Down
8 changes: 2 additions & 6 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -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>;

Expand Down
Loading

0 comments on commit fcd555a

Please sign in to comment.