Skip to content

Commit 44c208d

Browse files
committed
v2
1 parent 8b316b4 commit 44c208d

File tree

4 files changed

+140
-78
lines changed

4 files changed

+140
-78
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/target
2-
.vscode/
2+
.vscode/
3+
output.msgpack
4+
**.msgpack

Cargo.lock

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ edition = "2021"
66
[dependencies]
77
rmp-serde = "1.3.0"
88
serde = "1.0.219"
9+
walkdir = "2.5.0"
910

1011
[profile.custom-opt]
1112
inherits = "release"
1213
opt-level = 3
1314
lto = true
14-
codegen-units = 1
15+
codegen-units = 1

src/main.rs

Lines changed: 33 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use std::fs;
2-
use std::time::{Duration, Instant};
3-
use std::path::{PathBuf};
4-
use std::ffi::OsStr;
5-
6-
use rmp_serde::{encode};
7-
use std::collections::HashMap;
1+
use std::env;
82
use std::fs::File;
93
use std::io::Write;
4+
use std::path::{Path, PathBuf};
5+
use std::time::Instant;
6+
use std::collections::HashMap;
7+
use rmp_serde::encode;
8+
use walkdir::WalkDir;
109

1110
fn format_file_size(size: u64, unit: &str) -> String {
1211
match unit {
@@ -18,103 +17,61 @@ fn format_file_size(size: u64, unit: &str) -> String {
1817
}
1918
}
2019

21-
fn scan_dir(path: &str) -> Vec<PathBuf> {
22-
let mut result = Vec::new();
23-
if let Ok(entries) = fs::read_dir(path) {
24-
for entry in entries {
25-
if let Ok(entry) = entry {
26-
let path = entry.path();
27-
if path.is_dir() {
28-
// Récursivement scanner les sous-répertoires
29-
let mut sub_dir_results = scan_dir(path.to_str().unwrap());
30-
result.append(&mut sub_dir_results);
31-
} else {
32-
// Ajouter le fichier à la liste des résultats
33-
result.push(path);
34-
}
35-
}
36-
}
37-
}
38-
result
39-
}
40-
41-
fn scan_dirs(path: &str) -> Vec<PathBuf> {
42-
let result = scan_dir(path);
43-
result
20+
fn scan_dir(path: &Path) -> Vec<PathBuf> {
21+
WalkDir::new(path)
22+
.into_iter()
23+
.filter_map(|e| e.ok())
24+
.filter(|e| e.file_type().is_file())
25+
.map(|e| e.path().to_owned())
26+
.collect()
4427
}
4528

46-
fn parsing_to_hashmap(entries: &Vec<PathBuf>) -> Vec<HashMap<String, String>> {
47-
let mut dictionaries: Vec<HashMap<String, String>> = Vec::new();
48-
49-
for entry in entries {
29+
fn parsing_to_hashmap(entries: &[PathBuf]) -> Vec<HashMap<String, String>> {
30+
entries.iter().map(|entry| {
5031
let mut hashmap = HashMap::new();
51-
52-
// Convertir Option<&OsStr> en String pour "name"
53-
if let Some(name) = entry.file_name().and_then(OsStr::to_str) {
54-
hashmap.insert(String::from("name"), name.to_string());
55-
} else {
56-
hashmap.insert(String::from("name"), String::from(""));
57-
}
58-
59-
// Convertir Option<&OsStr> en String pour "ext"
60-
if let Some(ext) = entry.extension().and_then(OsStr::to_str) {
61-
hashmap.insert(String::from("ext"), ext.to_string());
62-
} else {
63-
hashmap.insert(String::from("ext"), String::from(""));
64-
}
65-
66-
// Convertir Option<&str> en String pour "path"
67-
if let Some(path) = entry.to_str() {
68-
hashmap.insert(String::from("path"), path.to_string());
69-
} else {
70-
hashmap.insert(String::from("path"), String::from(""));
71-
}
72-
73-
dictionaries.push(hashmap);
74-
}
75-
76-
dictionaries
32+
hashmap.insert("name".to_string(), entry.file_name().unwrap_or_default().to_string_lossy().into_owned());
33+
hashmap.insert("ext".to_string(), entry.extension().unwrap_or_default().to_string_lossy().into_owned());
34+
hashmap.insert("path".to_string(), entry.to_string_lossy().into_owned());
35+
hashmap
36+
}).collect()
7737
}
7838

79-
8039
fn main() {
81-
println!("1. Scanning folders...");
82-
let start_time: Instant = Instant::now();
40+
let args: Vec<String> = env::args().collect();
8341

84-
let paths: Vec<PathBuf> = scan_dirs("C:/Users/ederv/");
42+
if args.len() < 2 {
43+
eprintln!("Usage: {} <directory_path>", args[0]);
44+
std::process::exit(1);
45+
}
8546

86-
let end_time: Duration = start_time.elapsed();
47+
let target_dir = &args[1];
48+
println!("1. Scanning folders at: {}", target_dir);
49+
50+
let start_time = Instant::now();
51+
let paths = scan_dir(Path::new(target_dir));
52+
let end_time = start_time.elapsed();
8753

8854
println!("Time taken: {:?}", end_time);
8955
println!("Elements found: {:?}", paths.len());
9056

91-
if paths.len() > 0 {
57+
if !paths.is_empty() {
9258
let medium_time_per_element = end_time.as_nanos() / paths.len() as u128;
9359
println!("Medium time by element: {:?} ns", medium_time_per_element);
9460
} else {
9561
println!("No elements found, cannot calculate medium time by element.");
9662
}
9763

98-
//println!("2. Parsing to a list of dicts...");
9964
let hashmap = parsing_to_hashmap(&paths);
100-
101-
// Sérialiser le HashMap en MessagePack
10265
let encoded = encode::to_vec(&hashmap).unwrap();
10366

104-
// Écrire le MessagePack dans un fichier
10567
let mut file = File::create("output.msgpack").expect("Impossible de créer le fichier");
10668
file.write_all(&encoded).expect("Impossible d'écrire dans le fichier");
10769

108-
// Obtenir la taille du fichier
10970
let metadata = file.metadata().expect("Impossible d'obtenir les métadonnées du fichier");
11071
let file_size = metadata.len();
11172

112-
//println!("MessagePack sauvegardé dans output.msgpack");
113-
114-
// Afficher la taille du fichier dans différentes unités
11573
println!("Taille du fichier : {}", format_file_size(file_size, "o"));
11674
println!("Taille du fichier : {}", format_file_size(file_size, "Ko"));
11775
println!("Taille du fichier : {}", format_file_size(file_size, "Mo"));
11876
println!("Taille du fichier : {}", format_file_size(file_size, "Go"));
119-
120-
}
77+
}

0 commit comments

Comments
 (0)