From 14ab9544c9aa0c21bb611217e7747822f1cd6dbe Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Mon, 23 May 2022 14:04:39 +0100 Subject: [PATCH] aya-gen: create directories before writing file Signed-off-by: Dave Tucker --- aya-gen/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/aya-gen/src/lib.rs b/aya-gen/src/lib.rs index e635cd31..d0126863 100644 --- a/aya-gen/src/lib.rs +++ b/aya-gen/src/lib.rs @@ -1,5 +1,5 @@ use std::{ - fs::File, + fs::{create_dir_all, File}, io::{self, Write}, path::Path, }; @@ -10,10 +10,16 @@ pub mod getters; pub mod rustfmt; pub fn write_to_file>(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)?; file.write_all(code.as_bytes()) } pub fn write_to_file_fmt>(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)?) }