diff --git a/config-parser/config-parser-common/src/common.rs b/config-parser/config-parser-common/src/common.rs index 9889ffe..da04f1b 100644 --- a/config-parser/config-parser-common/src/common.rs +++ b/config-parser/config-parser-common/src/common.rs @@ -1,12 +1,13 @@ #![allow(dead_code)] -use std::collections::HashMap; +use std::{collections::HashMap, ops::IndexMut}; /// Holds config, value is either a `String` or a nested `ConfigMap` pub type ConfigMap = HashMap; /// Element of Config, either a `String` for a setting /// or a nested `ConfigMap` +#[derive(Clone, Debug)] pub enum ConfigElement { Setting(String), Nested(ConfigMap), @@ -20,8 +21,19 @@ pub fn parse_config_file(input: &String) -> (ConfigMap, Vec) { for (n, line) in lines.enumerate() { let line = line.trim(); + // ignore empty lines - if line.is_empty() { continue; } + if line.is_empty() { + continue; + } + + // ignore comments + if line.starts_with("#") { + continue; + } + + // remove inline comments + let line: String = remove_inline_comment(line); if line.starts_with("[[") { messages.push(format!("error on line #{n} - arrays are not supported in config file:\n{}", line)); @@ -64,4 +76,31 @@ pub fn parse_config_file(input: &String) -> (ConfigMap, Vec) { (config, messages) } +/// searchs for the first `#` not contained within a string +/// and drops the rest of the line +pub fn remove_inline_comment(line: &str) -> String { + let mut single_quotes: bool = false; + let mut double_quotes: bool = false; + let string: String = line.to_string(); + for (n, character) in line.char_indices() { + match character { + '\'' => { + single_quotes = !single_quotes; + }, + '\"' => { + double_quotes = !double_quotes; + }, + '#' => { + if single_quotes || double_quotes { + } else { + return string[0..n].to_string(); + } + }, + _ => { + continue; + }, + } + } + string +} diff --git a/config-parser/config-parser-common/src/lib.rs b/config-parser/config-parser-common/src/lib.rs index 12cdb6d..ea9dba3 100644 --- a/config-parser/config-parser-common/src/lib.rs +++ b/config-parser/config-parser-common/src/lib.rs @@ -6,4 +6,5 @@ pub use common::{ ConfigElement, ConfigMap, parse_config_file, + remove_inline_comment, };