use std::path::{Path, PathBuf};
use zoning::ordinance;
use zoning::survey::{Ask, Dialect, Survey};
use super::Tracked;
pub(super) fn scope(here: &Path, pinned: bool, under: &[String]) -> (PathBuf, Vec<PathBuf>) {
let here = here.canonicalize().unwrap_or_else(|_| here.to_path_buf());
let found = ordinance::discover(&here, under);
if pinned || !found.is_empty() {
return (here, found);
}
let ceiling = zoning::repo_root(&here);
for parent in here.ancestors().skip(1).take_while(|p| p.starts_with(&ceiling)) {
let contracts = ordinance::discover(parent, under);
if !contracts.is_empty() {
return (parent.to_path_buf(), contracts);
}
}
(here, Vec::new())
}
pub(super) fn basename(root: &Path, rel: &str) -> String {
let dir = if rel == "." { root.to_path_buf() } else { root.join(rel) };
dir.file_name().map_or_else(|| rel.to_owned(), |n| n.to_string_lossy().into_owned())
}
pub(super) fn tail(path: &Path) -> String {
if let Ok(here) = std::env::current_dir() {
if path == here {
return ".".to_owned();
}
if let Ok(rel) = path.strip_prefix(&here) {
return rel.display().to_string();
}
}
let mut last: Vec<_> = path.components().rev().take(2).collect();
last.reverse();
last.iter().map(|c| c.as_os_str().to_string_lossy()).collect::<Vec<_>>().join("/")
}
pub(super) fn declared(dir: &Path, dialect: &'static dyn Dialect) -> Option<String> {
dialect
.manifests()
.iter()
.filter_map(|m| std::fs::read_to_string(dir.join(m)).ok())
.find_map(|text| dialect.declared(&text))
.filter(|name| !name.is_empty())
}
pub(super) fn spoken(dir: &Path) -> Option<&'static dyn Dialect> {
zoning::survey::dialects()
.iter()
.copied()
.find(|d| d.manifests().iter().any(|m| dir.join(m).exists()))
}
pub(super) fn pathdiff(from: &Path, to: &Path) -> String {
if to == from {
return ".".to_owned();
}
match to.strip_prefix(from) {
Ok(rel) => rel.display().to_string(),
Err(_) if from.parent() == Some(to) => "..".to_owned(),
Err(_) => to.display().to_string(),
}
}
pub(super) fn barren(
dir: &Path,
source: &str,
dialect: &'static dyn Dialect,
nested: &[String],
) -> String {
let shown = tail(&dir.join(source));
let where_ = if shown == "." { "here".to_owned() } else { format!("under {shown}") };
let lang = dialect.name();
if !nested.is_empty() {
return format!(
"no {lang} source {where_} of its own — every file belongs to a nested package \
({}). Draft each of those instead",
nested.join(", ")
);
}
if dialect.manifests().iter().any(|m| dir.join(m).exists()) {
return format!(
"no {lang} source {where_}. A contract governs a module's imports, and a manifest \
is not a module: `{}` declares this package rather than belonging to it, so there \
is nothing left here to govern",
dialect.manifests().join("` / `")
);
}
let known: Vec<&str> = zoning::survey::dialects().iter().map(|d| d.name()).collect();
format!(
"no {lang} source {where_}. If the code is in another language, `--language NAME` \
reads it — this build knows {}",
known.join(", ")
)
}
pub(super) fn probe(
dir: &Path,
source: &str,
dialect: &'static dyn Dialect,
tracked: &mut Tracked<'_>,
nested: &[String],
package: &str,
) -> Survey {
let exclude: Vec<zoning::pattern::Pattern> =
nested.iter().map(|glob| zoning::pattern::Pattern::new(glob)).collect();
Survey::of(&Ask {
repo_root: dir,
module_root: &dir.join(source),
exclude: &exclude,
dialect,
package,
tracked: tracked.of(dialect),
})
}
pub(super) fn module(dir: &Path) -> (&'static str, Vec<String>) {
let source = if dir.join("src").is_dir() { "src" } else { "." };
let inside = if source == "." { String::new() } else { format!("{source}/") };
let nested = ordinance::parcels(dir, &[])
.into_iter()
.filter(|p| p.dir != ".")
.filter_map(|p| p.dir.strip_prefix(&inside).map(|within| format!("{within}/**")))
.collect();
(source, nested)
}