1- use std:: path:: { Path , PathBuf } ;
2- use walkdir:: WalkDir ;
31use std:: collections:: HashMap ;
42use std:: fs:: File ;
53use std:: io:: Write ;
4+ use std:: path:: { Path , PathBuf } ;
5+ use walkdir:: WalkDir ;
66use zstd;
77
88/// TODO: Faire la doc
99pub fn serialize ( path : & Path ) {
10-
1110 // Step 1 : Getting all entities into the Path
1211 fn scan_dir ( path : & Path ) -> Vec < PathBuf > {
13- WalkDir :: new ( path)
14- . into_iter ( )
15- . filter_map ( |e| e. ok ( ) )
16- . filter ( |e| e. file_type ( ) . is_file ( ) )
17- . map ( |e| e. path ( ) . to_owned ( ) )
18- . collect ( )
12+ WalkDir :: new ( path)
13+ . into_iter ( )
14+ . filter_map ( |e| e. ok ( ) )
15+ . filter ( |e| e. file_type ( ) . is_file ( ) )
16+ . map ( |e| e. path ( ) . to_owned ( ) )
17+ . collect ( )
1918 }
2019
2120 // Step 2 : Creating a list of dictionary with all related and useful informations.
2221 fn parsing_to_hashmap ( entries : & [ PathBuf ] ) -> Vec < HashMap < String , String > > {
23- entries. iter ( ) . map ( |entry : & PathBuf | {
24- let mut hashmap: HashMap < String , String > = HashMap :: new ( ) ;
25- hashmap. insert ( "name" . to_string ( ) , entry. file_name ( ) . unwrap_or_default ( ) . to_string_lossy ( ) . into_owned ( ) ) ;
26- hashmap. insert ( "ext" . to_string ( ) , entry. extension ( ) . unwrap_or_default ( ) . to_string_lossy ( ) . into_owned ( ) ) ;
27- hashmap. insert ( "path" . to_string ( ) , entry. to_string_lossy ( ) . into_owned ( ) ) ;
28- hashmap
29- } ) . collect ( )
22+ entries
23+ . iter ( )
24+ . map ( |entry : & PathBuf | {
25+ let mut hashmap: HashMap < String , String > = HashMap :: new ( ) ;
26+ hashmap. insert (
27+ "name" . to_string ( ) ,
28+ entry
29+ . file_name ( )
30+ . unwrap_or_default ( )
31+ . to_string_lossy ( )
32+ . into_owned ( ) ,
33+ ) ;
34+ hashmap. insert (
35+ "ext" . to_string ( ) ,
36+ entry
37+ . extension ( )
38+ . unwrap_or_default ( )
39+ . to_string_lossy ( )
40+ . into_owned ( ) ,
41+ ) ;
42+ hashmap. insert ( "path" . to_string ( ) , entry. to_string_lossy ( ) . into_owned ( ) ) ;
43+ hashmap
44+ } )
45+ . collect ( )
3046 }
3147
3248 // Step 3 : We serializing the Vector to a message pack.
3349 fn serialize_to_msgpack ( hashmap : & Vec < HashMap < String , String > > ) -> File {
3450 let encoded: Vec < u8 > = rmp_serde:: encode:: to_vec ( & hashmap) . unwrap ( ) ;
35- let mut file: File = File :: create ( "output.msgpack.temp" ) . expect ( "Creation of the file impossible" ) ;
36- file. write_all ( & encoded) . expect ( "Writing of the file impossible" ) ;
51+ let mut file: File =
52+ File :: create ( "output.msgpack.temp" ) . expect ( "Creation of the file impossible" ) ;
53+ file. write_all ( & encoded)
54+ . expect ( "Writing of the file impossible" ) ;
3755 // We will rewriting the file using the file content but this technique isn't good for production
3856 // In the futur commit, TODO: replacing this with a stream flux and output into a file AFTER.
3957 return file;
@@ -50,18 +68,18 @@ pub fn serialize(path: &Path) {
5068 serialize_to_msgpack ( & hashmap) ;
5169
5270 // Ouvrir le fichier sérialisé en lecture
53- let mut uncompressed_file = File :: open ( "output.msgpack.temp" ) . expect ( "Failed to open serialized file" ) ;
71+ let mut uncompressed_file =
72+ File :: open ( "output.msgpack.temp" ) . expect ( "Failed to open serialized file" ) ;
5473
5574 // Ouvrir un fichier de sortie pour recevoir la version compressée
56- let output_file = File :: create ( "output.msgpack.zst" ) . expect ( "Failed to create output compressed file" ) ;
75+ let output_file =
76+ File :: create ( "output.msgpack.zst" ) . expect ( "Failed to create output compressed file" ) ;
5777
5878 // Compresser du fichier sérialisé vers le fichier compressé avec un niveau de compression à 22 (max)
59- zstd:: stream:: copy_encode ( & mut uncompressed_file, output_file, 22 )
60- . expect ( "Compression failed" ) ;
79+ zstd:: stream:: copy_encode ( & mut uncompressed_file, output_file, 22 ) . expect ( "Compression failed" ) ;
6180
6281 match std:: fs:: remove_file ( Path :: new ( "output.msgpack.temp" ) ) {
6382 Ok ( _) => println ! ( "Fichier supprimé avec succès !" ) ,
6483 Err ( e) => eprintln ! ( "Erreur lors de la suppression du fichier : {}" , e) ,
6584 }
66-
67- }
85+ }
0 commit comments