use std::fs;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
pub fn ensure_dir(path: &Path) -> anyhow::Result<PathBuf> {
if !path.exists() {
fs::create_dir_all(path)?;
}
Ok(path.to_path_buf())
}
pub fn ensure_mod_rs(dir: &Path) -> anyhow::Result<PathBuf> {
let mod_rs = dir.join("mod.rs");
if !mod_rs.exists() {
fs::write(&mod_rs, format!("// module: {}\n\n", dir.file_name().and_then(|s| s.to_str()).unwrap_or("?")))?;
}
Ok(mod_rs)
}
pub fn ensure_pub_mod_decl(mod_rs: &Path, child_mod: &str) -> anyhow::Result<()> {
let decl = format!("pub mod {};", child_mod);
let mut content = String::new();
if mod_rs.exists() {
let mut f = fs::File::open(mod_rs)?;
f.read_to_string(&mut content)?;
} else {
if let Some(parent) = mod_rs.parent() {
fs::create_dir_all(parent)?;
}
fs::File::create(mod_rs)?;
}
if content.lines().any(|l| l.trim() == decl) {
return Ok(());
}
let mut f = fs::OpenOptions::new().append(true).open(mod_rs)?;
writeln!(f, "{}", decl)?;
Ok(())
}
pub fn ensure_root_mod(module_name: &str) -> anyhow::Result<()> {
let lib_rs = Path::new("src").join("lib.rs");
let decl = format!("pub mod {};", module_name);
ensure_dir(Path::new("src"))?;
if !lib_rs.exists() {
fs::write(&lib_rs, "// auto-generated lib.rs\n\n")?;
}
let mut content = String::new();
{
let mut f = fs::File::open(&lib_rs)?;
f.read_to_string(&mut content)?;
}
if content.lines().any(|l| l.trim() == decl) {
return Ok(());
}
let mut f = fs::OpenOptions::new().append(true).open(&lib_rs)?;
writeln!(f, "{}", decl)?;
Ok(())
}
pub fn module_dir(module: &str) -> PathBuf {
Path::new("src").join(module)
}
pub fn current_mod_file() -> PathBuf {
let cwd = std::env::current_dir().unwrap();
if cwd.ends_with("src") {
cwd.join("lib.rs")
} else {
cwd.join("mod.rs")
}
}