Skip to content

Instantly share code, notes, and snippets.

@arctic-hen7
Created September 1, 2021 10:05
Show Gist options
  • Save arctic-hen7/397f32ca26528190e289ab895bebb970 to your computer and use it in GitHub Desktop.
Save arctic-hen7/397f32ca26528190e289ab895bebb970 to your computer and use it in GitHub Desktop.
Temporary fix for `include_dir` #59
// This file contains a temporary fix for the issues with recursive extraction in `include_dir`
// Tracking issue is https://github.com/Michael-F-Bryan/include_dir/issues/59
use std::path::Path;
use include_dir::Dir;
use std::io::Write;
/// Extracts a directory included with `include_dir!` until issue #59 is fixed on that module (recursive extraction support).
pub fn extract_dir<S: AsRef<Path>>(dir: Dir, path: S) -> std::io::Result<()> {
let path = path.as_ref();
// Create all the subdirectories in here (but not their files yet)
for dir in dir.dirs() {
std::fs::create_dir_all(path.join(dir.path()))?;
// Recurse for this directory
extract_dir(*dir, path)?;
}
// Write all the files at the root of this directory
for file in dir.files() {
let mut fsf = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path.join(file.path()))?;
fsf.write_all(file.contents())?;
fsf.sync_all()?;
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment