1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-25 22:07:44 +00:00

Reduce the amount of unwrap() in cfg, simplify some code

This commit is contained in:
Joshua Goins 2024-04-15 18:24:32 -04:00
parent 7a3a836566
commit 73d84441f0

View file

@ -12,7 +12,7 @@ pub struct ConfigMap {
pub keys: Vec<(String, String)>, pub keys: Vec<(String, String)>,
} }
/// Represents a config file, which is made up of categories and settings. Categories may have zero to one settings. /// Represents a config file, which is made up of categories and settings. Categories may have zero to one setting.
#[derive(Debug)] #[derive(Debug)]
pub struct ConfigFile { pub struct ConfigFile {
/// The categories present in this config file. /// The categories present in this config file.
@ -34,18 +34,17 @@ impl ConfigFile {
let mut current_category: Option<String> = None; let mut current_category: Option<String> = None;
for (_, line) in reader.lines().enumerate() { for line in reader.lines().flatten() {
let unwrap = line.unwrap(); if !line.is_empty() && line != "\0" {
if !unwrap.is_empty() && unwrap != "\0" { if line.contains('<') || line.contains('>') {
if unwrap.contains('<') || unwrap.contains('>') { // Category
let name = &unwrap[1..unwrap.len() - 1]; let name = &line[1..line.len() - 1];
current_category = Some(String::from(name)); current_category = Some(String::from(name));
cfg.categories.push(String::from(name)); cfg.categories.push(String::from(name));
} else { } else if let (Some(category), Some((key, value))) = (&current_category, line.split_once('\t')) {
let parts = unwrap.split_once('\t').unwrap(); // Key-value pair
cfg.settings.entry(current_category.clone().unwrap()).or_insert_with(|| ConfigMap{ keys: Vec::new() }); cfg.settings.entry(category.clone()).or_insert_with(|| ConfigMap{ keys: Vec::new() });
cfg.settings.get_mut(category)?.keys.push((key.to_string(), value.to_string()));
cfg.settings.get_mut(&current_category.clone().unwrap()).unwrap().keys.push((parts.0.to_string(), parts.1.to_string()));
} }
} }
} }
@ -93,13 +92,7 @@ impl ConfigFile {
/// Checks if the CFG contains a category named `select_category` /// Checks if the CFG contains a category named `select_category`
pub fn has_category(&self, select_category: &str) -> bool { pub fn has_category(&self, select_category: &str) -> bool {
for category in self.settings.keys() { self.settings.contains_key(select_category)
if select_category == category {
return true;
}
}
false
} }
/// Sets the value to `new_value` of `select_key` /// Sets the value to `new_value` of `select_key`