11use crate :: error:: CkError ;
22use serde_json:: Value ;
33use std:: collections:: HashMap ;
4- use std:: fs;
54use std:: path:: { Path , PathBuf } ;
5+ use std:: { fs, io} ;
66
77#[ derive( Debug ) ]
88pub struct Cache {
@@ -15,28 +15,25 @@ pub struct Cache {
1515impl Cache {
1616 pub fn new ( doc_dir : & str ) -> Cache {
1717 Cache {
18- root : < str as AsRef < Path > > :: as_ref ( doc_dir) . to_owned ( ) ,
18+ root : Path :: new ( doc_dir) . to_owned ( ) ,
1919 files : HashMap :: new ( ) ,
2020 values : HashMap :: new ( ) ,
2121 last_path : None ,
2222 }
2323 }
2424
25- fn resolve_path ( & mut self , path : & String ) -> Result < PathBuf , CkError > {
25+ fn resolve_path ( & mut self , path : & String ) -> PathBuf {
2626 if path != "-" {
2727 let resolve = self . root . join ( path) ;
2828 self . last_path = Some ( resolve. clone ( ) ) ;
29- Ok ( resolve)
29+ resolve
3030 } else {
31- match & self . last_path {
32- Some ( p) => Ok ( p. clone ( ) ) ,
33- None => unreachable ! ( ) ,
34- }
31+ self . last_path . as_ref ( ) . unwrap ( ) . clone ( )
3532 }
3633 }
3734
38- pub fn get_file ( & mut self , path : & String ) -> Result < String , CkError > {
39- let path = self . resolve_path ( path) ? ;
35+ pub fn get_file ( & mut self , path : & String ) -> Result < String , io :: Error > {
36+ let path = self . resolve_path ( path) ;
4037
4138 if let Some ( f) = self . files . get ( & path) {
4239 return Ok ( f. clone ( ) ) ;
@@ -47,24 +44,21 @@ impl Cache {
4744 self . files . insert ( path, file. clone ( ) ) ;
4845
4946 Ok ( file)
50- // Err(_) => Err(CkError::FailedCheck(format!("File {:?} does not exist / could not be opened", path)))
5147 }
5248
5349 pub fn get_value ( & mut self , path : & String ) -> Result < Value , CkError > {
54- let path = self . resolve_path ( path) ? ;
50+ let path = self . resolve_path ( path) ;
5551
5652 if let Some ( v) = self . values . get ( & path) {
5753 return Ok ( v. clone ( ) ) ;
5854 }
5955
6056 let file = fs:: File :: open ( & path) ?;
61- // Err(_) => return Err(CkError::FailedCheck(format!("File {:?} does not exist / could not be opened", path)))
6257
6358 let val = serde_json:: from_reader :: < _ , Value > ( file) ?;
6459
6560 self . values . insert ( path, val. clone ( ) ) ;
6661
6762 Ok ( val)
68- // Err(_) => Err(CkError::FailedCheck(format!("File {:?} did not contain valid JSON", path)))
6963 }
7064}
0 commit comments