endcap-sl-software-ri-gener.../src/io.rs

28 lines
882 B
Rust
Raw Normal View History

//! File IO for generated codes.
use std::{collections::HashMap, fs, io, path};
/// Write formatted codes generated with [`Module::generate_code`](crate::types::Module::generate_code).
pub fn write_to_files(
files: HashMap<path::PathBuf, syn::File>,
out_path: &path::Path,
) -> io::Result<()> {
if !out_path.is_dir() {
return Err(io::Error::from(io::ErrorKind::NotADirectory));
}
if fs::read_dir(out_path)?.next().is_some() {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("out path `{}` is not empty", out_path.display()),
));
}
for (file_path, code) in files {
fs::DirBuilder::new()
.recursive(true)
.create(out_path.join(&file_path).parent().unwrap())?;
fs::write(out_path.join(&file_path), prettyplease::unparse(&code))?;
}
Ok(())
}