use std::path::Path;
pub const NAMESPACE_ENV: &str = "MEMFORK_NAMESPACE";
pub const FALLBACK: &str = "default";
pub const MAX_LEN: usize = 64;
pub const SEPARATOR: char = ':';
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
Flag,
Environment,
Repository,
WorkingDirectory,
Fallback,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Namespace {
pub name: String,
pub source: Source,
}
pub fn resolve(flag: Option<&str>, env: Option<&str>, cwd: &Path) -> Result<Namespace, String> {
for (given, source, what) in [
(flag, Source::Flag, "--namespace"),
(env, Source::Environment, NAMESPACE_ENV),
] {
if let Some(raw) = given.map(str::trim).filter(|s| !s.is_empty()) {
validate(raw).map_err(|why| {
format!(
"{what} `{raw}` is not a usable namespace: {why}. Something like `{}` would be.",
sanitise(raw).unwrap_or_else(|| FALLBACK.to_owned())
)
})?;
return Ok(Namespace {
name: raw.to_owned(),
source,
});
}
}
Ok(detect(cwd))
}
pub fn detect(cwd: &Path) -> Namespace {
let (dir, source) = match repository_root(cwd) {
Some(root) => (root, Source::Repository),
None => (cwd.to_path_buf(), Source::WorkingDirectory),
};
match dir.file_name().and_then(|n| n.to_str()).and_then(sanitise) {
Some(name) => Namespace { name, source },
None => Namespace {
name: FALLBACK.to_owned(),
source: Source::Fallback,
},
}
}
pub fn repository_root(start: &Path) -> Option<std::path::PathBuf> {
start
.ancestors()
.find(|dir| dir.join(".git").exists())
.map(Path::to_path_buf)
}
pub fn validate(name: &str) -> Result<(), &'static str> {
if name.is_empty() {
return Err("it is empty");
}
if name.len() > MAX_LEN {
return Err("it is longer than 64 characters");
}
if !name
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'.' | b'_' | b'-'))
{
return Err("only lowercase letters, digits, `.`, `_` and `-` are allowed");
}
if name.starts_with(['.', '-']) || name.ends_with(['.', '-']) {
return Err("it may not start or end with `.` or `-`");
}
Ok(())
}
pub fn sanitise(raw: &str) -> Option<String> {
let mut out = String::with_capacity(raw.len());
for c in raw.chars().flat_map(char::to_lowercase) {
let kept = c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '.' | '_');
if kept {
out.push(c);
} else if !out.is_empty() && !out.ends_with('-') {
out.push('-');
}
}
let trimmed = out.trim_matches(['.', '-']);
let mut cut = trimmed.chars().take(MAX_LEN).collect::<String>();
while cut.ends_with(['.', '-']) {
cut.pop();
}
(!cut.is_empty()).then_some(cut)
}
pub fn prefix(namespace: &str, kind: &str) -> String {
format!("{namespace}{SEPARATOR}{kind}{SEPARATOR}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn directory_names_become_namespaces() {
assert_eq!(sanitise("memfork").as_deref(), Some("memfork"));
assert_eq!(sanitise("MemFork").as_deref(), Some("memfork"));
assert_eq!(sanitise("My Project (2)").as_deref(), Some("my-project-2"));
assert_eq!(sanitise("web_app.v2").as_deref(), Some("web_app.v2"));
assert_eq!(sanitise("--odd--").as_deref(), Some("odd"));
assert_eq!(sanitise("café-ünï").as_deref(), Some("caf-n"));
assert_eq!(sanitise("项目"), None);
assert_eq!(sanitise(""), None);
let long = "a".repeat(100);
assert_eq!(sanitise(&long).map(|s| s.len()), Some(MAX_LEN));
}
#[test]
fn a_sanitised_name_is_always_valid() {
for raw in ["memfork", "My Project", "a.b-c_d", "x--", "-.-y", "ÄÖÜ abc"] {
if let Some(name) = sanitise(raw) {
assert_eq!(validate(&name), Ok(()), "{raw} -> {name}");
assert!(!name.contains(SEPARATOR));
}
}
}
#[test]
fn explicit_names_are_checked_not_rewritten() {
let here = Path::new(".");
assert_eq!(
resolve(Some("team-api"), Some("other"), here).unwrap(),
Namespace {
name: "team-api".to_owned(),
source: Source::Flag
}
);
assert_eq!(
resolve(None, Some("other"), here).unwrap().source,
Source::Environment
);
let err = resolve(Some("Team API"), None, here).unwrap_err();
assert!(err.contains("--namespace `Team API`"), "{err}");
assert!(err.contains("`team-api`"), "{err}");
let err = resolve(None, Some("a:b"), here).unwrap_err();
assert!(err.contains(NAMESPACE_ENV), "{err}");
}
#[test]
fn blank_explicit_values_fall_through() {
let dir = tempfile::tempdir().unwrap();
let ns = resolve(Some(" "), Some(""), dir.path()).unwrap();
assert_ne!(ns.source, Source::Flag);
assert_ne!(ns.source, Source::Environment);
}
#[test]
fn the_repository_root_names_the_namespace_from_any_depth() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("Shop Front");
let deep = repo.join("src").join("cart");
std::fs::create_dir_all(&deep).unwrap();
std::fs::create_dir_all(repo.join(".git")).unwrap();
for start in [&repo, &deep] {
let ns = detect(start);
assert_eq!(ns.name, "shop-front");
assert_eq!(ns.source, Source::Repository);
}
}
#[test]
fn a_worktree_git_file_marks_the_top_level_too() {
let tmp = tempfile::tempdir().unwrap();
let wt = tmp.path().join("feature-x");
std::fs::create_dir_all(wt.join("docs")).unwrap();
std::fs::write(
wt.join(".git"),
"gitdir: /elsewhere/.git/worktrees/feature-x\n",
)
.unwrap();
let ns = detect(&wt.join("docs"));
assert_eq!(ns.name, "feature-x");
assert_eq!(ns.source, Source::Repository);
}
#[test]
fn outside_a_repository_the_working_directory_names_it() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("Scratch Notes");
std::fs::create_dir_all(&dir).unwrap();
if repository_root(&dir).is_none() {
let ns = detect(&dir);
assert_eq!(ns.name, "scratch-notes");
assert_eq!(ns.source, Source::WorkingDirectory);
}
}
#[test]
fn a_name_with_nothing_usable_falls_back() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("项目");
std::fs::create_dir_all(&dir).unwrap();
if repository_root(&dir).is_none() {
let ns = detect(&dir);
assert_eq!(ns.name, FALLBACK);
assert_eq!(ns.source, Source::Fallback);
}
}
#[test]
fn prefixes_use_the_colon_convention() {
assert_eq!(prefix("shop", "decision"), "shop:decision:");
}
}