Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelcmtd committed Sep 26, 2021
1 parent 8ef1e85 commit 213def6
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
// TODO: multi-threading

use ignore::Walk;
use json;
use lazy_static::lazy_static;
use std::env;
use std::fs::read_to_string;
use std::ffi::OsStr;
use std::fs::read_to_string;
use std::path::Path;
use ignore::Walk;
use lazy_static::lazy_static;

type JVal = json::JsonValue;

lazy_static! {
static ref LANGS: JVal = json::parse(include_str!("langs.json")).unwrap();
}

fn derive_type(file: &Path, contents: &String, by_ext: &JVal, by_file: &JVal, by_shebang: &JVal) -> Option<String> {
fn derive_type(
file: &Path,
contents: &String,
by_ext: &JVal,
by_file: &JVal,
by_shebang: &JVal,
) -> Option<String> {
if let Some(ty) = file.extension().and_then(OsStr::to_str).map(|e| &by_ext[e]) {
return ty.as_str().map(String::from);
} else if let Some(ty) = Path::file_name(file).and_then(|e| e.to_str()).map(|e| &by_file[e]) {
} else if let Some(ty) = Path::file_name(file)
.and_then(|e| e.to_str())
.map(|e| &by_file[e])
{
return ty.as_str().map(String::from);
} else if let Some(ty) = contents.split("\n").next().and_then(|e| e.split(" ").next().and_then(|e| e.split("/").last().map(|e| &by_shebang[e]))) {
} else if let Some(ty) = contents.split("\n").next().and_then(|e| {
e.split(" ")
.next()
.and_then(|e| e.split("/").last().map(|e| &by_shebang[e]))
}) {
return ty.as_str().map(String::from);
}
return None;
Expand Down Expand Up @@ -56,22 +69,27 @@ fn main() {
let by_ext = &LANGS["by_ext"];
let by_file = &LANGS["by_file"];
let by_shebang = &LANGS["by_shebang"];
let raw_files = Walk::new(".").filter_map(Result::ok).filter(|e| e.file_type().unwrap().is_file());
let files: Vec<(String, usize, String)> = raw_files.map(|raw_file| {
let contents = match read_to_string(raw_file.path()) {
Ok(s) => s,
Err(_) => return None,
};
let path = String::from(raw_file.path().to_str().unwrap());
// TODO: make sense
let len = contents.lines().count();
if let Some(ty) = derive_type(raw_file.path(), &contents, by_ext, by_file, by_shebang) {
return Some((path, len, ty));
}
return None;
}).filter(|e| e.is_some()).map(|e| e.unwrap()).collect();
println!("{:?}", files);
return;
let raw_files = Walk::new(".")
.filter_map(Result::ok)
.filter(|e| e.file_type().unwrap().is_file());
let files: Vec<(String, usize, String)> = raw_files
.map(|raw_file| {
let contents = match read_to_string(raw_file.path()) {
Ok(s) => s,
Err(_) => return None,
};
let path = String::from(raw_file.path().to_str().unwrap());
// TODO: make sense
let len = contents.lines().count();
if let Some(ty) = derive_type(raw_file.path(), &contents, by_ext, by_file, by_shebang) {
return Some((path, len, ty));
}
return None;
})
.filter(|e| e.is_some())
.map(|e| e.unwrap())
.collect();
json = true;
if json {
// TODO: "header"
// TODO: cloc compatibility
Expand Down

0 comments on commit 213def6

Please sign in to comment.