use std::fs::{self, File};
use std::io::{self, Write};
use std::path::PathBuf;
pub fn write_to_file(path: &PathBuf, data: &[u8]) -> io::Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let temp_path = path.with_extension("tmp");
let mut temp_file = File::create(&temp_path)?;
{
temp_file.write_all(data)?;
temp_file.sync_all()?;
}
match fs::rename(&temp_path, path) {
Ok(_) => Ok(()),
Err(e) => {
let _ = fs::remove_file(&temp_path);
Err(e)
}
}
}
pub fn capitalize(s: &str) -> String {
s.chars()
.enumerate()
.map(|(i, c)| if i == 0 { c.to_ascii_uppercase() } else { c })
.collect()
}
pub fn sanitize_category_name(name: &str) -> String {
name.to_lowercase().replace(' ', "-")
}