mirror of
https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator.git
synced 2025-02-24 01:27:08 +09:00
28 lines
848 B
Rust
28 lines
848 B
Rust
|
//! 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,
|
||
|
"out path is not empty",
|
||
|
));
|
||
|
}
|
||
|
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(())
|
||
|
}
|