pub fn normalize_prefix(raw: &str) -> String {
raw.trim().replace('\\', "/").trim_matches('/').to_string()
}
fn prefix_covers(prefix: &str, path: &str) -> bool {
if prefix.is_empty() {
return false;
}
path == prefix
|| (path.len() > prefix.len()
&& path.as_bytes()[prefix.len()] == b'/'
&& path.starts_with(prefix))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SparseCone {
prefixes: Vec<String>,
}
impl SparseCone {
pub fn new<I, S>(prefixes: I) -> Option<SparseCone>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut prefixes: Vec<String> = prefixes
.into_iter()
.map(|p| normalize_prefix(p.as_ref()))
.filter(|p| !p.is_empty())
.collect();
prefixes.sort();
prefixes.dedup();
if prefixes.is_empty() {
None
} else {
Some(SparseCone { prefixes })
}
}
pub fn from_metadata(raw: Option<&str>) -> Option<SparseCone> {
SparseCone::new(raw?.lines())
}
pub fn to_metadata(&self) -> String {
self.prefixes.join("\n")
}
pub fn prefixes(&self) -> &[String] {
&self.prefixes
}
pub fn covers(&self, path: &str) -> bool {
let path = normalize_prefix(path);
self.prefixes.iter().any(|p| prefix_covers(p, &path))
}
pub fn intersects(&self, prefix: &str) -> bool {
let prefix = normalize_prefix(prefix);
prefix.is_empty()
|| self.covers(&prefix)
|| self.prefixes.iter().any(|p| prefix_covers(&prefix, p))
}
pub fn with_added<I, S>(&self, additions: I) -> SparseCone
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let merged = self
.prefixes
.iter()
.cloned()
.chain(additions.into_iter().map(|p| normalize_prefix(p.as_ref())))
.filter(|p| !p.is_empty());
SparseCone::new(merged).expect("union with a non-empty cone is non-empty")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalize_strips_and_unifies_separators() {
assert_eq!(normalize_prefix("/src/"), "src");
assert_eq!(normalize_prefix("src"), "src");
assert_eq!(normalize_prefix(" src/app "), "src/app");
assert_eq!(normalize_prefix("src\\app"), "src/app");
assert_eq!(normalize_prefix("///"), "");
assert_eq!(normalize_prefix(""), "");
}
#[test]
fn new_drops_blanks_and_dedups() {
let cone = SparseCone::new(["src", "", " ", "/src/", "libs"]).unwrap();
assert_eq!(cone.prefixes(), &["libs".to_string(), "src".to_string()]);
}
#[test]
fn new_returns_none_when_everything_blank() {
assert!(SparseCone::new(["", " ", "///"]).is_none());
assert!(SparseCone::new(Vec::<String>::new()).is_none());
}
#[test]
fn covers_respects_path_boundaries() {
let cone = SparseCone::new(["src", "docs/api"]).unwrap();
assert!(cone.covers("src"));
assert!(cone.covers("src/main.rs"));
assert!(cone.covers("src/deep/nested/x.rs"));
assert!(cone.covers("docs/api/index.md"));
assert!(!cone.covers("src2/x.rs"));
assert!(!cone.covers("srcfoo"));
assert!(!cone.covers("docs/guide.md"));
assert!(!cone.covers("README.md"));
assert!(!cone.covers("Cargo.toml"));
}
#[test]
fn covers_normalizes_the_queried_path() {
let cone = SparseCone::new(["src"]).unwrap();
assert!(cone.covers("/src/main.rs"));
assert!(cone.covers("src\\main.rs"));
}
#[test]
fn intersects_covers_ancestors_and_descendants() {
let cone = SparseCone::new(["src/app"]).unwrap();
assert!(cone.intersects(""));
assert!(cone.intersects("src"));
assert!(cone.intersects("src/app"));
assert!(cone.intersects("src/app/main.rs"));
assert!(!cone.intersects("src/lib"));
assert!(!cone.intersects("docs"));
assert!(!cone.intersects("src/ap"));
}
#[test]
fn metadata_round_trips() {
let cone = SparseCone::new(["libs/shared", "src"]).unwrap();
let raw = cone.to_metadata();
assert_eq!(raw, "libs/shared\nsrc");
let parsed = SparseCone::from_metadata(Some(&raw)).unwrap();
assert_eq!(parsed, cone);
}
#[test]
fn from_metadata_absent_or_blank_is_full_checkout() {
assert!(SparseCone::from_metadata(None).is_none());
assert!(SparseCone::from_metadata(Some("")).is_none());
assert!(SparseCone::from_metadata(Some("\n \n")).is_none());
}
#[test]
fn with_added_unions_and_dedups() {
let cone = SparseCone::new(["src"]).unwrap();
let widened = cone.with_added(["libs", "src"]);
assert_eq!(widened.prefixes(), &["libs".to_string(), "src".to_string()]);
}
}