@@ -13,6 +13,7 @@ pub struct Cache {
1313}
1414
1515impl Cache {
16+ /// Create a new cache, used to read files only once and otherwise store their contents.
1617 pub fn new ( doc_dir : & str ) -> Cache {
1718 Cache {
1819 root : Path :: new ( doc_dir) . to_owned ( ) ,
@@ -32,9 +33,7 @@ impl Cache {
3233 }
3334 }
3435
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 > {
3837 if let Some ( f) = self . files . get ( & path) {
3938 return Ok ( f. clone ( ) ) ;
4039 }
@@ -46,16 +45,22 @@ impl Cache {
4645 Ok ( file)
4746 }
4847
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.
4955 pub fn get_value ( & mut self , path : & String ) -> Result < Value , CkError > {
5056 let path = self . resolve_path ( path) ;
5157
5258 if let Some ( v) = self . values . get ( & path) {
5359 return Ok ( v. clone ( ) ) ;
5460 }
5561
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) ?;
5964
6065 self . values . insert ( path, val. clone ( ) ) ;
6166
0 commit comments