@@ -5,17 +5,20 @@ use std::path::PathBuf;
55#[ derive( Debug ) ]
66pub struct Config {
77 pub pantry_dir : PathBuf ,
8+ pub pantry_db_file : PathBuf ,
89 pub dist_url : String ,
910 pub pkgx_dir : PathBuf ,
1011}
1112
1213impl Config {
1314 pub fn new ( ) -> io:: Result < Self > {
1415 let pantry_dir = get_pantry_dir ( ) ?;
16+ let pantry_db_file: PathBuf = get_pantry_db_file ( ) ?;
1517 let dist_url = get_dist_url ( ) ;
1618 let pkgx_dir = get_pkgx_dir ( ) ?;
1719 Ok ( Self {
1820 pantry_dir,
21+ pantry_db_file,
1922 dist_url,
2023 pkgx_dir,
2124 } )
@@ -29,16 +32,33 @@ fn get_dist_url() -> String {
2932 env ! ( "PKGX_DIST_URL" ) . to_string ( )
3033}
3134
32- fn get_pantry_dir ( ) -> io:: Result < PathBuf > {
35+ #[ allow( non_snake_case) ]
36+ fn get_PKGX_PANTRY_DIR ( ) -> Option < PathBuf > {
3337 if let Ok ( env_dir) = env:: var ( "PKGX_PANTRY_DIR" ) {
3438 let path = PathBuf :: from ( env_dir) ;
35- if !path. is_absolute ( ) {
36- return Ok ( env:: current_dir ( ) ?. join ( path) ) ;
39+ if path. is_absolute ( ) {
40+ Some ( path)
41+ } else if let Ok ( cwd) = env:: current_dir ( ) {
42+ Some ( cwd. join ( path) )
3743 } else {
38- return Ok ( path ) ;
44+ None
3945 }
46+ } else {
47+ None
48+ }
49+ }
50+
51+ fn get_pantry_dir ( ) -> io:: Result < PathBuf > {
52+ if let Some ( path) = get_PKGX_PANTRY_DIR ( ) {
53+ Ok ( path)
54+ } else if let Some ( path) = dirs_next:: data_local_dir ( ) {
55+ Ok ( path. join ( "pkgx/pantry" ) )
56+ } else {
57+ Err ( io:: Error :: new (
58+ io:: ErrorKind :: NotFound ,
59+ "Could not determine cache directory" ,
60+ ) )
4061 }
41- Ok ( dirs_next:: cache_dir ( ) . unwrap ( ) . join ( "pkgx/pantry" ) )
4262}
4363
4464fn get_pkgx_dir ( ) -> io:: Result < PathBuf > {
@@ -59,3 +79,16 @@ fn get_pkgx_dir() -> io::Result<PathBuf> {
5979 Ok ( default. unwrap ( ) )
6080 }
6181}
82+
83+ fn get_pantry_db_file ( ) -> io:: Result < PathBuf > {
84+ if let Some ( path) = get_PKGX_PANTRY_DIR ( ) {
85+ Ok ( path. join ( "pantry.2.db" ) )
86+ } else if let Some ( path) = dirs_next:: cache_dir ( ) {
87+ Ok ( path. join ( "pkgx/pantry.2.db" ) )
88+ } else {
89+ Err ( io:: Error :: new (
90+ io:: ErrorKind :: NotFound ,
91+ "Could not determine data directory" ,
92+ ) )
93+ }
94+ }
0 commit comments