Accept missing static_files dir

This commit is contained in:
Adrian Heine 2020-03-03 23:58:25 +01:00
parent 907a4962c5
commit 9dd2da3cc4

View file

@ -1,5 +1,6 @@
use std::env; use std::env;
use std::fs::{read_dir, File}; use std::fs::{read_dir, File};
use std::io::ErrorKind::NotFound;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -20,24 +21,34 @@ pub fn create_static_output_files(source_dir: &str) {
let out_dir = env::var("OUT_DIR").unwrap(); let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("static_files.rs"); let dest_path = Path::new(&out_dir).join("static_files.rs");
let mut f = File::create(&dest_path).unwrap(); let mut f = File::create(&dest_path).unwrap();
for maybe_dir_entry in read_dir(source_dir).unwrap() { match read_dir(source_dir) {
let file_path = maybe_dir_entry.unwrap().path(); Ok(dir_content) => {
let mut buffer = String::new(); for maybe_dir_entry in dir_content {
File::open(file_path.clone()) let file_path = maybe_dir_entry.unwrap().path();
.unwrap() let mut buffer = String::new();
.read_to_string(&mut buffer) File::open(file_path.clone())
.unwrap(); .unwrap()
let fence = buffer.chars().filter(|c| *c == '#').collect::<String>() + "#"; .read_to_string(&mut buffer)
f.write_all( .unwrap();
format!( let fence = buffer.chars().filter(|c| *c == '#').collect::<String>() + "#";
"pub const {}: &str = r{1}\"{2}\"{1};\n", f.write_all(
get_const_name(&file_path), format!(
fence, "pub const {}: &str = r{1}\"{2}\"{1};\n",
buffer get_const_name(&file_path),
) fence,
.as_bytes(), buffer
) )
.unwrap(); .as_bytes(),
)
.unwrap();
}
}
Err(err) => {
if err.kind() == NotFound {
} else {
Err(err).unwrap()
}
}
} }
} }