@@ -9,11 +9,10 @@ use std::{
99
1010use glob:: glob;
1111use serde:: { de:: DeserializeOwned , Serialize } ;
12- use serde_json:: json;
1312
1413use super :: { AdapterError , StoreAdapter } ;
1514
16- struct JsonFileStoreAdapter < K , V > {
15+ pub struct JsonFileStoreAdapter < K , V > {
1716 dirpath : PathBuf ,
1817 key : PhantomData < K > ,
1918 value : PhantomData < V > ,
2423 K : Hash + PartialEq + Serialize + DeserializeOwned ,
2524 V : Serialize + DeserializeOwned ,
2625{
27- fn create_dir ( dirpath : & PathBuf ) -> Result < ( ) , AdapterError > {
28- std:: fs:: create_dir_all ( dirpath)
29- . map_err ( |e| AdapterError :: InitializationError ( e. into ( ) ) ) ?;
30-
31- Ok ( ( ) )
32- }
3326 pub fn new ( dirpath : PathBuf ) -> Result < Self , AdapterError > {
3427 if !dirpath. exists ( ) {
3528 Self :: create_dir ( & dirpath) ?;
@@ -42,12 +35,18 @@ where
4235 } )
4336 }
4437
38+ fn create_dir ( dirpath : & PathBuf ) -> Result < ( ) , AdapterError > {
39+ fs:: create_dir_all ( dirpath) . map_err ( |e| AdapterError :: InitializationError ( e. into ( ) ) ) ?;
40+
41+ Ok ( ( ) )
42+ }
43+
4544 fn get_hash_from_key ( & self , key : & K ) -> String {
4645 let mut hasher = DefaultHasher :: new ( ) ;
4746 key. hash ( & mut hasher) ;
4847 let checksum = hasher. finish ( ) ;
4948
50- format ! ( "{:X }" , checksum)
49+ format ! ( "{:0>16X }" , checksum)
5150 }
5251
5352 fn get_filename_from_key ( & self , key : & K ) -> PathBuf {
@@ -117,6 +116,7 @@ where
117116 )
118117 }
119118}
119+
120120impl < K , V > StoreAdapter for JsonFileStoreAdapter < K , V >
121121where
122122 K : Hash + PartialEq + Serialize + DeserializeOwned ,
@@ -191,7 +191,7 @@ where
191191
192192#[ cfg( test) ]
193193mod tests {
194- use std :: io :: Read ;
194+ use serde_json :: json ;
195195 use std:: time:: Duration ;
196196
197197 use super :: * ;
@@ -274,6 +274,7 @@ mod tests {
274274
275275 assert ! ( adapter. store_record( & 1 , & record) . is_ok( ) ) ;
276276 assert_eq ! ( record, adapter. get_record( & 1 ) . unwrap( ) . unwrap( ) ) ;
277+ rmdir ( dir) ;
277278 }
278279
279280 #[ test]
@@ -285,5 +286,6 @@ mod tests {
285286
286287 assert ! ( adapter. store_record( & 1 , & record) . is_ok( ) ) ;
287288 assert_eq ! ( record, adapter. get_record( & 1 ) . unwrap( ) . unwrap( ) ) ;
289+ rmdir ( dir) ;
288290 }
289291}
0 commit comments