use std::collections::HashSet;
use std::ffi::OsStr;
use std::path::Path;
pub const DEFAULT_EXTENSIONS: &[&str] = &[
"c", "cc", "cpp", "cxx", "c++", "h", "hh", "hpp", "hxx", "h++", "inl", "ipp", "tpp", "ixx", "m",
"mm", "cs", "csx", "java", "kt", "kts", "rs", "go", "py", "pyi", "rb", "php", "pl", "pm", "lua", "js", "jsx", "ts", "tsx", "mjs",
"cjs", "sh", "bash", "zsh", "ps1", "bat", "cmd", "json", "jsonc", "yaml", "yml", "toml", "ini", "cfg", "conf", "xml", "csv", "tsv", "sql",
"md", "markdown", "txt", "rst", "proto",
];
pub const RC_EXTENSIONS: &[&str] = &["rc", "rc2", "dlg"];
pub const MSBUILD_EXTENSIONS: &[&str] = &[
"sln", "vcxproj", "vcproj", "props", "targets", "filters", "user",
];
pub const DEFAULT_EXCLUDE_DIRS: &[&str] = &[
"dep",
"deps",
"third_party",
"third-party",
"3rdparty",
"vendor",
"external",
"extern",
"_tools",
"tools_bin",
".svn",
".git",
".hg",
".vs",
".idea",
".vscode",
"build",
"out",
"bin",
"obj",
"debug",
"release",
"target",
"node_modules",
"__pycache__",
".venv",
"venv",
"temp",
"tmp",
"cache",
];
pub const DEFAULT_EXCLUDE_PREFIXES: &[&str] = &["outputfile"];
#[derive(Debug, Clone, Default)]
pub struct FilterOptions {
pub ext_override: Vec<String>,
pub exclude_ext: Vec<String>,
pub exclude_dirs: Vec<String>,
pub exclude_globs: Vec<String>,
pub include_rc: bool,
pub include_msbuild: bool,
pub include_hidden: bool,
}
#[derive(Debug, Clone)]
pub struct FilterConfig {
pub extensions: HashSet<String>,
pub exclude_dirs: HashSet<String>,
pub exclude_prefixes: Vec<String>,
pub exclude_globs: Vec<Glob>,
pub include_hidden: bool,
}
impl FilterConfig {
pub fn build(opts: &FilterOptions) -> Result<FilterConfig, String> {
let exclude_ext: HashSet<String> = normalize_ext_list(&opts.exclude_ext)?;
if opts.include_rc && RC_EXTENSIONS.iter().any(|e| exclude_ext.contains(*e)) {
return Err("--include-rc conflicts with --exclude-ext (rc/rc2/dlg)".to_string());
}
if opts.include_msbuild && MSBUILD_EXTENSIONS.iter().any(|e| exclude_ext.contains(*e)) {
return Err(concat!(
"--include-msbuild conflicts with --exclude-ext ",
"(sln/vcxproj/vcproj/props/targets/filters/user)"
)
.to_string());
}
let mut extensions: HashSet<String> = if !opts.ext_override.is_empty() {
normalize_ext_list(&opts.ext_override)?
} else {
let mut base: HashSet<String> = DEFAULT_EXTENSIONS
.iter()
.map(|s| (*s).to_string())
.collect();
if opts.include_rc {
base.extend(RC_EXTENSIONS.iter().map(|s| (*s).to_string()));
}
if opts.include_msbuild {
base.extend(MSBUILD_EXTENSIONS.iter().map(|s| (*s).to_string()));
}
base
};
for e in &exclude_ext {
extensions.remove(e);
}
let mut exclude_dirs: HashSet<String> = DEFAULT_EXCLUDE_DIRS
.iter()
.map(|s| (*s).to_string())
.collect();
exclude_dirs.extend(normalize_dir_list(&opts.exclude_dirs));
let exclude_prefixes: Vec<String> = DEFAULT_EXCLUDE_PREFIXES
.iter()
.map(|s| (*s).to_string())
.collect();
let mut exclude_globs = Vec::new();
for pattern in &opts.exclude_globs {
match Glob::compile(pattern) {
Ok(g) => exclude_globs.push(g),
Err(e) => return Err(format!("invalid --exclude-glob `{pattern}`: {e}")),
}
}
Ok(FilterConfig {
extensions,
exclude_dirs,
exclude_prefixes,
exclude_globs,
include_hidden: opts.include_hidden,
})
}
pub fn is_excluded_dir_name(&self, name: &OsStr) -> bool {
let n = name.to_string_lossy().to_ascii_lowercase();
self.exclude_dirs.contains(&n) || self.exclude_prefixes.iter().any(|p| n.starts_with(p))
}
pub fn is_hidden_name(&self, name: &OsStr) -> bool {
!self.include_hidden && name.to_string_lossy().starts_with('.')
}
pub fn is_excluded_by_glob(&self, rel_components: &[&str]) -> bool {
self.exclude_globs.iter().any(|g| g.matches(rel_components))
}
}
fn normalize_ext_list(values: &[String]) -> Result<HashSet<String>, String> {
let mut out = HashSet::new();
for raw in values {
let v = raw.trim();
let v = v.strip_prefix('.').unwrap_or(v).to_ascii_lowercase();
if v.starts_with('.') {
return Err(format!(
"invalid extension value `{raw}`: too many leading dots"
));
}
if v.contains('/') {
return Err(format!("invalid extension value `{raw}`: contains `/`"));
}
if !v.is_empty() {
out.insert(v);
}
}
Ok(out)
}
fn normalize_dir_list(values: &[String]) -> HashSet<String> {
values
.iter()
.map(|s| s.trim().to_ascii_lowercase())
.filter(|s| !s.is_empty())
.collect()
}
pub fn extension_of(path: &Path) -> Option<String> {
path.extension()
.map(|e| e.to_string_lossy().to_ascii_lowercase())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Glob {
parts: Vec<String>,
}
const GLOB_STEP_BUDGET: u64 = 1_000_000;
impl Glob {
pub fn compile(pattern: &str) -> Result<Glob, String> {
Glob::compile_inner(pattern, true)
}
pub fn compile_raw(pattern: &str) -> Result<Glob, String> {
Glob::compile_inner(pattern, false)
}
fn compile_inner(pattern: &str, normalize_backslash: bool) -> Result<Glob, String> {
let normalized = if normalize_backslash {
pattern.replace('\\', "/")
} else {
pattern.to_string()
};
let parts: Vec<String> = normalized
.split('/')
.filter(|s| !s.is_empty())
.map(|s| collapse_stars(&s.to_ascii_lowercase()))
.collect();
if parts.is_empty() {
return Err("empty pattern".to_string());
}
Ok(Glob { parts })
}
pub fn matches(&self, components: &[&str]) -> bool {
let lowered: Vec<String> = components.iter().map(|c| c.to_ascii_lowercase()).collect();
let refs: Vec<&str> = lowered.iter().map(String::as_str).collect();
let mut budget = GLOB_STEP_BUDGET;
match_parts(&self.parts, &refs, &mut budget)
}
}
fn collapse_stars(component: &str) -> String {
if component.chars().all(|c| c == '*') && component.len() >= 2 {
return "**".to_string();
}
let mut out = String::with_capacity(component.len());
let mut last_was_star = false;
for c in component.chars() {
if c == '*' {
if !last_was_star {
out.push(c);
}
} else {
out.push(c);
}
last_was_star = c == '*';
}
out
}
fn match_parts(pat: &[String], path: &[&str], budget: &mut u64) -> bool {
if *budget == 0 {
return false;
}
*budget -= 1;
match pat.first() {
None => path.is_empty(),
Some(first) if first == "**" => {
for i in 0..=path.len() {
if match_parts(&pat[1..], &path[i..], budget) {
return true;
}
}
false
}
Some(first) => match path.first() {
None => false,
Some(head) => {
match_component(first.as_str(), head, budget)
&& match_parts(&pat[1..], &path[1..], budget)
}
},
}
}
fn match_component(pat: &str, s: &str, budget: &mut u64) -> bool {
let p: Vec<char> = pat.chars().collect();
let t: Vec<char> = s.chars().collect();
match_chars(&p, &t, budget)
}
fn match_chars(pat: &[char], text: &[char], budget: &mut u64) -> bool {
if *budget == 0 {
return false;
}
*budget -= 1;
match pat.first() {
None => text.is_empty(),
Some('*') => (0..=text.len()).any(|i| match_chars(&pat[1..], &text[i..], budget)),
Some('?') => !text.is_empty() && match_chars(&pat[1..], &text[1..], budget),
Some(c) => !text.is_empty() && text[0] == *c && match_chars(&pat[1..], &text[1..], budget),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn glob_double_star_matches_nested_and_root() {
let g = Glob::compile("**/Generated/**").unwrap();
assert!(g.matches(&["GameServer", "Generated", "a.cpp"]));
assert!(g.matches(&["Generated", "b.h"]));
assert!(!g.matches(&["GameServer", "a.cpp"]));
assert!(!g.matches(&["Generated.cpp"]));
}
#[test]
fn glob_is_case_insensitive_and_normalizes_slashes() {
let g = Glob::compile("**\\generated\\**").unwrap();
assert!(g.matches(&["GameServer", "GENERATED", "x.cpp"]));
}
#[test]
fn glob_component_wildcards() {
let g = Glob::compile("**/*.tmp.*").unwrap();
assert!(g.matches(&["a", "x.tmp.cpp"]));
assert!(!g.matches(&["a", "y.cpp"]));
let q = Glob::compile("??-gen").unwrap();
assert!(q.matches(&["ab-gen"]));
assert!(!q.matches(&["abc-gen"]));
}
#[test]
fn glob_empty_pattern_rejected() {
assert!(Glob::compile("").is_err());
assert!(Glob::compile("///").is_err());
}
#[test]
fn build_default_whitelist_contains_cpp_not_rc() {
let cfg = FilterConfig::build(&FilterOptions::default()).unwrap();
assert!(cfg.extensions.contains("cpp"));
assert!(cfg.extensions.contains("proto"));
assert!(!cfg.extensions.contains("rc"));
assert!(!cfg.extensions.contains("vcxproj"));
}
#[test]
fn build_opt_in_flags_add_their_sets() {
let cfg = FilterConfig::build(&FilterOptions {
include_rc: true,
include_msbuild: true,
..Default::default()
})
.unwrap();
assert!(cfg.extensions.contains("rc"));
assert!(cfg.extensions.contains("rc2"));
assert!(cfg.extensions.contains("dlg"));
assert!(cfg.extensions.contains("vcxproj"));
assert!(cfg.extensions.contains("sln"));
}
#[test]
fn build_ext_override_replaces_default_set() {
let cfg = FilterConfig::build(&FilterOptions {
ext_override: vec!["H".into(), "cpp".into()],
include_rc: true,
..Default::default()
})
.unwrap();
assert!(cfg.extensions.contains("h"));
assert!(cfg.extensions.contains("cpp"));
assert!(!cfg.extensions.contains("md"));
assert!(!cfg.extensions.contains("rc"));
}
#[test]
fn build_exclude_ext_removes_from_whitelist() {
let cfg = FilterConfig::build(&FilterOptions {
exclude_ext: vec!["CPP".into(), "".into()],
..Default::default()
})
.unwrap();
assert!(!cfg.extensions.contains("cpp"));
assert!(cfg.extensions.contains("h"));
}
#[test]
fn build_detects_documented_conflicts() {
let err = FilterConfig::build(&FilterOptions {
include_rc: true,
exclude_ext: vec!["rc".into()],
..Default::default()
})
.unwrap_err();
assert!(err.contains("--include-rc"));
let err = FilterConfig::build(&FilterOptions {
include_msbuild: true,
exclude_ext: vec!["vcxproj".into()],
..Default::default()
})
.unwrap_err();
assert!(err.contains("--include-msbuild"));
}
#[test]
fn build_rejects_bad_glob() {
let res = FilterConfig::build(&FilterOptions {
exclude_globs: vec!["".into()],
..Default::default()
});
assert!(res.is_err());
}
#[test]
fn excluded_dir_name_is_case_insensitive_with_prefix_rule() {
let cfg = FilterConfig::build(&FilterOptions {
exclude_dirs: vec!["GameServer201".into()],
..Default::default()
})
.unwrap();
assert!(cfg.is_excluded_dir_name(OsStr::new("dep")));
assert!(cfg.is_excluded_dir_name(OsStr::new("DEP")));
assert!(cfg.is_excluded_dir_name(OsStr::new(".svn")));
assert!(cfg.is_excluded_dir_name(OsStr::new("OutputFile1")));
assert!(cfg.is_excluded_dir_name(OsStr::new("OutputFiles")));
assert!(cfg.is_excluded_dir_name(OsStr::new("gameserver201")));
assert!(!cfg.is_excluded_dir_name(OsStr::new("src")));
assert!(!cfg.is_excluded_dir_name(OsStr::new("temp.md")));
}
#[test]
fn hidden_respects_include_flag() {
let cfg = FilterConfig::build(&FilterOptions::default()).unwrap();
assert!(cfg.is_hidden_name(OsStr::new(".hidden.cpp")));
assert!(!cfg.is_hidden_name(OsStr::new("a.cpp")));
let cfg = FilterConfig::build(&FilterOptions {
include_hidden: true,
..Default::default()
})
.unwrap();
assert!(!cfg.is_hidden_name(OsStr::new(".hidden.cpp")));
}
#[test]
fn extension_of_lowercases() {
assert_eq!(extension_of(Path::new("a.CPP")), Some("cpp".to_string()));
assert_eq!(extension_of(Path::new("a.tar.gz")), Some("gz".to_string()));
assert_eq!(extension_of(Path::new("Makefile")), None);
}
}