Skip to content

Commit 031eb59

Browse files
committed
Added cli implementation
1 parent 0286639 commit 031eb59

File tree

7 files changed

+244
-24
lines changed

7 files changed

+244
-24
lines changed

Cargo.lock

Lines changed: 131 additions & 4 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
name = "yadm"
33
version = "0.1.0"
44
edition = "2021"
5+
authors = ["Dervaux Esteban <[email protected]>"]
56

67
[dependencies]
8+
clap = { version = "4.5.40", features = ["derive"] }
79
rmp-serde = "1.3.0"
810
serde = "1.0.219"
911
walkdir = "2.5.0"

src/cli.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use clap::{Parser, Subcommand, Args, ArgAction};
2+
3+
#[derive(Parser)]
4+
#[command(
5+
name = env!("CARGO_PKG_NAME"),
6+
version = env!("CARGO_PKG_VERSION"),
7+
author = env!("CARGO_PKG_AUTHORS"),
8+
about = "YADM is a lightweight mapping tool designed for small-scale projects. It allows you to serialize the structure of a file system directory into compact and optimized formats for fast storage and access."
9+
)]
10+
pub struct Cli {
11+
/// Active le mode verbeux
12+
#[arg(short, long, global = true)]
13+
pub verbose: bool,
14+
15+
#[command(subcommand)]
16+
pub command: Commands,
17+
}
18+
19+
#[derive(Subcommand)]
20+
pub enum Commands {
21+
/// Show the current version of YADM
22+
Version,
23+
24+
/// Serialize a directory or just one file
25+
Serialize(SerializeArgs),
26+
27+
/// Parse a serialize file
28+
Parse(ParseArgs),
29+
}
30+
31+
#[derive(Args)]
32+
pub struct SerializeArgs {
33+
/// Path to the element to serialize
34+
#[arg(value_name = "PATH")]
35+
pub path: String,
36+
37+
/// Enable or disable compression using zstd, (set to true by default)
38+
#[arg(long, action = ArgAction::Set, default_value_t = true)]
39+
pub use_zstd: bool,
40+
}
41+
42+
#[derive(Args)]
43+
pub struct ParseArgs {
44+
/// Path to the file to parse.
45+
#[arg(value_name = "PATH")]
46+
pub path: String,
47+
}

src/main.rs

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

4+
use std::path::Path;
5+
6+
use clap::Parser;
7+
use cli::{Cli, Commands};
8+
49
fn main() {
5-
yadm::serialize(Path::new("../"))
10+
let cli = Cli::parse();
11+
12+
match &cli.command {
13+
Commands::Version => {
14+
println!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
15+
}
16+
17+
Commands::Serialize(args) => {
18+
let path = &args.path;
19+
let use_zstd = args.use_zstd;
20+
let verbose = cli.verbose;
21+
22+
yadm::serialize(Path::new(path), use_zstd, verbose);
23+
}
24+
25+
Commands::Parse(args) => {
26+
let path = &args.path;
27+
let verbose = cli.verbose;
28+
29+
yadm::parse(Path::new(path), verbose);
30+
}
31+
}
632
}

src/yadm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
pub mod report;
22
pub mod serialize;
33
pub use serialize::serialize;
4+
pub mod parse;
5+
pub use parse::parse;

src/yadm/parse.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std::path::Path;
2+
3+
pub fn parse(_path: &Path, _verbose: bool) {
4+
eprintln!("Not implemented")
5+
}

src/yadm/serialize.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use zstd;
88
use super::report::Report;
99

1010
/// TODO: Faire la doc
11-
pub fn serialize(path: &Path) {
11+
pub fn serialize(path: &Path, use_zstd: bool, verbose: bool) {
1212
// Step 1 : Getting all entities into the Path
1313
fn scan_dir(path: &Path) -> Vec<PathBuf> {
1414
WalkDir::new(path)
@@ -19,7 +19,7 @@ pub fn serialize(path: &Path) {
1919
.collect()
2020
}
2121

22-
// Step 2 : Creating a list of dictionary with all related and useful informations.
22+
// Step 2 : Creating a list of dictionary with all related and useful information.
2323
fn parsing_to_hashmap(entries: &[PathBuf]) -> Vec<HashMap<String, String>> {
2424
entries
2525
.iter()
@@ -51,15 +51,15 @@ pub fn serialize(path: &Path) {
5151
fn serialize_to_msgpack(hashmap: &Vec<HashMap<String, String>>) -> File {
5252
let encoded: Vec<u8> = rmp_serde::encode::to_vec(&hashmap).unwrap();
5353
let mut file: File =
54-
File::create("output.msgpack.temp").expect("Creation of the file impossible");
54+
File::create("map.msgpack.temp").expect("Creation of the file impossible");
5555
file.write_all(&encoded)
5656
.expect("Writing of the file impossible");
5757
// We will rewriting the file using the file content but this technique isn't good for production
5858
// In the futur commit, TODO: replacing this with a stream flux and output into a file AFTER.
5959
return file;
6060
}
6161

62-
// Report give some informations to know how good or bad this version of YADM was.
62+
// Report give some information to know how good or bad this version of YADM was.
6363
let mut report: Report = Report::new(path.to_path_buf());
6464

6565
// We check if the path go to only one File to skip the WalkDir call if not needed.
@@ -85,24 +85,35 @@ pub fn serialize(path: &Path) {
8585
serialize_to_msgpack(&hashmap);
8686
report.time_msgpack_parsing();
8787

88-
// Ouvrir le fichier sérialisé en lecture
89-
let mut uncompressed_file =
90-
File::open("output.msgpack.temp").expect("Failed to open serialized file");
88+
if use_zstd {
89+
// Ouvrir le fichier sérialisé en lecture
90+
let mut uncompressed_file =
91+
File::open("map.msgpack.temp").expect("Failed to open serialized file");
9192

92-
// Ouvrir un fichier de sortie pour recevoir la version compressée
93-
let output_file =
94-
File::create("output.msgpack.zst").expect("Failed to create output compressed file");
93+
// Ouvrir un fichier de sortie pour recevoir la version compressée
94+
let output_file =
95+
File::create("map.msgpack.zst").expect("Failed to create output compressed file");
9596

96-
report.time_zstd_compression();
97-
// Compresser du fichier sérialisé vers le fichier compressé avec un niveau de compression à 22 (max)
98-
zstd::stream::copy_encode(&mut uncompressed_file, output_file, 22).expect("Compression failed");
99-
report.time_zstd_compression();
97+
report.time_zstd_compression();
98+
// Compresser du fichier sérialisé vers le fichier compressé avec un niveau de compression à 22 (max)
99+
zstd::stream::copy_encode(&mut uncompressed_file, output_file, 22).expect("Compression failed");
100+
report.time_zstd_compression();
100101

101-
match std::fs::remove_file(Path::new("output.msgpack.temp")) {
102-
Ok(_) => println!("Fichier supprimé avec succès !"),
103-
Err(e) => eprintln!("Erreur lors de la suppression du fichier : {}", e),
102+
match std::fs::remove_file(Path::new("map.msgpack.temp")) {
103+
Ok(_) => println!("Fichier supprimé avec succès !"),
104+
Err(e) => eprintln!("Erreur lors de la suppression du fichier : {}", e),
105+
}
106+
} else {
107+
match std::fs::rename(Path::new("map.msgpack.temp"), Path::new("map.msgpack")) {
108+
Ok(_) => println!("Fichier supprimé avec succès !"),
109+
Err(e) => eprintln!("Erreur lors de la suppression du fichier : {}", e),
110+
}
104111
}
112+
105113

106114
report.close_report();
107-
println!("{}", report);
115+
if verbose {
116+
// FIXME: In the futur, we need to implement a dummy report struct to skip all timers when "verbose" is set to false
117+
println!("{}", report);
118+
}
108119
}

0 commit comments

Comments
 (0)