Skip to content

Commit e4a964f

Browse files
committed
Used rustfmt and adding the tool to the github workflow
1 parent 52516fa commit e4a964f

File tree

5 files changed

+76
-33
lines changed

5 files changed

+76
-33
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Rustfmt Check
2+
3+
on:
4+
push:
5+
branches:
6+
- '**' # Toutes les branches
7+
8+
jobs:
9+
rustfmt:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v3
15+
16+
- name: Install Rust toolchain
17+
run: |
18+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
19+
source $HOME/.cargo/env
20+
rustup component add rustfmt
21+
22+
- name: Run rustfmt check
23+
run: |
24+
source $HOME/.cargo/env
25+
cargo fmt -- --check

src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::path::Path;
22
mod yadm;
33

4-
54
fn main() {
6-
yadm::serialize(Path::new("."))
7-
}
5+
yadm::serialize(Path::new("."))
6+
}

src/yadm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ pub mod report;
22
pub use report::report::Report;
33

44
pub mod serialize;
5-
pub use serialize::serialize;
5+
pub use serialize::serialize;

src/yadm/report.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ pub mod report {
2424

2525
impl fmt::Display for Report {
2626
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
write!(f, "Scan of {} found {} elements in {:?}",
28-
self.target,
29-
self.elements_found,
30-
self.full_duration
27+
write!(
28+
f,
29+
"Scan of {} found {} elements in {:?}",
30+
self.target, self.elements_found, self.full_duration
3131
)?;
3232

3333
write!(
34-
f, "
34+
f,
35+
"
3536
=== YADM report ===
3637
Target : {}
3738
Scan duration : {:.2}s

src/yadm/serialize.rs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,57 @@
1-
use std::path::{Path, PathBuf};
2-
use walkdir::WalkDir;
31
use std::collections::HashMap;
42
use std::fs::File;
53
use std::io::Write;
4+
use std::path::{Path, PathBuf};
5+
use walkdir::WalkDir;
66
use zstd;
77

88
/// TODO: Faire la doc
99
pub 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

Comments
 (0)