use std::env;
use std::io;
use std::path::{Path, PathBuf};
use crate::memory::mounts::{Mount, Refusal};
use crate::repr::repr;
pub const HOME_ENV: &str = "NEOSIAN_HOME";
const FIX: &str = "pass --scope or --mount (or memory_scope=) to name the scope yourself";
const USER_PROSE: &str =
"durable facts about the user: preferences, identity, standing constraints";
const PROJECT_PROSE: &str = "facts local to this project: decisions, layout, conventions";
pub fn home() -> io::Result<PathBuf> {
let user =
|| env::home_dir().ok_or_else(|| io::Error::other("no home directory; set NEOSIAN_HOME"));
match env::var_os(HOME_ENV).filter(|named| !named.is_empty()) {
None => Ok(user()?.join(".neosian")),
Some(named) => match Path::new(&named).strip_prefix("~") {
Ok(below) => Ok(user()?.join(below)),
Err(_) => Ok(named.into()),
},
}
}
fn slug(name: &str) -> String {
let kept = |c: char| c.is_ascii_alphanumeric() || "_.-".contains(c);
let slug: String = name
.chars()
.take(128)
.map(|c| if kept(c) { c } else { '-' })
.collect();
slug.trim_matches(['-', '.']).to_string()
}
fn login() -> Result<String, Refusal> {
let named = ["LOGNAME", "USER", "LNAME", "USERNAME"]
.iter()
.find_map(|key| env::var(key).ok().filter(|name| !name.is_empty()));
match named
.map(|name| slug(&name))
.filter(|login| !login.is_empty())
{
Some(login) => Ok(login),
None => Err(Refusal::Usage(format!(
"the login cannot be read as a scope id; {FIX}"
))),
}
}
pub fn project_scope(cwd: &Path) -> Result<String, Refusal> {
let user = format!("user:{}", login()?);
let directory = cwd.canonicalize().unwrap_or_else(|_| cwd.to_path_buf());
let name = directory.file_name().map(|name| name.to_string_lossy());
let Some(project) = name.map(|name| slug(&name)).filter(|slug| !slug.is_empty()) else {
let shown = repr(&directory.to_string_lossy());
return Err(Refusal::Usage(format!(
"no project name in {shown} to derive a scope from; {FIX}"
)));
};
Ok(format!("{user}/proj:{project}"))
}
pub fn user_mount() -> Result<Mount, Refusal> {
let scope = format!("user:{}", login()?);
Ok(Mount::new(&scope, "user", (false, false), USER_PROSE)?)
}
pub fn project_mounts(cwd: &Path) -> Result<Vec<Mount>, Refusal> {
let user = user_mount()?;
let scope = project_scope(cwd)?;
Ok(vec![
user,
Mount::new(&scope, "project", (false, false), PROJECT_PROSE)?,
])
}
#[cfg(test)]
mod tests {
use super::slug;
#[test]
fn a_slug_keeps_the_grammar_and_trims_the_edges() {
for (name, id) in [
("neosian", "neosian"),
("My Project", "My-Project"),
(".hidden.", "hidden"),
("café", "caf"),
("a_b.c-d", "a_b.c-d"),
("...", ""),
("日本", ""),
] {
assert_eq!(slug(name), id, "{name}");
}
assert_eq!(slug(&"x".repeat(200)).len(), 128);
}
}