use std::collections::BTreeMap; #[derive(Clone, Debug, PartialEq, Eq)] pub struct LangShare { pub name: String, pub files: u64, pub bytes: u64, pub percent: u32, pub others: Vec, } const EXTENSIONS: &[(&str, &str)] = &[ ("rs", "Rust"), ("js", "JavaScript"), ("mjs", "JavaScript"), ("cjs", "JavaScript"), ("jsx", "JavaScript"), ("ts", "TypeScript"), ("mts", "TypeScript"), ("cts", "TypeScript"), ("tsx", "TypeScript"), ("py", "Python"), ("pyi", "Python"), ("pyw", "Python"), ("go", "Go"), ("c", "C"), ("h", "C"), ("cc", "C++"), ("cpp", "C++"), ("cxx", "C++"), ("c++", "C++"), ("hpp", "C++"), ("hh", "C++"), ("hxx", "C++"), ("cs", "C#"), ("java", "Java"), ("kt", "Kotlin"), ("kts", "Kotlin"), ("swift", "Swift"), ("rb", "Ruby"), ("php", "PHP"), ("sh", "Shell"), ("bash", "Shell"), ("zsh", "Shell"), ("fish", "Shell"), ("lua", "Lua"), ("zig", "Zig"), ("nim", "Nim"), ("hs", "Haskell"), ("ml", "OCaml"), ("mli", "OCaml"), ("ex", "Elixir"), ("exs", "Elixir"), ("erl", "Erlang"), ("hrl", "Erlang"), ("clj", "Clojure"), ("cljs", "Clojure"), ("cljc", "Clojure"), ("scala", "Scala"), ("dart", "Dart"), ("r", "R"), ("jl", "Julia"), ("sql", "SQL"), ("html", "HTML"), ("htm", "HTML"), ("css", "CSS"), ("scss", "SCSS"), ("sass", "Sass"), ("less", "Less"), ("vue", "Vue"), ("svelte", "Svelte"), ("nix", "Nix"), ("dockerfile", "Dockerfile"), ("mk", "Makefile"), ("cmake", "CMake"), ("glsl", "GLSL"), ("vert", "GLSL"), ("frag", "GLSL"), ("comp", "GLSL"), ("wgsl", "WGSL"), ("hlsl", "HLSL"), ("hlsli", "HLSL"), ("s", "Assembly"), ("asm", "Assembly"), ("pl", "Perl"), ("pm", "Perl"), ("ps1", "PowerShell"), ("psm1", "PowerShell"), ("vim", "Vim script"), ("el", "Emacs Lisp"), ("f77", "Fortran"), ("f90", "Fortran"), ("f95", "Fortran"), ("f03", "Fortran"), ("f08", "Fortran"), ("d", "D"), ("v", "V"), ("gleam", "Gleam"), ("odin", "Odin"), ]; const SKIPPED_SEGMENTS: &[&str] = &[ "vendor", "node_modules", "third_party", "dist", "target", ".git", ]; fn language_of(path: &str) -> Option<&'static str> { if path.split('/').any(|seg| SKIPPED_SEGMENTS.contains(&seg)) { return None; } let name = path.rsplit('/').next().unwrap_or(path).to_ascii_lowercase(); if name.ends_with(".min.js") || name.ends_with(".min.css") || name.ends_with(".lock") || name == "package-lock.json" || name == "pnpm-lock.yaml" || name.contains(".generated.") || name.ends_with(".pb.go") { return None; } match name.as_str() { "dockerfile" => return Some("Dockerfile"), "makefile" | "gnumakefile" => return Some("Makefile"), "justfile" => return Some("Just"), "cmakelists.txt" => return Some("CMake"), _ => {} } let (_, ext) = name.rsplit_once('.')?; EXTENSIONS .iter() .find(|(e, _)| *e == ext) .map(|(_, lang)| *lang) } pub fn languages(files: &[(String, u64)], top: usize) -> Vec { let mut by_lang: BTreeMap<&'static str, (u64, u64)> = BTreeMap::new(); for (path, size) in files { if let Some(lang) = language_of(path) { let entry = by_lang.entry(lang).or_default(); entry.0 += 1; entry.1 += size; } } let mut rows: Vec = by_lang .into_iter() .map(|(name, (files, bytes))| LangShare { name: name.to_string(), files, bytes, percent: 0, others: Vec::new(), }) .collect(); rows.sort_by_key(|a| std::cmp::Reverse(a.bytes)); if rows.len() > top { let rest = rows.split_off(top); rows.push(LangShare { name: "Other".to_string(), files: rest.iter().map(|r| r.files).sum(), bytes: rest.iter().map(|r| r.bytes).sum(), percent: 0, others: rest.into_iter().map(|r| r.name).collect(), }); } let total: u64 = rows.iter().map(|r| r.bytes).sum(); assign_percents(&mut rows, total); rows } fn assign_percents(rows: &mut [LangShare], total: u64) { if total == 0 { return; } let total = u128::from(total); let mut remainders: Vec<(usize, u128)> = Vec::with_capacity(rows.len()); let mut floor_sum = 0u32; for (i, row) in rows.iter_mut().enumerate() { let scaled = u128::from(row.bytes) * 100; row.percent = u32::try_from(scaled / total).unwrap_or(100); floor_sum += row.percent; remainders.push((i, scaled % total)); } remainders.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0))); let missing = 100u32.saturating_sub(floor_sum) as usize; for (i, _) in remainders.into_iter().take(missing) { rows[i].percent += 1; } } pub fn license_id(text: &str) -> Option<&'static str> { let t = normalise(text); if t.contains("permission is hereby granted, free of charge") { return Some("MIT"); } if t.contains("this is free and unencumbered software released into the public domain") { return Some("Unlicense"); } if t.contains("boost software license") { return Some("BSL-1.0"); } if t.contains("apache license") && t.contains("version 2.0") { return Some("Apache-2.0"); } if t.contains("mozilla public license") && t.contains("2.0") { return Some("MPL-2.0"); } if t.contains("permission to use, copy, modify, and/or distribute this software for any purpose with or without fee") { return Some(if t.contains("provided that the above copyright notice") { "ISC" } else { "0BSD" }); } if t.contains("redistribution and use in source and binary forms") { return Some(if t.contains("neither the name") { "BSD-3-Clause" } else { "BSD-2-Clause" }); } copyleft_id(&t) } fn copyleft_id(t: &str) -> Option<&'static str> { const TITLES: [(&str, &str); 3] = [ ("gnu affero general public license", "AGPL"), ("gnu lesser general public license", "LGPL"), ("gnu general public license", "GPL"), ]; let (pos, title, family) = TITLES .iter() .filter_map(|(title, family)| t.find(title).map(|pos| (pos, *title, *family))) .min_by_key(|(pos, _, _)| *pos)?; let after: String = t[pos + title.len()..].chars().take(40).collect(); let version = if after.contains("version 2.1") { "2.1" } else if after.contains("version 3") { "3" } else if after.contains("version 2") { "2" } else { "" }; match (family, version) { ("AGPL", "3") => Some("AGPL-3.0"), ("LGPL", "3") => Some("LGPL-3.0"), ("LGPL", "2.1") => Some("LGPL-2.1"), ("GPL", "3") => Some("GPL-3.0"), ("GPL", "2") => Some("GPL-2.0"), _ => None, } } fn normalise(text: &str) -> String { text.split_whitespace() .collect::>() .join(" ") .to_lowercase() } const LICENSE_NAMES: &[&str] = &[ "license", "license.md", "license.txt", "license.rst", "licence", "licence.md", "licence.txt", "copying", "copying.md", "copying.txt", "license-mit", "license-apache", "license-apache-2.0", "license-bsd", "license-gpl", ]; pub fn license_file(root_names: &[String]) -> Option<&str> { LICENSE_NAMES.iter().find_map(|want| { root_names .iter() .find(|name| name.eq_ignore_ascii_case(want)) .map(String::as_str) }) } #[cfg(test)] mod tests { use super::*; #[test] fn languages_skip_vendored_and_fold_the_rest() { let files: Vec<(String, u64)> = [ ("src/main.rs", 1000), ("src/lib.rs", 500), ("web/app.ts", 300), ("web/view.tsx", 200), ("scripts/run.py", 100), ("README.md", 9999), ("vendor/crate/lib.rs", 50000), ("node_modules/x/index.js", 50000), ("web/app.min.js", 40000), ("Dockerfile", 60), ] .iter() .map(|(p, s)| (p.to_string(), *s)) .collect(); let shares = languages(&files, 1); assert_eq!(shares.len(), 2); assert_eq!(shares[0].name, "Rust"); assert_eq!((shares[0].files, shares[0].bytes), (2, 1500)); assert_eq!(shares[1].name, "Other"); assert_eq!((shares[1].files, shares[1].bytes), (4, 660)); assert_eq!(shares[1].others, ["TypeScript", "Python", "Dockerfile"]); assert_eq!(shares.iter().map(|s| s.percent).sum::(), 100); } #[test] fn license_id_matches_mit_and_agpl() { let mit = "MIT License\n\nCopyright (c) 2026 Someone\n\nPermission is hereby granted, free of\ncharge, to any person obtaining a copy"; assert_eq!(license_id(mit), Some("MIT")); let agpl = " GNU AFFERO GENERAL PUBLIC LICENSE\n Version 3, 19 November 2007\n\n ... version 3 of the GNU General Public License ..."; assert_eq!(license_id(agpl), Some("AGPL-3.0")); } #[test] fn license_file_prefers_license_over_copying() { let names: Vec = ["README.md", "COPYING", "license.txt"] .iter() .map(|s| s.to_string()) .collect(); assert_eq!(license_file(&names), Some("license.txt")); let names: Vec = ["src", "LICENSE.md", "LICENSE"] .iter() .map(|s| s.to_string()) .collect(); assert_eq!(license_file(&names), Some("LICENSE")); } }