Last active
October 4, 2021 05:10
-
-
Save ShapeShapa/b1a59bcf72057899eda3572d4ec2b714 to your computer and use it in GitHub Desktop.
generates a folder structure from a given json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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