]> git.lizzy.rs Git - rust.git/blob - src/tools/jsondocck/src/cache.rs
Auto merge of #97870 - eggyal:inplace_fold_spec, r=wesleywiser
[rust.git] / src / tools / jsondocck / src / cache.rs
1 use crate::config::Config;
2 use serde_json::Value;
3 use std::collections::HashMap;
4 use std::path::Path;
5
6 use fs_err as fs;
7
8 #[derive(Debug)]
9 pub struct Cache {
10     value: Value,
11     pub variables: HashMap<String, Value>,
12 }
13
14 impl Cache {
15     /// Create a new cache, used to read files only once and otherwise store their contents.
16     pub fn new(config: &Config) -> Cache {
17         let root = Path::new(&config.doc_dir);
18         let filename = Path::new(&config.template).file_stem().unwrap();
19         let file_path = root.join(&Path::with_extension(Path::new(filename), "json"));
20         let content = fs::read_to_string(&file_path).expect("failed to read JSON file");
21
22         Cache {
23             value: serde_json::from_str::<Value>(&content).expect("failed to convert from JSON"),
24             variables: HashMap::new(),
25         }
26     }
27
28     pub fn value(&self) -> &Value {
29         &self.value
30     }
31 }