1- use std:: env;
2- use std:: fs:: File ;
3- use std:: io:: Write ;
4- use std:: path:: { Path , PathBuf } ;
5- use std:: time:: { Duration , Instant } ;
6- use std:: collections:: HashMap ;
7- use rmp_serde;
8- use walkdir:: WalkDir ;
9-
1+ use std:: path:: Path ;
102mod yadm;
11- use yadm:: report:: report:: Report ;
12-
13- fn scan_dir ( path : & Path ) -> Vec < PathBuf > {
14- WalkDir :: new ( path)
15- . into_iter ( )
16- . filter_map ( |e| e. ok ( ) )
17- . filter ( |e| e. file_type ( ) . is_file ( ) )
18- . map ( |e| e. path ( ) . to_owned ( ) )
19- . collect ( )
20- }
213
22- 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 ( )
30- }
314
325fn main ( ) {
33- let args: Vec < String > = env:: args ( ) . collect ( ) ;
34-
35- if args. len ( ) < 2 {
36- eprintln ! ( "Usage: {} <directory_path>" , args[ 0 ] ) ;
37- std:: process:: exit ( 1 ) ;
38- }
39-
40- let target_dir: & String = & args[ 1 ] ;
41-
42- println ! ( "1. Scanning folders at: {}" , target_dir) ;
43- let scan_start: Instant = Instant :: now ( ) ;
44- let paths: Vec < PathBuf > = scan_dir ( Path :: new ( target_dir) ) ;
45- let scan_duration: Duration = scan_start. elapsed ( ) ;
46-
47- let total_files: u64 =
48- if paths. is_empty ( ) {
49- eprintln ! ( "No files found. Exiting." ) ;
50- return ;
51- } else {
52- let count = paths. len ( ) as u64 ;
53- println ! ( "Found {} files" , count) ;
54- count
55- } ;
56-
57-
58- println ! ( "2. Parsing to hashmap..." ) ;
59- let hashmap_parsing_start: Instant = Instant :: now ( ) ;
60- let hashmap: Vec < HashMap < String , String > > = parsing_to_hashmap ( & paths) ;
61- let hashmap_parsing_duration: Duration = hashmap_parsing_start. elapsed ( ) ;
62-
63-
64- println ! ( "3. Encoding + writing MessagePack..." ) ;
65- let msgpack_parsing_start = Instant :: now ( ) ;
66-
67- let encoded: Vec < u8 > = rmp_serde:: encode:: to_vec ( & hashmap) . unwrap ( ) ;
68- let mut file: File = File :: create ( "output.msgpack" ) . expect ( "Impossible de créer le fichier" ) ;
69- file. write_all ( & encoded) . expect ( "Impossible d'écrire dans le fichier" ) ;
70-
71- let msgpack_parsing_duration: Duration = msgpack_parsing_start. elapsed ( ) ;
72-
73- let metadata: std:: fs:: Metadata = file. metadata ( ) . expect ( "Impossible d'obtenir les métadonnées du fichier" ) ;
74- let file_size: u64 = metadata. len ( ) ;
75-
76- let report: Report = Report {
77- target : args[ 0 ] . clone ( ) ,
78- scan_start_at : scan_start,
79- scan_duration : scan_duration,
80- elements_found : file_size,
81- hashmap_parsing_start_at : hashmap_parsing_start,
82- hashmap_parsing_duration : hashmap_parsing_duration,
83- msgpack_parsing_start_at : msgpack_parsing_start,
84- msgpack_parsing_duration : msgpack_parsing_duration,
85- output_file_size : file_size,
86- full_duration : scan_duration+hashmap_parsing_duration+msgpack_parsing_duration,
87- average_duration_by_file : file_size. checked_div ( scan_duration. as_secs ( ) +hashmap_parsing_duration. as_secs ( ) +msgpack_parsing_duration. as_secs ( ) ) . unwrap_or ( 0 ) ,
88- average_size_by_file : file_size. checked_div ( total_files) . unwrap_or ( 0 ) ,
89- } ;
90-
91- println ! ( "{}" , report)
6+ yadm:: serialize ( Path :: new ( "." ) )
927}
0 commit comments