Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions src/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,85 +51,58 @@ where

impl Exit for PythonCode {
fn compute(node: &Node, stats: &mut Stats) {
match node.kind_id().into() {
Python::ReturnStatement => {
stats.exit += 1;
}
_ => {}
if let Python::ReturnStatement = node.kind_id().into() {
stats.exit += 1;
}
}
}

impl Exit for MozjsCode {
fn compute(node: &Node, stats: &mut Stats) {
match node.kind_id().into() {
Mozjs::ReturnStatement => {
stats.exit += 1;
}
_ => {}
if let Mozjs::ReturnStatement = node.kind_id().into() {
stats.exit += 1;
}
}
}

impl Exit for JavascriptCode {
fn compute(node: &Node, stats: &mut Stats) {
match node.kind_id().into() {
Javascript::ReturnStatement => {
stats.exit += 1;
}
_ => {}
if let Javascript::ReturnStatement = node.kind_id().into() {
stats.exit += 1;
}
}
}

impl Exit for TypescriptCode {
fn compute(node: &Node, stats: &mut Stats) {
match node.kind_id().into() {
Typescript::ReturnStatement => {
stats.exit += 1;
}
_ => {}
if let Typescript::ReturnStatement = node.kind_id().into() {
stats.exit += 1;
}
}
}

impl Exit for TsxCode {
fn compute(node: &Node, stats: &mut Stats) {
match node.kind_id().into() {
Tsx::ReturnStatement => {
stats.exit += 1;
}
_ => {}
if let Tsx::ReturnStatement = node.kind_id().into() {
stats.exit += 1;
}
}
}

impl Exit for RustCode {
fn compute(node: &Node, stats: &mut Stats) {
use Rust::*;

match node.kind_id().into() {
ReturnExpression => {
stats.exit += 1;
}
_ => {
if Self::is_func(node) {
if let Some(_) = node.child_by_field_name("return_type") {
stats.exit += 1;
}
}
}
if let Rust::ReturnExpression = node.kind_id().into() {
stats.exit += 1;
} else if Self::is_func(node) && node.child_by_field_name("return_type").is_some() {
stats.exit += 1;
}
}
}

impl Exit for CppCode {
fn compute(node: &Node, stats: &mut Stats) {
match node.kind_id().into() {
Cpp::ReturnStatement => {
stats.exit += 1;
}
_ => {}
if let Cpp::ReturnStatement = node.kind_id().into() {
stats.exit += 1;
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ fn consumer(receiver: JobReceiver) {
}
}

fn send_file(path: PathBuf, cfg: &Arc<Config>, language: &Option<LANG>, sender: &JobSender) {
fn send_file(path: PathBuf, cfg: &Arc<Config>, language: Option<LANG>, sender: &JobSender) {
sender
.send(Some(JobItem {
language: language.clone(),
language,
path,
cfg: Arc::clone(cfg),
}))
Expand Down Expand Up @@ -203,14 +203,14 @@ fn explore(
};
}

send_file(path, &cfg, &language, &sender);
send_file(path, &cfg, language, &sender);
}
}
} else if (include.is_empty() || include.is_match(&path))
&& (exclude.is_empty() || !exclude.is_match(&path))
&& path.is_file()
{
send_file(path, &cfg, &language, &sender);
send_file(path, &cfg, language, &sender);
}
}

Expand Down Expand Up @@ -393,7 +393,7 @@ fn main() {
}

let paths: Vec<_> = matches.values_of("paths").unwrap().collect();
let paths: Vec<String> = paths.iter().map(|x| x.to_string()).collect();
let paths: Vec<String> = paths.iter().map(|x| (*x).to_string()).collect();
let dump = matches.is_present("dump");
let function = matches.is_present("function");
let in_place = matches.is_present("in_place");
Expand Down Expand Up @@ -488,7 +488,6 @@ fn main() {

let producer = {
let sender = sender.clone();
let cfg = cfg.clone();
let include = mk_globset(matches.values_of("include").unwrap());
let exclude = mk_globset(matches.values_of("exclude").unwrap());

Expand Down Expand Up @@ -543,7 +542,7 @@ fn main() {
println!("{}", data);
} else {
let output = PathBuf::from(output);
write_file(&output, data.to_string().as_bytes()).unwrap();
write_file(&output, data.as_bytes()).unwrap();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,12 @@ impl<'a> FuncSpace<'a> {
file.push(".json");

let mut json_path = output_path.clone();
json_path.push(file.clone());
json_path.push(file);

if json_path.as_path().exists() {
let mut new_filename = path.to_str().unwrap().to_string();
let re = Regex::new(r"[\\:/]").unwrap();
new_filename = re.replace_all(&mut new_filename, "_").to_string();
new_filename = re.replace_all(&new_filename, "_").to_string();
new_filename.push_str(".json");
json_path.pop();
json_path.push(new_filename);
Expand Down
2 changes: 1 addition & 1 deletion src/preproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl PreprocFile {
pub fn new_macros(macros: &[&str]) -> Self {
let mut pf = Self::default();
for m in macros {
pf.macros.insert(m.to_string());
pf.macros.insert((*m).to_string());
}
pf
}
Expand Down
46 changes: 25 additions & 21 deletions src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::languages::fake;
use crate::languages::*;
use regex::bytes::Regex;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fs::File;
use std::io::{Read, Write};
Expand Down Expand Up @@ -65,13 +66,10 @@ pub fn get_language_for_file(path: &PathBuf) -> Option<LANG> {
}

fn mode_to_str(mode: &[u8]) -> Option<String> {
std::str::from_utf8(mode)
.ok()
.map(|m| m.to_lowercase())
.map(|m| m.to_string())
std::str::from_utf8(mode).ok().map(|m| m.to_lowercase())
}

fn get_emacs_mode<'a>(buf: &'a [u8]) -> Option<String> {
fn get_emacs_mode(buf: &[u8]) -> Option<String> {
// we just try to use the emacs info (if there)
lazy_static! {
// comment containing coding info are useful
Expand Down Expand Up @@ -111,10 +109,11 @@ pub fn guess_language<P: AsRef<Path>>(buf: &[u8], path: P) -> (Option<LANG>, Str
.extension()
.map(|e| e.to_str().unwrap())
.map(|e| e.to_lowercase())
.unwrap_or("".to_string());
.unwrap_or_else(|| "".to_string());
let from_ext = get_from_ext(&ext);

let mode = get_emacs_mode(buf).unwrap_or("".to_string());
let mode = get_emacs_mode(buf).unwrap_or_else(|| "".to_string());

let from_mode = get_from_emacs_mode(&mode);

if let Some(lang_ext) = from_ext {
Expand All @@ -134,15 +133,16 @@ pub fn guess_language<P: AsRef<Path>>(buf: &[u8], path: P) -> (Option<LANG>, Str
fake::get_true(&ext, &mode).unwrap_or_else(|| lang_ext.get_name().to_string()),
)
}
} else if let Some(lang_mode) = from_mode {
(
Some(lang_mode),
fake::get_true(&ext, &mode).unwrap_or_else(|| lang_mode.get_name().to_string()),
)
} else {
if let Some(lang_mode) = from_mode {
(
Some(lang_mode),
fake::get_true(&ext, &mode).unwrap_or_else(|| lang_mode.get_name().to_string()),
)
} else {
(None, fake::get_true(&ext, &mode).unwrap_or("".to_string()))
}
(
None,
fake::get_true(&ext, &mode).unwrap_or_else(|| "".to_string()),
)
}
}

Expand Down Expand Up @@ -236,12 +236,16 @@ pub fn guess_file(
continue;
}
if let Some(dist) = get_paths_dist(current_path, &p) {
if dist < dist_min {
dist_min = dist;
path_min.clear();
path_min.push(p);
} else if dist == dist_min {
path_min.push(p)
match dist.cmp(&dist_min) {
Ordering::Less => {
dist_min = dist;
path_min.clear();
path_min.push(p);
}
Ordering::Equal => {
path_min.push(p);
}
Ordering::Greater => {}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,9 @@ mod tests {
// Inspired from https://hg.mozilla.org/mozilla-central/file/9b2a99adc05e53cd4010de512f50118594756650/extensions/java/xpcom/tests/testparams/TestParams.java#l64.
#[actix_rt::test]
async fn test_web_comment_plain_bad_chars() {
let bad_bytes = &[142, 137, 138, 136, 140, 141, 10];
let input_vec = ["/*char*/s: ".as_bytes(), bad_bytes].concat();
let output_vec = ["s: ".as_bytes(), bad_bytes].concat();
let bad_bytes: &[u8] = &[142, 137, 138, 136, 140, 141, 10];
let input_vec = [b"/*char*/s: ", bad_bytes].concat();
let output_vec = [b"s: ", bad_bytes].concat();

let mut app = test::init_service(
App::new()
Expand Down