use std::path::{Path, PathBuf};
pub(crate) fn manifest_config_path() -> Option<PathBuf> {
let dir = std::env::var("CARGO_MANIFEST_DIR").ok()?;
let path = Path::new(&dir).join("config").join("application.toml");
if path.is_file() {
Some(path)
} else {
None
}
}
pub(crate) fn api_only() -> bool {
manifest_config_path()
.and_then(|p| std::fs::read_to_string(p).ok())
.map(|contents| has_api_only(&contents))
.unwrap_or(false)
}
fn has_api_only(contents: &str) -> bool {
for line in contents.lines() {
let line = line.split('#').next().unwrap_or("").trim();
let Some((key, value)) = line.split_once('=') else {
continue;
};
if key.trim() == "api_only" && value.trim().eq_ignore_ascii_case("true") {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::has_api_only;
#[test]
fn detects_api_only_true() {
assert!(has_api_only("[app]\nname = \"x\"\napi_only = true\n"));
assert!(has_api_only("api_only=true"));
assert!(has_api_only("api_only = TRUE # marker"));
}
#[test]
fn ignores_absent_or_false() {
assert!(!has_api_only("[app]\nname = \"x\"\n"));
assert!(!has_api_only("api_only = false"));
assert!(!has_api_only("# api_only = true"));
assert!(!has_api_only("not_api_only = true"));
}
}