Skip to content

Commit

Permalink
don't crash on missing config entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jhspetersson committed May 11, 2024
1 parent 9d165b2 commit 10258ee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
22 changes: 11 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const CONFIG_FILE: &str = "config.toml";

macro_rules! vec_of_strings {
($($str:literal),*) => {
vec![
Some(vec![
$(String::from($str)),*
]
])
}
}

Expand All @@ -22,15 +22,15 @@ pub struct Config {
pub gitignore: Option<bool>,
pub hgignore: Option<bool>,
pub dockerignore: Option<bool>,
pub is_zip_archive : Vec<String>,
pub is_archive : Vec<String>,
pub is_audio : Vec<String>,
pub is_book : Vec<String>,
pub is_doc : Vec<String>,
pub is_font : Vec<String>,
pub is_image : Vec<String>,
pub is_source : Vec<String>,
pub is_video : Vec<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")]
Expand Down
34 changes: 19 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ use crate::util::error_message;
use crate::util::str_to_bool;

fn main() -> ExitCode {
let default_config = Config::default();

let mut config = match Config::new() {
Ok(cnf) => cnf,
Err(err) => {
eprintln!("{}", err);
Config::default()
default_config.clone()
}
};

Expand Down Expand Up @@ -84,7 +86,7 @@ fn main() -> ExitCode {
}

if first_arg.contains("help") || first_arg.starts_with("-h") || first_arg.starts_with("/?") || first_arg.starts_with("/h") {
usage_info(config, no_color);
usage_info(config, default_config, no_color);
return ExitCode::SUCCESS;
}

Expand All @@ -101,7 +103,7 @@ fn main() -> ExitCode {
Ok(cnf) => cnf,
Err(err) => {
eprintln!("{}", err);
Config::default()
default_config.clone()
}
};

Expand Down Expand Up @@ -138,7 +140,7 @@ fn main() -> ExitCode {
},
Ok(query) => {
let _ = rl.add_history_entry(query.as_str());
exec_search(query, &mut config, no_color);
exec_search(query, &mut config, &default_config, no_color);
},
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
Expand All @@ -163,7 +165,7 @@ fn main() -> ExitCode {
}
} else {
let query = args.join(" ");
exit_value = Some(exec_search(query, &mut config, no_color));
exit_value = Some(exec_search(query, &mut config, &default_config, no_color));
}

config.save();
Expand All @@ -186,7 +188,7 @@ fn main() -> ExitCode {
ExitCode::SUCCESS
}

fn exec_search(query: String, config: &mut Config, no_color: bool) -> u8 {
fn exec_search(query: String, config: &mut Config, default_config: &Config, no_color: bool) -> u8 {
let mut p = Parser::new();
let query = p.parse(&query, config.debug);

Expand All @@ -199,7 +201,7 @@ fn exec_search(query: String, config: &mut Config, no_color: bool) -> u8 {
let is_terminal = stdout().is_terminal();
let use_colors = !no_color && is_terminal;

let mut searcher = Searcher::new(&query, config, use_colors);
let mut searcher = Searcher::new(&query, config, default_config, use_colors);
searcher.list_search_results().unwrap();

let error_count = searcher.error_count;
Expand Down Expand Up @@ -240,22 +242,24 @@ fn help_hint() {
For more detailed instructions please refer to the URL above or run fselect --help");
}

fn usage_info(config: Config, no_color: bool) {
fn usage_info(config: Config, default_config: Config, no_color: bool) {
short_usage_info(no_color);

let is_archive = config.is_archive.join(", ");
let is_audio = config.is_audio.join(", ");
let is_book = config.is_book.join(", ");
let is_doc = config.is_doc.join(", ");
let is_image = config.is_image.join(", ");
let is_source = config.is_source.join(", ");
let is_video = config.is_video.join(", ");
let is_archive = config.is_archive.unwrap_or(default_config.is_archive.unwrap()).join(", ");
let is_audio = config.is_audio.unwrap_or(default_config.is_audio.unwrap()).join(", ");
let is_book = config.is_book.unwrap_or(default_config.is_book.unwrap()).join(", ");
let is_doc = config.is_doc.unwrap_or(default_config.is_doc.unwrap()).join(", ");
let is_font = config.is_font.unwrap_or(default_config.is_font.unwrap()).join(", ");
let is_image = config.is_image.unwrap_or(default_config.is_image.unwrap()).join(", ");
let is_source = config.is_source.unwrap_or(default_config.is_source.unwrap()).join(", ");
let is_video = config.is_video.unwrap_or(default_config.is_video.unwrap()).join(", ");

println!("
Files Detected as Archives: {is_archive}
Files Detected as Audio: {is_audio}
Files Detected as Book: {is_book}
Files Detected as Document: {is_doc}
Files Detected as Fonts: {is_font}
Files Detected as Image: {is_image}
Files Detected as Source Code: {is_source}
Files Detected as Video: {is_video}
Expand Down
22 changes: 12 additions & 10 deletions src/searcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl FileMetadataState {
pub struct Searcher<'a> {
query: &'a Query,
config : &'a Config,
default_config : &'a Config,
use_colors: bool,
results_writer: ResultsWriter,
#[cfg(all(unix, feature = "users"))]
Expand All @@ -174,13 +175,14 @@ pub struct Searcher<'a> {
}

impl <'a> Searcher<'a> {
pub fn new(query: &'a Query, config: &'a Config, use_colors: bool) -> Self {
pub fn new(query: &'a Query, config: &'a Config, default_config: &'a Config, use_colors: bool) -> Self {
let limit = query.limit;

let results_writer = ResultsWriter::new(&query.output_format);
Searcher {
query,
config,
default_config,
use_colors,
results_writer,
#[cfg(all(unix, feature = "users"))]
Expand Down Expand Up @@ -1783,38 +1785,38 @@ impl <'a> Searcher<'a> {
}

fn is_zip_archive(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_zip_archive)
has_extension(file_name, &self.config.is_zip_archive.as_ref().unwrap_or(&self.default_config.is_zip_archive.as_ref().unwrap()))
}

fn is_archive(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_archive)
has_extension(file_name, &self.config.is_archive.as_ref().unwrap_or(&self.default_config.is_archive.as_ref().unwrap()))
}

fn is_audio(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_audio)
has_extension(file_name, &self.config.is_audio.as_ref().unwrap_or(&self.default_config.is_audio.as_ref().unwrap()))
}

fn is_book(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_book)
has_extension(file_name, &self.config.is_book.as_ref().unwrap_or(&self.default_config.is_book.as_ref().unwrap()))
}

fn is_doc(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_doc)
has_extension(file_name, &self.config.is_doc.as_ref().unwrap_or(&self.default_config.is_doc.as_ref().unwrap()))
}

fn is_font(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_font)
has_extension(file_name, &self.config.is_font.as_ref().unwrap_or(&self.default_config.is_font.as_ref().unwrap()))
}

fn is_image(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_image)
has_extension(file_name, &self.config.is_image.as_ref().unwrap_or(&self.default_config.is_image.as_ref().unwrap()))
}

fn is_source(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_source)
has_extension(file_name, &self.config.is_source.as_ref().unwrap_or(&self.default_config.is_source.as_ref().unwrap()))
}

fn is_video(&self, file_name: &str) -> bool {
has_extension(file_name, &self.config.is_video)
has_extension(file_name, &self.config.is_video.as_ref().unwrap_or(&self.default_config.is_video.as_ref().unwrap()))
}
}

0 comments on commit 10258ee

Please sign in to comment.