use crate::Profile;
use crate::error::SpacesError;
pub const MAX_DEPTH: usize = 8;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Imports {
pub code: Vec<String>,
pub spaces: Vec<String>,
pub space_aliases: Vec<(String, String)>,
}
fn space_part(part: &str) -> Option<(String, Option<String>)> {
let (k, n) = part.split_once(':')?;
if !k.trim().eq_ignore_ascii_case("space") || n.trim().is_empty() {
return None;
}
let n = n.trim();
match n.split_once(" as ") {
Some((t, a)) if !t.trim().is_empty() && !a.trim().is_empty() => Some((t.trim().to_string(), Some(a.trim().to_string()))),
_ => Some((n.to_string(), None)),
}
}
fn header(line: &str) -> Option<(usize, usize)> {
let i = line.find("@imports")?;
let start = i + "@imports".len();
let rest = &line[start..];
let trimmed = rest.trim_end();
let body = trimmed.trim_end_matches("*/").trim_end_matches("-->").trim_end();
Some((start, start + body.len()))
}
pub fn imports(source: &str) -> Imports {
let mut out = Imports::default();
for line in source.lines() {
let Some((a, b)) = header(line) else { continue };
for part in line[a..b].split(',').map(str::trim).filter(|p| !p.is_empty()) {
match space_part(part) {
Some((n, alias)) => {
out.space_aliases.push((alias.unwrap_or_else(|| n.clone()), n.clone()));
out.spaces.push(n);
}
None => out.code.push(part.to_string()),
}
}
}
out
}
pub fn rename_space_import(source: &str, from: &str, to: &str) -> Option<String> {
let mut changed = false;
let lines: Vec<String> = source
.split_inclusive('\n')
.map(|line| {
let Some((a, b)) = header(line) else { return line.to_string() };
let parts: Vec<String> = line[a..b]
.split(',')
.map(|part| match space_part(part.trim()) {
Some((n, alias)) if n.eq_ignore_ascii_case(from) => {
changed = true;
let alias = alias.unwrap_or(n);
if alias.eq_ignore_ascii_case(to) { format!(" space:{to}") } else { format!(" space:{to} as {alias}") }
}
_ => part.to_string(),
})
.collect();
format!("{}{}{}", &line[..a], parts.join(","), &line[b..])
})
.collect();
changed.then(|| lines.concat())
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Lineup<'a> {
pub main_id: &'a str,
pub kind: &'a str,
pub members: Vec<(&'a str, &'a str, &'a str)>,
pub main_source: &'a str,
}
pub fn lineup_hash(profile: &Profile, l: &Lineup) -> String {
let mut ms: Vec<(String, String, String)> = l.members.iter().map(|(a, c, p)| (a.to_lowercase(), c.to_string(), p.to_string())).collect();
ms.sort();
let mut sp: Vec<String> = imports(l.main_source).spaces.iter().map(|x| x.to_lowercase()).collect();
sp.sort();
sp.dedup();
let mut h = blake3::Hasher::new();
h.update(profile.lineup_domain);
h.update(l.main_id.as_bytes());
h.update(&[0]);
h.update(l.kind.as_bytes());
for (a, c, p) in &ms {
h.update(format!("\0m{a}\0{c}\0{p}").as_bytes());
}
for x in &sp {
h.update(format!("\0s{x}").as_bytes());
}
h.finalize().to_hex()[..32].to_string()
}
pub trait Graph {
type Space;
type Error: From<SpacesError>;
fn name<'s>(&self, s: &'s Self::Space) -> &'s str;
fn main_source(&self, s: &Self::Space) -> Result<String, Self::Error>;
fn find(&self, name: &str) -> Option<Self::Space>;
}
pub fn check_cycles<G: Graph>(g: &G, s: &G::Space) -> Result<(), G::Error> {
fn walk<G: Graph>(g: &G, s: &G::Space, path: &mut Vec<String>) -> Result<(), G::Error> {
let name = g.name(s);
if path.iter().any(|p| p.eq_ignore_ascii_case(name)) {
path.push(name.to_string());
return Err(SpacesError::invalid("space", format!("a cycle of spaces: {}", path.join(" → "))).into());
}
if path.len() >= MAX_DEPTH {
return Err(
SpacesError::invalid("space", format!("spaces call spaces more than {MAX_DEPTH} deep: {} → {}", path.join(" → "), name)).into()
);
}
path.push(name.to_string());
for n in imports(&g.main_source(s)?).spaces {
let t = g.find(&n).ok_or_else(|| SpacesError::invalid("space", format!("{}: main imports space:{n}, which doesn't exist", g.name(s))))?;
walk(g, &t, path)?;
}
path.pop();
Ok(())
}
walk(g, s, &mut vec![])
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
#[test]
fn imports_and_renames() {
let i = imports("// @imports fast, slow, space:prices as p\n/* @imports space:other */\nfn run() {}");
assert_eq!(i.code, vec!["fast", "slow"]);
assert_eq!(i.spaces, vec!["prices", "other"]);
assert_eq!(i.space_aliases, vec![("p".to_string(), "prices".to_string()), ("other".to_string(), "other".to_string())]);
let r = rename_space_import("// @imports a, space:old\nx\n", "old", "new").unwrap();
assert_eq!(r, "// @imports a, space:new as old\nx\n");
assert_eq!(imports(&r).space_aliases, vec![("old".to_string(), "new".to_string())]);
assert!(rename_space_import("// @imports a\n", "old", "new").is_none());
}
#[test]
fn lineup_hash_ignores_case_and_order() {
let a = Lineup {
main_id: "m",
kind: "analysis",
members: vec![("A", "c1", ""), ("b", "c2", "pin")],
main_source: "// @imports space:X, space:y",
};
let b = Lineup {
main_id: "m",
kind: "analysis",
members: vec![("B", "c2", "pin"), ("a", "c1", "")],
main_source: "// @imports space:Y, space:x, space:x",
};
assert_eq!(lineup_hash(&crate::WEIRDNET, &a), lineup_hash(&crate::WEIRDNET, &b));
let c = Lineup { kind: "transform", ..a.clone() };
assert_ne!(lineup_hash(&crate::WEIRDNET, &a), lineup_hash(&crate::WEIRDNET, &c));
assert_eq!(lineup_hash(&crate::WEIRDNET, &a).len(), 32);
}
struct G(HashMap<&'static str, &'static str>);
impl Graph for G {
type Space = String;
type Error = SpacesError;
fn name<'s>(&self, s: &'s String) -> &'s str {
s
}
fn main_source(&self, s: &String) -> Result<String, SpacesError> {
Ok(self.0[s.as_str()].to_string())
}
fn find(&self, name: &str) -> Option<String> {
let n = name.to_lowercase();
self.0.contains_key(n.as_str()).then_some(n)
}
}
#[test]
fn cycles_depth_and_missing_imports() {
let g = G(HashMap::from([("a", "// @imports space:b"), ("b", "// @imports space:A"), ("c", "// @imports space:nope"), ("d", "")]));
let e = check_cycles(&g, &"a".to_string()).unwrap_err().to_string();
assert_eq!(e, "invalid space: a cycle of spaces: a → b → a");
let e = check_cycles(&g, &"c".to_string()).unwrap_err().to_string();
assert_eq!(e, "invalid space: c: main imports space:nope, which doesn't exist");
check_cycles(&g, &"d".to_string()).unwrap();
let names = ["s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9"];
let srcs = [
"// @imports space:s1",
"// @imports space:s2",
"// @imports space:s3",
"// @imports space:s4",
"// @imports space:s5",
"// @imports space:s6",
"// @imports space:s7",
"// @imports space:s8",
"// @imports space:s9",
"",
];
let g = G(names.iter().copied().zip(srcs).collect());
let e = check_cycles(&g, &"s0".to_string()).unwrap_err().to_string();
assert!(e.contains("more than 8 deep: s0 → s1 → s2 → s3 → s4 → s5 → s6 → s7 → s8"), "{e}");
check_cycles(&g, &"s2".to_string()).unwrap();
}
}