mod analyze;
mod git;
mod render;
mod units;
#[cfg(test)]
mod tests;
use std::path::Path;
use analyze::analyze;
pub const SCHEMA: &str = "lean-ctx.landing-impact.v1";
pub const DEFAULT_DEPTH: usize = 3;
pub const MAX_DEPTH: usize = 8;
fn is_json(format: Option<&str>) -> bool {
matches!(format, Some(f) if f.eq_ignore_ascii_case("json"))
}
pub fn handle(
base: Option<&str>,
head: Option<&str>,
other: Option<&str>,
depth: Option<usize>,
root: &str,
format: Option<&str>,
) -> String {
let pick = |r: Option<&str>, d: &'static str| {
r.map(str::trim)
.filter(|s| !s.is_empty())
.unwrap_or(d)
.to_string()
};
let base = pick(base, "HEAD~1");
let head = pick(head, "HEAD");
let other = other.map(str::trim).filter(|s| !s.is_empty());
let depth = depth.unwrap_or(DEFAULT_DEPTH).clamp(1, MAX_DEPTH);
match analyze(Path::new(root), &base, &head, other, depth) {
Ok(report) if is_json(format) => render::json(&report),
Ok(report) => render::text(&report),
Err(e) if is_json(format) => render::json_error(&e),
Err(e) => format!("landing impact failed: {e}"),
}
}
pub fn cli(args: &[String], root: &str, format: Option<&str>) -> String {
let mut positional: Vec<&str> = Vec::new();
let mut other: Option<&str> = None;
let mut depth: Option<usize> = None;
let mut i = 0;
while i < args.len() {
let a = args[i].as_str();
if let Some(v) = a.strip_prefix("--other=") {
other = Some(v);
} else if a == "--other" {
other = args.get(i + 1).map(String::as_str);
i += 1;
} else if let Some(v) = a.strip_prefix("--depth=") {
depth = v.parse().ok();
} else if a == "--depth" {
depth = args.get(i + 1).and_then(|v| v.parse().ok());
i += 1;
} else if !a.starts_with("--") {
positional.push(a);
}
i += 1;
}
handle(
positional.first().copied(),
positional.get(1).copied(),
other,
depth,
root,
format,
)
}