use std::env; use std::error::Error; use std::fs::{read as read_file, File}; use std::io::ErrorKind::NotFound; use std::io::Write; use std::path::Path; pub fn create_static_output( source_path: &Path, mut target: impl Write, ) -> Result<(), Box> { let const_name = source_path .file_stem() .ok_or("Not a filename")? .to_str() .ok_or("Filename is not valid unicode")? .to_uppercase(); let file = read_file(source_path)?; let content = std::str::from_utf8(&file)?; let fence = content.chars().filter(|&c| c == '#').collect::() + "#"; writeln!( target, "pub const {const_name}: &str = r{fence}\"{content}\"{fence};" )?; Ok(()) } pub fn create_static_output_files( source_dir: &Path, dest_path: &Path, ) -> Result<(), Box> { let mut f = File::create(dest_path)?; match source_dir.read_dir() { Ok(dir_content) => { for maybe_dir_entry in dir_content { create_static_output(&maybe_dir_entry?.path(), &mut f)?; } } Err(err) => { if err.kind() != NotFound { return Err(format!("Unexpected error: {err}").into()); } } } Ok(()) } pub fn main() -> Result<(), Box> { create_static_output_files( Path::new("static_files"), &(Path::new(&env::var("OUT_DIR")?).join("static_files.rs")), ) }