Skip to content

Commit 33337ee

Browse files
committed
Cleaning files, changed how yadm mod is integrate
1 parent 124a953 commit 33337ee

File tree

4 files changed

+49
-95
lines changed

4 files changed

+49
-95
lines changed

Cargo.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,4 @@ edition = "2021"
66
[dependencies]
77
rmp-serde = "1.3.0"
88
serde = "1.0.219"
9-
walkdir = "2.5.0"
10-
11-
[profile.custom-opt]
12-
inherits = "release"
13-
opt-level = 3
14-
lto = true
15-
codegen-units = 1
9+
walkdir = "2.5.0"

src/main.rs

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,7 @@
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;
102
mod 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

325
fn 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
}

src/yadm.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
pub mod report;
1+
pub mod report;
2+
pub use report::report::Report;
3+
4+
pub mod serialize;
5+
pub use serialize::serialize;

src/yadm/serialize.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::path::{Path, PathBuf};
2+
use walkdir::WalkDir;
3+
use std::collections::HashMap;
4+
use std::fs::File;
5+
use std::io::Write;
6+
7+
/// TODO: Faire la doc
8+
pub fn serialize(path: &Path) {
9+
10+
// Step 1 : Getting all entities into the Path
11+
fn scan_dir(path: &Path) -> Vec<PathBuf> {
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()
18+
}
19+
20+
// Step 2 : Creating a list of dictionary with all related and useful informations.
21+
fn parsing_to_hashmap(entries: &[PathBuf]) -> Vec<HashMap<String, String>> {
22+
entries.iter().map(|entry: &PathBuf| {
23+
let mut hashmap: HashMap<String, String> = HashMap::new();
24+
hashmap.insert("name".to_string(), entry.file_name().unwrap_or_default().to_string_lossy().into_owned());
25+
hashmap.insert("ext".to_string(), entry.extension().unwrap_or_default().to_string_lossy().into_owned());
26+
hashmap.insert("path".to_string(), entry.to_string_lossy().into_owned());
27+
hashmap
28+
}).collect()
29+
}
30+
31+
// We check if the path go to only one File to skip the WalkDir call if not needed.
32+
let hashmap: Vec<HashMap<String, String>> = if path.is_file() {
33+
parsing_to_hashmap(&[path.to_path_buf()])
34+
} else {
35+
parsing_to_hashmap(&scan_dir(path))
36+
};
37+
38+
let encoded: Vec<u8> = rmp_serde::encode::to_vec(&hashmap).unwrap();
39+
let mut file: File = File::create("output.msgpack").expect("Creation of the file impossible");
40+
file.write_all(&encoded).expect("Writing of the file impossible");
41+
}

0 commit comments

Comments
 (0)