Skip to content

Commit

Permalink
Version 0.1.2
Browse files Browse the repository at this point in the history
- Exclude common binary file patterns by default
- Add setting to ignore non-utf8 files (default true)
- Add setting to show files with no BIDI chars found (default true)
  • Loading branch information
maweil committed Nov 9, 2021
1 parent 788773d commit 81d62f5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bidi_detector"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 4 additions & 2 deletions bidi_config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[general]
includes = [
"src/*/**",
"**/*.rs",
"**/*.patch",
"**/*.json",
"**/Dockerfile",
Expand All @@ -12,4 +12,6 @@ excludes = [
]

[display]
show_details = true
show_details = true
verbose = true
ignore_invalid_data = false
127 changes: 87 additions & 40 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,9 @@ use glob::glob;
use glob::Pattern;
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::ErrorKind;
use std::process::exit;
const CONFIG_PATH: &str = "bidi_config.toml";
const DEFAULT_CONFIG: &str = r#"
[general]
includes = [
"**/*",
]
excludes = [
]
[display]
show_details = true"#;

#[derive(Serialize, Deserialize)]
struct GeneralSettings {
includes: Vec<String>,
Expand All @@ -26,6 +16,8 @@ struct GeneralSettings {
#[derive(Serialize, Deserialize)]
struct DisplaySettings {
show_details: bool,
ignore_invalid_data: Option<bool>,
verbose: Option<bool>,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -34,17 +26,48 @@ struct Config {
display: DisplaySettings,
}

fn get_default_config() -> Config {
Config {
general: GeneralSettings {
includes: vec![String::from("**/*")],
excludes: vec![
String::from("**/*.jpg"),
String::from("**/*.png"),
String::from("**/*.gif"),
String::from("**/*.zip"),
String::from("**/*.tar"),
String::from("**/*.gz"),
String::from("**/*.bz2"),
String::from("**/*.7z"),
String::from("**/*.class"),
String::from("**/*.rlib"),
String::from("**/*.so"),
String::from("**/.git/*"),
],
},
display: DisplaySettings {
show_details: true,
ignore_invalid_data: Option::from(true),
verbose: Option::from(true),
},
}
}

fn read_config(config_file_path: &str) -> Config {
let config_content: String =
fs::read_to_string(&config_file_path).unwrap_or_else(|_| String::from(DEFAULT_CONFIG));
let decoded: Config = toml::from_str(&config_content).unwrap();
decoded
let config_file = fs::read_to_string(&config_file_path);
match config_file {
Ok(file_content) => toml::from_str(&file_content).expect("Invalid config file content."),
Err(_e) => {
println!("Using default configuration");
get_default_config()
}
}
}

fn matches_exclude(exclude_patterns: &[String], test_str: &str) -> bool {
for exclude in exclude_patterns {
let compiled_pattern: Pattern =
Pattern::new(exclude).expect("Could not compile exclude pattern!");
Pattern::new(exclude).expect("Could not compile exclude pattern.");
if compiled_pattern.matches(test_str) {
return true;
}
Expand All @@ -53,7 +76,13 @@ fn matches_exclude(exclude_patterns: &[String], test_str: &str) -> bool {
}

fn check_files(config: &Config) -> u64 {
let default_display_settings: DisplaySettings = get_default_config().display;
let general_config = &config.general;
let verbose_output: bool = config
.display
.verbose
.unwrap_or_else(|| default_display_settings.verbose.unwrap());
let show_occurence_details = config.display.show_details;
let mut bidi_chars_found_overall: u64 = 0;
for include in &general_config.includes {
for entry in glob(include).expect("Failed to compile include pattern") {
Expand All @@ -62,29 +91,43 @@ fn check_files(config: &Config) -> u64 {
if path.is_file() {
let display_path = path.display().to_string();
if !(matches_exclude(&general_config.excludes, &display_path)) {
let content: String =
fs::read_to_string(path).expect("Could not read file content");
let result = check_for_bidi_chars(&content);
if result.contains_bidi_chars {
bidi_chars_found_overall += result.occurences.len() as u64;
}
println!(
"{} - {} BIDI characters",
&display_path,
result.occurences.len()
);
if config.display.show_details {
for occurence in &result.occurences {
let char_detail =
get_char_detail(&occurence.found_char).unwrap();
eprintln!(
"Found character {} ({}), {}:{}:{}",
char_detail.abbreviation,
char_detail.name,
&display_path,
occurence.line,
occurence.char_pos
)
let read_str_res = fs::read_to_string(path);
match read_str_res {
Ok(content) => {
let result = check_for_bidi_chars(&content);
let num_occurences: u64 = result.occurences.len() as u64;
if result.contains_bidi_chars {
bidi_chars_found_overall += num_occurences;
}
if num_occurences > 0 || verbose_output {
println!(
"{} - {} BIDI characters",
&display_path, num_occurences
);
}
if show_occurence_details {
for occurence in &result.occurences {
let char_detail =
get_char_detail(&occurence.found_char).unwrap();
eprintln!(
"Found character {} ({}), {}:{}:{}",
char_detail.abbreviation,
char_detail.name,
&display_path,
occurence.line,
occurence.char_pos
)
}
}
}
Err(e) => {
let ignore_invalid_data: bool =
config.display.ignore_invalid_data.unwrap_or_else(|| {
default_display_settings.ignore_invalid_data.unwrap()
});
if e.kind() != ErrorKind::InvalidData || !ignore_invalid_data {
eprintln!("Could not read file {} - {}", &display_path, e);
}
}
}
}
Expand Down Expand Up @@ -121,7 +164,11 @@ mod tests {
includes: vec![String::from("test/*.js"), String::from("src/*")],
excludes: vec![],
},
display: DisplaySettings { show_details: true },
display: DisplaySettings {
show_details: true,
ignore_invalid_data: Option::from(true),
verbose: Option::from(false),
},
};
let result = check_files(&config);
assert_eq!(result, 6);
Expand Down

0 comments on commit 81d62f5

Please sign in to comment.