Skip to content

Commit

Permalink
Fixed minor parser inaccuracies
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Power committed Jan 26, 2018
1 parent 39cd9d6 commit 553736f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 31 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,31 @@ fn generate_languages(out_dir: &OsStr) {
h
};

let json: Value = {
let mut json: Value = {
let json = File::open(&"languages.json").expect("Cant open json");
serde_json::from_reader(json).expect("Can't parse json")
};

for (key, ref mut item) in json.get_mut("languages")
.unwrap()
.as_object_mut()
.unwrap()
.iter_mut()
{
macro_rules! sort_prop {
($prop:expr) => {{
if let Some(ref mut prop) = item.get_mut($prop) {
prop.as_array_mut()
.unwrap()
.sort_unstable_by(compare_json_str_len)
}
}}
}

sort_prop!("quotes");
sort_prop!("multi_line");
}

let output = Path::new(&out_dir).join("language_type.rs");
let mut source_template = File::open(&"src/language/language_type.hbs.rs")
.expect("Can't find Template");
Expand All @@ -41,6 +61,16 @@ fn generate_languages(out_dir: &OsStr) {
}
}

fn compare_json_str_len(a: &Value, b: &Value) -> ::std::cmp::Ordering {
let a = a.as_array().expect("a as array");
let b = b.as_array().expect("b as array");

let max_a_size = a.iter().map(|e| e.as_str().unwrap().len()).max().unwrap();
let max_b_size = b.iter().map(|e| e.as_str().unwrap().len()).max().unwrap();

max_b_size.cmp(&max_a_size)
}

fn generate_tests(out_dir: &OsStr) {
use std::io::Write;

Expand Down
2 changes: 1 addition & 1 deletion src/language/languages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn count_files((name, ref mut language): (&LanguageType, &mut Language)) {
trace!("{}", line);

if ((!stack.is_empty() || ended_with_comments) && !had_code) ||
starts_with_comment
(starts_with_comment && quote.is_none())
{
stats.comments += 1;
trace!("Determined to be comment. So far: {} lines", stats.comments);
Expand Down

0 comments on commit 553736f

Please sign in to comment.