Skip to content

Commit

Permalink
Code formatting and documentation (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu-LAURENT39 authored May 12, 2024
1 parent 95789b8 commit ab3f2ca
Show file tree
Hide file tree
Showing 37 changed files with 3,639 additions and 2,176 deletions.
111 changes: 65 additions & 46 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Handles configuration loading and saving
use std::fs;
use std::io::{Read, Write};
use std::path::PathBuf;
Expand All @@ -18,25 +20,25 @@ macro_rules! vec_of_strings {

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Config {
pub no_color : Option<bool>,
pub no_color: Option<bool>,
pub gitignore: Option<bool>,
pub hgignore: Option<bool>,
pub dockerignore: Option<bool>,
pub is_zip_archive : Option<Vec<String>>,
pub is_archive : Option<Vec<String>>,
pub is_audio : Option<Vec<String>>,
pub is_book : Option<Vec<String>>,
pub is_doc : Option<Vec<String>>,
pub is_font : Option<Vec<String>>,
pub is_image : Option<Vec<String>>,
pub is_source : Option<Vec<String>>,
pub is_video : Option<Vec<String>>,
pub default_file_size_format : Option<String>,
pub is_zip_archive: Option<Vec<String>>,
pub is_archive: Option<Vec<String>>,
pub is_audio: Option<Vec<String>>,
pub is_book: Option<Vec<String>>,
pub is_doc: Option<Vec<String>>,
pub is_font: Option<Vec<String>>,
pub is_image: Option<Vec<String>>,
pub is_source: Option<Vec<String>>,
pub is_video: Option<Vec<String>>,
pub default_file_size_format: Option<String>,
pub check_for_updates: Option<bool>,
#[serde(skip_serializing, default = "get_false")]
pub debug : bool,
pub debug: bool,
#[serde(skip)]
save : bool,
save: bool,
}

fn get_false() -> bool {
Expand Down Expand Up @@ -68,13 +70,10 @@ impl Config {
}

pub fn from(config_file: PathBuf) -> Result<Config, String> {
if let Ok(mut file) = fs::File::open(&config_file) {
if let Ok(mut file) = fs::File::open(config_file) {
let mut contents = String::new();
if let Ok(_) = file.read_to_string(&mut contents) {
match toml::from_str(&contents) {
Ok(config) => Ok(config),
Err(err) => Err(err.to_string())
}
if file.read_to_string(&mut contents).is_ok() {
toml::from_str(&contents).map_err(|err| err.to_string())
} else {
Err("Could not read config file. Using default settings.".to_string())
}
Expand All @@ -97,18 +96,13 @@ impl Config {

#[cfg(not(windows))]
fn get_project_dir() -> Option<PathBuf> {
match ProjectDirs::from("", ORGANIZATION, APPLICATION) {
Some(pd) => Some(pd.config_dir().to_path_buf()),
_ => None
}
ProjectDirs::from("", ORGANIZATION, APPLICATION).map(|pd| pd.config_dir().to_path_buf())
}

#[cfg(windows)]
fn get_project_dir() -> Option<PathBuf> {
match ProjectDirs::from("", ORGANIZATION, APPLICATION) {
Some(pd) => Some(pd.config_dir().parent().unwrap().to_path_buf()),
_ => None
}
ProjectDirs::from("", ORGANIZATION, APPLICATION)
.map(|pd| pd.config_dir().parent().unwrap().to_path_buf())
}

pub fn save(&self) {
Expand All @@ -133,29 +127,54 @@ impl Config {
let toml = toml::to_string_pretty(&self).unwrap();

if let Ok(mut file) = fs::File::create(&config_file) {
let _ = file.write_all(&toml.as_bytes());
let _ = file.write_all(toml.as_bytes());
}
}

pub fn default() -> Config {
Config {
no_color : Some(false),
gitignore : Some(false),
hgignore : Some(false),
dockerignore : Some(false),
is_zip_archive : vec_of_strings![".zip", ".jar", ".war", ".ear"],
is_archive : vec_of_strings![".7z", ".bz2", ".bzip2", ".gz", ".gzip", ".lz", ".rar", ".tar", ".xz", ".zip"],
is_audio : vec_of_strings![".aac", ".aiff", ".amr", ".flac", ".gsm", ".m4a", ".m4b", ".m4p", ".mp3", ".ogg", ".wav", ".wma"],
is_book : vec_of_strings![".azw3", ".chm", ".djv", ".djvu", ".epub", ".fb2", ".mobi", ".pdf"],
is_doc : vec_of_strings![".accdb", ".doc", ".docm", ".docx", ".dot", ".dotm", ".dotx", ".mdb", ".odp", ".ods", ".odt", ".pdf", ".potm", ".potx", ".ppt", ".pptm", ".pptx", ".rtf", ".xlm", ".xls", ".xlsm", ".xlsx", ".xlt", ".xltm", ".xltx", ".xps"],
is_font : vec_of_strings![".eot", ".fon", ".otc", ".otf", ".ttc", ".ttf", ".woff", ".woff2"],
is_image : vec_of_strings![".bmp", ".exr", ".gif", ".heic", ".jpeg", ".jpg", ".jxl", ".png", ".psb", ".psd", ".svg", ".tga", ".tiff", ".webp"],
is_source : vec_of_strings![".asm", ".bas", ".c", ".cc", ".ceylon", ".clj", ".coffee", ".cpp", ".cs", ".d", ".dart", ".elm", ".erl", ".go", ".gradle", ".groovy", ".h", ".hh", ".hpp", ".java", ".jl", ".js", ".jsp", ".jsx", ".kt", ".kts", ".lua", ".nim", ".pas", ".php", ".pl", ".pm", ".py", ".rb", ".rs", ".scala", ".sol", ".swift", ".tcl", ".ts", ".tsx", ".vala", ".vb", ".zig"],
is_video : vec_of_strings![".3gp", ".avi", ".flv", ".m4p", ".m4v", ".mkv", ".mov", ".mp4", ".mpeg", ".mpg", ".webm", ".wmv"],
default_file_size_format : Some(String::new()),
check_for_updates : Some(false),
debug : false,
save : true,
no_color: Some(false),
gitignore: Some(false),
hgignore: Some(false),
dockerignore: Some(false),
is_zip_archive: vec_of_strings![".zip", ".jar", ".war", ".ear"],
is_archive: vec_of_strings![
".7z", ".bz2", ".bzip2", ".gz", ".gzip", ".lz", ".rar", ".tar", ".xz", ".zip"
],
is_audio: vec_of_strings![
".aac", ".aiff", ".amr", ".flac", ".gsm", ".m4a", ".m4b", ".m4p", ".mp3", ".ogg",
".wav", ".wma"
],
is_book: vec_of_strings![
".azw3", ".chm", ".djv", ".djvu", ".epub", ".fb2", ".mobi", ".pdf"
],
is_doc: vec_of_strings![
".accdb", ".doc", ".docm", ".docx", ".dot", ".dotm", ".dotx", ".mdb", ".odp",
".ods", ".odt", ".pdf", ".potm", ".potx", ".ppt", ".pptm", ".pptx", ".rtf", ".xlm",
".xls", ".xlsm", ".xlsx", ".xlt", ".xltm", ".xltx", ".xps"
],
is_font: vec_of_strings![
".eot", ".fon", ".otc", ".otf", ".ttc", ".ttf", ".woff", ".woff2"
],
is_image: vec_of_strings![
".bmp", ".exr", ".gif", ".heic", ".jpeg", ".jpg", ".jxl", ".png", ".psb", ".psd",
".svg", ".tga", ".tiff", ".webp"
],
is_source: vec_of_strings![
".asm", ".bas", ".c", ".cc", ".ceylon", ".clj", ".coffee", ".cpp", ".cs", ".d",
".dart", ".elm", ".erl", ".go", ".gradle", ".groovy", ".h", ".hh", ".hpp", ".java",
".jl", ".js", ".jsp", ".jsx", ".kt", ".kts", ".lua", ".nim", ".pas", ".php", ".pl",
".pm", ".py", ".rb", ".rs", ".scala", ".sol", ".swift", ".tcl", ".ts", ".tsx",
".vala", ".vb", ".zig"
],
is_video: vec_of_strings![
".3gp", ".avi", ".flv", ".m4p", ".m4v", ".mkv", ".mov", ".mp4", ".mpeg", ".mpg",
".webm", ".wmv"
],
default_file_size_format: Some(String::new()),
check_for_updates: Some(false),
debug: false,
save: true,
}
}
}
Expand All @@ -170,4 +189,4 @@ mod tests {

assert!(config.is_source.unwrap().contains(&String::from(".rs")));
}
}
}
42 changes: 18 additions & 24 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl Expr {
result.extend(right.get_required_fields());
}

if let Some( field) = self.field {
if let Some(field) = self.field {
result.insert(field);
}

Expand All @@ -190,7 +190,7 @@ impl Expr {
fn contains_numeric_field(expr: &Expr) -> bool {
let field = match expr.field {
Some(ref field) => field.is_numeric_field(),
None => false
None => false,
};

if field {
Expand All @@ -199,19 +199,17 @@ impl Expr {

let function = match expr.function {
Some(ref function) => function.is_numeric_function(),
None => false
None => false,
};

if function {
return true;
}

let left = match expr.left {
Some(ref left) => Self::contains_numeric_field(&left),
None => false
};

left
match expr.left {
Some(ref left) => Self::contains_numeric_field(left),
None => false,
}
}

pub fn contains_datetime(&self) -> bool {
Expand All @@ -221,19 +219,17 @@ impl Expr {
fn contains_datetime_field(expr: &Expr) -> bool {
let field = match expr.field {
Some(ref field) => field.is_datetime_field(),
None => false
None => false,
};

if field {
return true;
}

let left = match expr.left {
Some(ref left) => Self::contains_datetime_field(&left),
None => false
};

left
match expr.left {
Some(ref left) => Self::contains_datetime_field(left),
None => false,
}
}

pub fn contains_colorized(&self) -> bool {
Expand All @@ -247,19 +243,17 @@ impl Expr {

let field = match expr.field {
Some(ref field) => field.is_colorized_field(),
None => false
None => false,
};

if field {
return true;
}

let left = match expr.left {
Some(ref left) => Self::contains_colorized_field(&left),
None => false
};

left
match expr.left {
Some(ref left) => Self::contains_colorized_field(left),
None => false,
}
}
}

Expand Down Expand Up @@ -287,7 +281,7 @@ impl Display for Expr {
}

if let Some(ref val) = self.val {
fmt.write_str(&val)?;
fmt.write_str(val)?;
}

if let Some(ref right) = self.right {
Expand Down
Loading

0 comments on commit ab3f2ca

Please sign in to comment.