aya-gen: create directories before writing file

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
pull/277/head
Dave Tucker 3 years ago
parent 3acd8d3650
commit 14ab9544c9

@ -1,5 +1,5 @@
use std::{ use std::{
fs::File, fs::{create_dir_all, File},
io::{self, Write}, io::{self, Write},
path::Path, path::Path,
}; };
@ -10,10 +10,16 @@ pub mod getters;
pub mod rustfmt; pub mod rustfmt;
pub fn write_to_file<T: AsRef<Path>>(path: T, code: &str) -> Result<(), io::Error> { pub fn write_to_file<T: AsRef<Path>>(path: T, code: &str) -> Result<(), io::Error> {
if let Some(parent) = path.as_ref().parent() {
create_dir_all(parent)?;
}
let mut file = File::create(path)?; let mut file = File::create(path)?;
file.write_all(code.as_bytes()) file.write_all(code.as_bytes())
} }
pub fn write_to_file_fmt<T: AsRef<Path>>(path: T, code: &str) -> Result<(), io::Error> { pub fn write_to_file_fmt<T: AsRef<Path>>(path: T, code: &str) -> Result<(), io::Error> {
if let Some(parent) = path.as_ref().parent() {
create_dir_all(parent)?;
}
write_to_file(path, &rustfmt::format(code)?) write_to_file(path, &rustfmt::format(code)?)
} }

Loading…
Cancel
Save