1
1
use std:: { collections:: BTreeMap , fs:: File , io, path:: PathBuf } ;
2
2
3
3
use thiserror:: Error ;
4
- use xdg:: { BaseDirectories , BaseDirectoriesError } ;
5
4
6
5
use crate :: package_json:: PackageJson ;
7
6
@@ -29,15 +28,22 @@ pub type ReleaseTags = BTreeMap<ReleaseTag, PackageJson>;
29
28
impl Cache {
30
29
pub const CACHE_FILE : & str = "cache.json" ;
31
30
32
- pub fn cache_file ( ) -> Result < PathBuf , Error > {
33
- let xdg_dirs = BaseDirectories :: with_prefix ( env ! ( "CARGO_PKG_NAME" ) ) ?;
34
- let cache_file = xdg_dirs. place_cache_file ( Self :: CACHE_FILE ) ?;
31
+ pub fn cache_dir ( ) -> Result < PathBuf , Error > {
32
+ let cache_dir = dirs:: cache_dir ( )
33
+ . ok_or ( Error :: CacheDir ) ?
34
+ . join ( env ! ( "CARGO_PKG_NAME" ) ) ;
35
+
36
+ Ok ( cache_dir)
37
+ }
38
+
39
+ pub fn cache_file_path ( ) -> Result < PathBuf , Error > {
40
+ let cache_file = Self :: cache_dir ( ) ?. join ( Self :: CACHE_FILE ) ;
35
41
36
42
Ok ( cache_file)
37
43
}
38
44
39
45
pub fn read ( ) -> Result < Self , Error > {
40
- let cache_file = match File :: open ( Self :: cache_file ( ) ?) {
46
+ let cache_file = match File :: open ( Self :: cache_file_path ( ) ?) {
41
47
Ok ( f) => f,
42
48
Err ( _) => return Ok ( Default :: default ( ) ) ,
43
49
} ;
@@ -47,7 +53,9 @@ impl Cache {
47
53
}
48
54
49
55
pub fn save ( & self ) -> Result < ( ) , Error > {
50
- let cache_file = File :: create ( Self :: cache_file ( ) ?) ?;
56
+ std:: fs:: create_dir_all ( Self :: cache_dir ( ) ?) ?;
57
+
58
+ let cache_file = File :: create ( Self :: cache_file_path ( ) ?) ?;
51
59
serde_json:: to_writer ( cache_file, & self . 0 ) ?;
52
60
53
61
Ok ( ( ) )
@@ -59,8 +67,8 @@ impl Cache {
59
67
pub enum Error {
60
68
#[ error( "io error" ) ]
61
69
IO ( #[ from] io:: Error ) ,
62
- #[ error( "xdg base directories error" ) ]
63
- BaseDirectories ( # [ from ] BaseDirectoriesError ) ,
70
+ #[ error( "cache dir error" ) ]
71
+ CacheDir ,
64
72
#[ error( "serde error" ) ]
65
73
SerdeJson ( #[ from] serde_json:: Error ) ,
66
74
}
0 commit comments