use std::error::Error;
use std::path::Path;
use globset::GlobSet;
const TASK_DIRS: &[&[&str]] = &[
&["mix", "tasks"],
&["management", "commands"],
&["bin"],
&["scripts"],
&["examples"],
&["benches"],
];
const PROGRAMS: &[&str] = &[
"src/main.rs",
"build.rs",
"__main__.py",
"setup.py",
"manage.py",
];
pub struct EntryPoints {
configured: GlobSet,
}
impl EntryPoints {
pub fn new(patterns: &[String]) -> Result<Self, Box<dyn Error>> {
Ok(Self {
configured: super::inert::globs(patterns, "[impact] entry_points")?,
})
}
pub fn conventional(&self, path: &Path) -> bool {
let dirs: Vec<&str> = path
.parent()
.into_iter()
.flat_map(|dir| dir.components())
.filter_map(|c| c.as_os_str().to_str())
.collect();
let in_tasks = TASK_DIRS
.iter()
.any(|task| dirs.windows(task.len()).any(|window| window == *task));
in_tasks || PROGRAMS.iter().any(|program| path.ends_with(program))
}
pub fn configured(&self, path: &Path) -> bool {
self.configured.is_match(path)
}
}
#[cfg(test)]
#[path = "entry_test.rs"]
mod tests;