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