use std::fs;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, bail};
pub const MARKER: &str = "[package.metadata.slave]";
pub fn find_root() -> Result<PathBuf> {
let cwd = std::env::current_dir().context("failed to read the current directory")?;
find_root_from(&cwd)
}
fn find_root_from(start: &Path) -> Result<PathBuf> {
for dir in start.ancestors() {
let manifest = dir.join("Cargo.toml");
if let Ok(contents) = fs::read_to_string(&manifest) {
if contents.lines().any(|line| line.trim() == MARKER) {
return Ok(dir.to_path_buf());
}
}
}
bail!(
"not inside a slave application (no Cargo.toml containing `{MARKER}` was found).\n\
Create one with `slave new <name>` and run this command from its directory."
)
}