use std::path::{Path, PathBuf};
const SHELL_ONLY_EXTS: &[&str] = &[".cmd", ".bat", ".ps1"];
pub struct CspEnv<'a> {
pub csp_bin: Option<String>,
pub path: Option<String>,
pub pathext: Option<String>,
pub is_win: bool,
pub is_executable: &'a dyn Fn(&Path) -> bool,
}
pub fn resolve_csp(env: &CspEnv) -> Option<String> {
if let Some(override_) = env
.csp_bin
.as_ref()
.map(|s| s.trim())
.filter(|s| !s.is_empty())
{
return Some(override_.to_string());
}
let path_var = env.path.as_deref().unwrap_or("");
if path_var.is_empty() {
return None;
}
let exts: Vec<String> = if env.is_win {
env.pathext
.as_deref()
.unwrap_or(".EXE;.COM")
.split(';')
.filter(|e| !e.is_empty())
.filter(|e| !SHELL_ONLY_EXTS.contains(&e.to_ascii_lowercase().as_str()))
.map(|e| e.to_string())
.collect()
} else {
vec![String::new()]
};
let sep = if env.is_win { ';' } else { ':' };
for dir in path_var.split(sep) {
if dir.is_empty() {
continue;
}
for ext in &exts {
let candidate = PathBuf::from(dir).join(format!("csp{}", ext.to_ascii_lowercase()));
if (env.is_executable)(&candidate) {
return Some(candidate.to_string_lossy().into_owned());
}
if env.is_win {
let upper = PathBuf::from(dir).join(format!("csp{}", ext.to_ascii_uppercase()));
if (env.is_executable)(&upper) {
return Some(upper.to_string_lossy().into_owned());
}
}
}
}
None
}
pub fn default_is_executable(p: &Path) -> bool {
let Ok(meta) = std::fs::metadata(p) else {
return false;
};
if !meta.is_file() {
return false;
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
meta.permissions().mode() & 0o111 != 0
}
#[cfg(not(unix))]
{
true
}
}
pub fn resolve_csp_default() -> Option<String> {
let env = CspEnv {
csp_bin: std::env::var("CSP_BIN").ok(),
path: std::env::var("PATH")
.ok()
.or_else(|| std::env::var("Path").ok()),
pathext: std::env::var("PATHEXT").ok(),
is_win: cfg!(windows),
is_executable: &default_is_executable,
};
resolve_csp(&env)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn csp_bin_override_wins_without_probe() {
let never = |_: &Path| false;
let env = CspEnv {
csp_bin: Some(" /custom/csp ".into()),
path: Some("/usr/bin".into()),
pathext: None,
is_win: false,
is_executable: &never,
};
assert_eq!(resolve_csp(&env).as_deref(), Some("/custom/csp"));
}
#[test]
fn empty_override_falls_through_to_path() {
let match_bin = |p: &Path| p == Path::new("/opt/bin/csp");
let env = CspEnv {
csp_bin: Some(" ".into()),
path: Some("/usr/bin:/opt/bin".into()),
pathext: None,
is_win: false,
is_executable: &match_bin,
};
assert_eq!(resolve_csp(&env).as_deref(), Some("/opt/bin/csp"));
}
#[test]
fn absent_from_path_returns_none() {
let never = |_: &Path| false;
let env = CspEnv {
csp_bin: None,
path: Some("/usr/bin:/opt/bin".into()),
pathext: None,
is_win: false,
is_executable: &never,
};
assert_eq!(resolve_csp(&env), None);
}
#[test]
fn empty_path_returns_none() {
let never = |_: &Path| false;
let env = CspEnv {
csp_bin: None,
path: None,
pathext: None,
is_win: false,
is_executable: &never,
};
assert_eq!(resolve_csp(&env), None);
}
#[test]
fn windows_drops_shell_only_extensions() {
let match_cmd = |p: &Path| p.to_string_lossy().to_lowercase().ends_with("csp.cmd");
let env = CspEnv {
csp_bin: None,
path: Some("C:\\bin".into()),
pathext: Some(".CMD;.EXE".into()),
is_win: true,
is_executable: &match_cmd,
};
assert_eq!(resolve_csp(&env), None);
}
}