Skip to content

Instantly share code, notes, and snippets.

@ShapeShapa
Last active October 4, 2021 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShapeShapa/b1a59bcf72057899eda3572d4ec2b714 to your computer and use it in GitHub Desktop.
Save ShapeShapa/b1a59bcf72057899eda3572d4ec2b714 to your computer and use it in GitHub Desktop.
generates a folder structure from a given json file
use serde_json::Value;
use std::{any, fs, path::Path};
fn parse_json_file(file: &str) -> Value {
let contents: String = fs::read_to_string(file).expect("Unable to read file");
return serde_json::from_str(&contents).unwrap();
}
fn make_dirs(json: &Value, dir: &Path) {
if let Some(i) = json.as_object() {
i.into_iter().for_each(|(key, value)| {
fs::create_dir(&dir.join(key)).unwrap();
println!("{:?}", &dir.join(key));
if type_of(value) == "&serde_json::value::Value" {
make_dirs(value, &dir.join(key));
}
})
}
}
fn type_of<T>(_: T) -> &'static str {
any::type_name::<T>()
}
pub fn main() {
// folder_structure.json
//
// {
// "root": {
// "sub_1": {},
// "sub_2": {}
// }
// }
let datas = parse_json_file("folder_structure.json");
make_dirs(&datas, Path::new("."))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment