From 5edc411bd7de72cbbb94e807cab32c5376664a54 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Fri, 22 Sep 2023 18:05:29 -0400 Subject: [PATCH] Better the imperfect config file documentation, rename struct --- src/cfg.rs | 22 ++++++++++++++-------- src/lib.rs | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/cfg.rs b/src/cfg.rs index 0468009..63eeedc 100644 --- a/src/cfg.rs +++ b/src/cfg.rs @@ -7,20 +7,26 @@ use std::io::{BufRead, BufReader, BufWriter, Cursor, Write}; use crate::cfg; use crate::gamedata::MemoryBuffer; +/// Represents a collection of keys, mapped to their values. #[derive(Debug)] -pub struct CFGSetting { +pub struct ConfigMap { + /// A map of setting name to value. pub keys: HashMap, } +/// Represents a config file, which is made up of categories and settings. Categories may have zero to one settings. #[derive(Debug)] -pub struct CFG { +pub struct ConfigFile { + /// The categories present in this config file. pub categories: Vec, - pub settings: HashMap, + /// A mapping of category to keys. + pub settings: HashMap, } -impl CFG { - pub fn from_existing(buffer: &MemoryBuffer) -> Option { - let mut cfg = CFG { +impl ConfigFile { + /// Parses an existing config file. + pub fn from_existing(buffer: &MemoryBuffer) -> Option { + let mut cfg = ConfigFile { categories: Vec::new(), settings: HashMap::new() }; @@ -34,7 +40,6 @@ impl CFG { // now parse the line! let unwrap = line.unwrap(); - println!("{}", unwrap); if !unwrap.is_empty() { if unwrap.contains('<') || unwrap.contains('>') { let name = &unwrap[1..unwrap.len() - 1]; @@ -44,7 +49,7 @@ impl CFG { } else { let parts = unwrap.split_once('\t').unwrap(); if !cfg.settings.contains_key(¤t_category.clone().unwrap()) { - cfg.settings.insert(current_category.clone().unwrap(), cfg::CFGSetting{ keys: HashMap::new() }); + cfg.settings.insert(current_category.clone().unwrap(), cfg::ConfigMap{ keys: HashMap::new() }); } cfg.settings.get_mut(¤t_category.clone().unwrap()).unwrap().keys.insert(parts.0.to_string(), parts.1.to_string()); @@ -55,6 +60,7 @@ impl CFG { Some(cfg) } + /// Writes an existing config file to a buffer. pub fn write_to_buffer(&self) -> Option { let mut buffer = MemoryBuffer::new(); diff --git a/src/lib.rs b/src/lib.rs index d9209ab..a5bcbc9 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,7 +86,7 @@ pub mod cmp; /// Reading character save datas files (DAT) pub mod chardat; -/// Reading config files (CFG) +/// Reading and writing the plaintext config files (CFG) used by the game to store most of it's configuration. pub mod cfg; mod crc;