framework-startup 26.8.151

lingting framework
Documentation
//! 自启定义文件的写入与删除。

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())),
    }
}