Skip to content
Open
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
75 changes: 75 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ path = "src/main.rs"
[dependencies]
regex = "1.10.2"
serde_json = "1.0.111"
toml = "0.8.12"
walkdir = "2.4.0"
38 changes: 23 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::fs::{read_to_string, File};
use std::io::{self, BufRead};
use std::path::Path;
use std::path::{Path, PathBuf};
use toml::Table;
use walkdir::WalkDir;

fn is_crate_directory(crate_dir: &Path) -> bool {
crate_dir.join("Cargo.toml").exists()
}

fn get_toml_path(crate_dir: &Path) -> PathBuf {
crate_dir.join("Cargo.toml")
}

fn main() -> io::Result<()> {
let patterns = [
("panic", Regex::new(r"panic!\(").unwrap(), "🚨"),
Expand Down Expand Up @@ -55,13 +60,22 @@ fn main() -> io::Result<()> {
let crate_path = entry.path();

if is_crate_directory(crate_path) {
let crate_name = crate_path
.canonicalize()
.unwrap_or_default()
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default()
.to_string();
let toml_path = get_toml_path(crate_path);
let toml_content = read_to_string(toml_path).unwrap_or_default();
let toml_parsed = toml_content.parse::<Table>().unwrap_or_default();

// check if relevant toml keys exist. panics if keys do not exist and access is attempted
if !toml_parsed.contains_key("package") {
continue;
}
if !match toml_parsed["package"].as_table() {
Some(x) => x.contains_key("name"),
None => continue
} {
continue;
}

let crate_name = toml_parsed["package"]["name"].as_str().unwrap_or_default().to_string();

if crate_name.is_empty()
|| crate_name == exclude_crate_name
Expand All @@ -72,12 +86,6 @@ fn main() -> io::Result<()> {

let mut pattern_counts: HashMap<&str, (usize, String)> = HashMap::new();

let crate_name = crate_path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default()
.to_string();

for entry in WalkDir::new(crate_path).into_iter().filter_map(|e| e.ok()) {
if entry.file_type().is_file() && entry.path().extension().map_or(false, |ext| ext == "rs")
{
Expand Down