1
1
use crate :: error:: CkError ;
2
2
use serde_json:: Value ;
3
3
use std:: collections:: HashMap ;
4
- use std:: fs;
5
4
use std:: path:: { Path , PathBuf } ;
5
+ use std:: { fs, io} ;
6
6
7
7
#[ derive( Debug ) ]
8
8
pub struct Cache {
@@ -15,28 +15,25 @@ pub struct Cache {
15
15
impl Cache {
16
16
pub fn new ( doc_dir : & str ) -> Cache {
17
17
Cache {
18
- root : < str as AsRef < Path > > :: as_ref ( doc_dir) . to_owned ( ) ,
18
+ root : Path :: new ( doc_dir) . to_owned ( ) ,
19
19
files : HashMap :: new ( ) ,
20
20
values : HashMap :: new ( ) ,
21
21
last_path : None ,
22
22
}
23
23
}
24
24
25
- fn resolve_path ( & mut self , path : & String ) -> Result < PathBuf , CkError > {
25
+ fn resolve_path ( & mut self , path : & String ) -> PathBuf {
26
26
if path != "-" {
27
27
let resolve = self . root . join ( path) ;
28
28
self . last_path = Some ( resolve. clone ( ) ) ;
29
- Ok ( resolve)
29
+ resolve
30
30
} else {
31
- match & self . last_path {
32
- Some ( p) => Ok ( p. clone ( ) ) ,
33
- None => unreachable ! ( ) ,
34
- }
31
+ self . last_path . as_ref ( ) . unwrap ( ) . clone ( )
35
32
}
36
33
}
37
34
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) ;
40
37
41
38
if let Some ( f) = self . files . get ( & path) {
42
39
return Ok ( f. clone ( ) ) ;
@@ -47,24 +44,21 @@ impl Cache {
47
44
self . files . insert ( path, file. clone ( ) ) ;
48
45
49
46
Ok ( file)
50
- // Err(_) => Err(CkError::FailedCheck(format!("File {:?} does not exist / could not be opened", path)))
51
47
}
52
48
53
49
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) ;
55
51
56
52
if let Some ( v) = self . values . get ( & path) {
57
53
return Ok ( v. clone ( ) ) ;
58
54
}
59
55
60
56
let file = fs:: File :: open ( & path) ?;
61
- // Err(_) => return Err(CkError::FailedCheck(format!("File {:?} does not exist / could not be opened", path)))
62
57
63
58
let val = serde_json:: from_reader :: < _ , Value > ( file) ?;
64
59
65
60
self . values . insert ( path, val. clone ( ) ) ;
66
61
67
62
Ok ( val)
68
- // Err(_) => Err(CkError::FailedCheck(format!("File {:?} did not contain valid JSON", path)))
69
63
}
70
64
}
0 commit comments