use std::fs;
use std::io::ErrorKind;
use std::path::Path;
use anyhow::{Context, Result};
pub(crate) fn write_file(path: &Path, content: &str) -> Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.with_context(|| format!("创建目录失败:{}", parent.display()))?;
}
fs::write(path, content).with_context(|| format!("写入文件失败:{}", path.display()))
}
pub(crate) fn remove_file(path: &Path) -> Result<()> {
match fs::remove_file(path) {
Ok(()) => Ok(()),
Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
Err(error) => Err(error).with_context(|| format!("删除文件失败:{}", path.display())),
}
}