@@ -13,6 +13,7 @@ pub struct Cache {
13
13
}
14
14
15
15
impl Cache {
16
+ /// Create a new cache, used to read files only once and otherwise store their contents.
16
17
pub fn new ( doc_dir : & str ) -> Cache {
17
18
Cache {
18
19
root : Path :: new ( doc_dir) . to_owned ( ) ,
@@ -32,9 +33,7 @@ impl Cache {
32
33
}
33
34
}
34
35
35
- pub fn get_file ( & mut self , path : & String ) -> Result < String , io:: Error > {
36
- let path = self . resolve_path ( path) ;
37
-
36
+ fn read_file ( & mut self , path : PathBuf ) -> Result < String , io:: Error > {
38
37
if let Some ( f) = self . files . get ( & path) {
39
38
return Ok ( f. clone ( ) ) ;
40
39
}
@@ -46,16 +45,22 @@ impl Cache {
46
45
Ok ( file)
47
46
}
48
47
48
+ /// Get the text from a file. If called multiple times, the file will only be read once
49
+ pub fn get_file ( & mut self , path : & String ) -> Result < String , io:: Error > {
50
+ let path = self . resolve_path ( path) ;
51
+ self . read_file ( path)
52
+ }
53
+
54
+ /// Parse the JSON from a file. If called multiple times, the file will only be read once.
49
55
pub fn get_value ( & mut self , path : & String ) -> Result < Value , CkError > {
50
56
let path = self . resolve_path ( path) ;
51
57
52
58
if let Some ( v) = self . values . get ( & path) {
53
59
return Ok ( v. clone ( ) ) ;
54
60
}
55
61
56
- let file = fs:: File :: open ( & path) ?;
57
-
58
- let val = serde_json:: from_reader :: < _ , Value > ( file) ?;
62
+ let content = self . read_file ( path. clone ( ) ) ?;
63
+ let val = serde_json:: from_str :: < Value > ( & content) ?;
59
64
60
65
self . values . insert ( path, val. clone ( ) ) ;
61
66
0 commit comments