Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Feb 13, 2024
1 parent 3032be4 commit 9f11868
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 183 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ cargo install comrak
mkdir -p dist
cp -r target/llvm-cov/html dist/coverage
sed 's|CHANGELOG.md|CHANGELOG.html|g' README.md >README.md.tmp
comrak --gfm -o dist/index.html --width 10 README.md.tmp
comrak --gfm -o dist/CHANGELOG.html --width 10 CHANGELOG.md
comrak --gfm -o dist/index.html --width 80 README.md.tmp
comrak --gfm -o dist/CHANGELOG.html --width 80 CHANGELOG.md

# cleanup
rm README.md.tmp
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ cargo test -- --show-output
cargo test <test_name>
```

## 2024-02-13

- better separation of options - `VerbosityFields` and `OptionsFields`

## 2024-01-25

- Add tests
Expand Down
91 changes: 49 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,44 @@ use std::{
};

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// Contains informations about json options
pub struct JsonFields {
/// Contains informations about verbosity options
pub struct VerbosityFields {
/// if true, the program will print json
pub json: bool,
/// if true, the program will print json with pretty print
pub json_pretty: bool,
/// if true, the program will only print errors as json
pub json_error: bool,
/// verbosity of the program
pub verbose: bool,
}

#[derive(Debug)]
/// contains informations about cleaning options
pub struct OptionsFields {
/// if true, the program will not rename files
pub dry_run: bool,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// Options for the program
pub struct OptionnalFields {
/// if true, the program will not rename files
pub dry_run: bool,
/// verbosity of the program
pub verbose: bool,
pub options: OptionsFields,
/// contains informations about JsonFields
pub json: JsonFields,
pub verbosity: VerbosityFields,
}

impl PartialEq<OptionnalFields> for OptionnalFields {
fn eq(&self, other: &OptionnalFields) -> bool {
self.dry_run == other.dry_run
&& self.verbose == other.verbose
&& self.json.json == other.json.json
&& self.json.json_pretty == other.json.json_pretty
&& self.json.json_error == other.json.json_error
self.options.dry_run == other.options.dry_run
&& self.verbosity.verbose == other.verbosity.verbose
&& self.verbosity.json == other.verbosity.json
&& self.verbosity.json_pretty == other.verbosity.json_pretty
&& self.verbosity.json_error == other.verbosity.json_error
}
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
/// Contains informations about a result of a single file
pub struct CustomSingleResult {
Expand Down Expand Up @@ -231,7 +235,7 @@ pub fn check_similar(vector: Vec<u8>, name_acc: &mut String, last_was_under: boo
return false;
}

fn clean_name(path: &OsStr, _options: &OptionnalFields) -> OsString {
fn clean_name(path: &OsStr, _options: &OptionsFields) -> OsString {
// for each byte of the path if it's not ascii, replace it with _
let mut new_name = String::new();
let mut vec_grapheme = Vec::with_capacity(4);
Expand Down Expand Up @@ -292,7 +296,7 @@ fn clean_name(path: &OsStr, _options: &OptionnalFields) -> OsString {
return OsString::from(new_name);
}

fn clean_path(file_path: &PathBuf, options: &OptionnalFields) -> CustomSingleResult {
fn clean_path(file_path: &PathBuf, options: &OptionsFields) -> CustomSingleResult {
let file_name = file_path.file_name();
if file_name.is_none() {
return CustomSingleResult {
Expand Down Expand Up @@ -349,7 +353,7 @@ fn is_directory_entry(entry: &DirEntry) -> bool {
}
}

fn clean_directory(dir_path: &PathBuf, options: &OptionnalFields) -> Vec<CustomSingleResult> {
fn clean_directory(dir_path: &PathBuf, options: &OptionsFields) -> Vec<CustomSingleResult> {
let mut dir_path = dir_path.clone();
let mut res = Vec::new();
let res_dir = clean_path(&dir_path, &options);
Expand Down Expand Up @@ -388,7 +392,7 @@ fn clean_directory(dir_path: &PathBuf, options: &OptionnalFields) -> Vec<CustomS
return res;
}

fn clean(path: PathBuf, options: &OptionnalFields) -> Vec<CustomSingleResult> {
fn clean(path: PathBuf, options: &OptionsFields) -> Vec<CustomSingleResult> {
if is_directory(&path) {
return clean_directory(&path, &options);
} else {
Expand Down Expand Up @@ -418,11 +422,6 @@ fn show_version() {

/// Parse the arguments and return the options and the paths to check
pub fn parse_args(args: Vec<String>) -> Result<(OptionnalFields, Vec<PathBuf>), i32> {
if args.len() == 1 {
println!("You need to provide at least one path");
return Err(1);
}

let mut dry_run = true;
let mut verbose = true;
let mut json = false;
Expand Down Expand Up @@ -462,18 +461,14 @@ pub fn parse_args(args: Vec<String>) -> Result<(OptionnalFields, Vec<PathBuf>),
}
}
if path_to_check.len() == 0 {
if verbose {
println!("You need to provide at least one valid path !");
} else if json {
println!(r#"{{"error": "You need to provide at least one path"}}"#);
}
return Err(1);
let paths = get_path_of_dir(".");
path_to_check.extend(paths);
}
return Ok((
OptionnalFields {
dry_run,
verbose,
json: JsonFields {
options: OptionsFields { dry_run },
verbosity: VerbosityFields {
verbose,
json,
json_pretty,
json_error,
Expand All @@ -485,7 +480,7 @@ pub fn parse_args(args: Vec<String>) -> Result<(OptionnalFields, Vec<PathBuf>),

/// Print the output of the program conforming to the options
pub fn print_output(
options: &OptionnalFields,
options: &VerbosityFields,
final_res: Vec<CustomSingleResult>,
) -> Result<(), i32> {
if options.verbose {
Expand All @@ -509,10 +504,10 @@ pub fn print_output(
} else {
println!("{} files checked", len);
}
} else if options.json.json {
} else if options.json {
#[cfg(feature = "serde")]
{
let vec_to_json = if options.json.json_error {
let vec_to_json = if options.json_error {
let mut vec_to_json: Vec<CustomSingleResult> = Vec::new();
for one_res in final_res {
if one_res.error.is_some() {
Expand All @@ -523,7 +518,7 @@ pub fn print_output(
} else {
final_res
};
let json_string = if options.json.json_pretty {
let json_string = if options.json_pretty {
serde_json::to_string_pretty(&vec_to_json)
} else {
serde_json::to_string(&vec_to_json)
Expand All @@ -539,18 +534,30 @@ pub fn print_output(
Ok(())
}

/// main function of the program
pub fn notox(options: &OptionnalFields, paths_to_check: Vec<PathBuf>) -> Vec<CustomSingleResult> {
if options.verbose {
println!("Running with options: {:?}", &options);
/// Do the program, return the Vector of result
pub fn notox(
full_options: &OptionnalFields,
paths_to_check: Vec<PathBuf>,
) -> Vec<CustomSingleResult> {
if full_options.verbosity.verbose {
println!("Running with options: {:?}", &full_options);
}
let mut final_res = Vec::new();
for one_path in paths_to_check {
if options.verbose {
if full_options.verbosity.verbose {
println!("Checking: {:?}", one_path);
}
let one_res = clean(one_path, &options);
let one_res = clean(one_path, &full_options.options);
final_res.extend(one_res);
}
return final_res;
}

/// main function of the program: clean and print the output
pub fn notox_full(full_options: &OptionnalFields, paths_to_check: Vec<PathBuf>) -> i32 {
let final_res = notox(&full_options, paths_to_check);
if let Err(code) = print_output(&full_options.verbosity, final_res) {
return code;
}
0
}
11 changes: 4 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use notox::{notox, parse_args, print_output};
use notox::{notox_full, parse_args};

fn main() -> Result<(), ()> {
let args: Vec<String> = std::env::args().collect();
let parsed_args = parse_args(args);
if let Err(code) = parsed_args {
std::process::exit(code);
} else if let Ok((options, paths_to_check)) = parsed_args {
let final_res = notox(&options, paths_to_check);
if let Err(code) = print_output(&options, final_res) {
std::process::exit(code);
}
}
Ok(())
let (options, paths_to_check) = parsed_args.unwrap();
let result_code = notox_full(&options, paths_to_check);
std::process::exit(result_code);
}
18 changes: 8 additions & 10 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
mod tests {
use std::{os::unix::fs::PermissionsExt, path::PathBuf};

use notox::OptionnalFields;

#[test]
fn test_parse_args_clean_directory() {
let dir = "src".to_owned();
Expand All @@ -18,10 +16,10 @@ mod tests {
.count();
assert_eq!(
options,
OptionnalFields {
dry_run: true,
verbose: false,
json: notox::JsonFields {
notox::OptionnalFields {
options: notox::OptionsFields { dry_run: true },
verbosity: notox::VerbosityFields {
verbose: false,
json: true,
json_pretty: false,
json_error: false,
Expand Down Expand Up @@ -105,10 +103,10 @@ mod tests {

assert_eq!(
options,
OptionnalFields {
dry_run: false,
verbose: false,
json: notox::JsonFields {
notox::OptionnalFields {
options: notox::OptionsFields { dry_run: false },
verbosity: notox::VerbosityFields {
verbose: false,
json: true,
json_pretty: false,
json_error: false,
Expand Down
Loading

0 comments on commit 9f11868

Please sign in to comment.