use std::env; use std::fs::{read_dir, File}; use std::io::ErrorKind::NotFound; use std::io::Read; use std::io::Write; use std::path::{Path, PathBuf}; fn get_const_name>(p: &P) -> String { let mut file_name_without_extension = p.clone().into(); file_name_without_extension.set_extension(""); String::from( file_name_without_extension .file_name() .unwrap() .to_string_lossy(), ) .to_uppercase() } pub fn create_static_output_files(source_dir: &str) { let out_dir = env::var("OUT_DIR").unwrap(); let dest_path = Path::new(&out_dir).join("static_files.rs"); let mut f = File::create(&dest_path).unwrap(); match read_dir(source_dir) { Ok(dir_content) => { for maybe_dir_entry in dir_content { let file_path = maybe_dir_entry.unwrap().path(); let mut buffer = String::new(); File::open(file_path.clone()) .unwrap() .read_to_string(&mut buffer) .unwrap(); let fence = buffer.chars().filter(|c| *c == '#').collect::() + "#"; f.write_all( format!( "pub const {}: &str = r{1}\"{2}\"{1};\n", get_const_name(&file_path), fence, buffer ) .as_bytes(), ) .unwrap(); } } Err(err) => { if err.kind() == NotFound { } else { Err(err).unwrap() } } } } #[allow(unused)] fn main() { create_static_output_files("static_files"); }