use super::super::{NINJA_ENV, NINJA_PROGRAM};
use camino::Utf8PathBuf;
use mockable::Env;
use std::path::PathBuf;
use tracing::debug;
pub(super) fn resolve_ninja_program_utf8_with(env: &impl Env) -> Utf8PathBuf {
env.os_string(NINJA_ENV).map_or_else(
|| {
debug!(
ninja_program = NINJA_PROGRAM,
source = "fallback",
"Resolved Ninja executable from default program",
);
Utf8PathBuf::from(NINJA_PROGRAM)
},
|value| {
let path = PathBuf::from(value);
if path.as_os_str().is_empty() {
debug!(
fallback_program = NINJA_PROGRAM,
source = "fallback",
"Ignoring empty Ninja executable override",
);
Utf8PathBuf::from(NINJA_PROGRAM)
} else {
match Utf8PathBuf::from_path_buf(path) {
Ok(program) => {
debug!(
ninja_program = %program,
source = NINJA_ENV,
"Resolved Ninja executable from environment override",
);
program
}
Err(non_utf8_path) => {
debug!(
configured_ninja = %non_utf8_path.to_string_lossy(),
fallback_program = NINJA_PROGRAM,
source = "fallback",
"Ignoring non-UTF-8 Ninja executable override",
);
Utf8PathBuf::from(NINJA_PROGRAM)
}
}
}
},
)
}
#[cfg(test)]
pub(super) fn resolve_ninja_program_with(env: &impl Env) -> PathBuf {
resolve_ninja_program_utf8_with(env).into()
}
#[must_use]
pub fn resolve_ninja_program_utf8() -> Utf8PathBuf {
resolve_ninja_program_utf8_with(&mockable::DefaultEnv)
}
#[must_use]
pub fn resolve_ninja_program() -> PathBuf {
resolve_ninja_program_utf8().into()
}