test.rs

 1use std::path::{Path, PathBuf};
 2use tempdir::TempDir;
 3
 4pub fn temp_tree(tree: serde_json::Value) -> TempDir {
 5    let dir = TempDir::new("").unwrap();
 6    write_tree(dir.path(), tree);
 7    dir
 8}
 9
10fn write_tree(path: &Path, tree: serde_json::Value) {
11    use serde_json::Value;
12    use std::fs;
13
14    if let Value::Object(map) = tree {
15        for (name, contents) in map {
16            let mut path = PathBuf::from(path);
17            path.push(name);
18            match contents {
19                Value::Object(_) => {
20                    fs::create_dir(&path).unwrap();
21                    write_tree(&path, contents);
22                }
23                Value::Null => {
24                    fs::create_dir(&path).unwrap();
25                }
26                Value::String(contents) => {
27                    fs::write(&path, contents).unwrap();
28                }
29                _ => {
30                    panic!("JSON object must contain only objects, strings, or null");
31                }
32            }
33        }
34    } else {
35        panic!("You must pass a JSON object to this helper")
36    }
37}