use configparser::ini::Ini;
use file_sql::ForceUnwrap;
use std::{collections::HashMap, fs::File, io::prelude::*};
#[derive(Debug)]
pub struct Parser<'a> {
pub path: Option<&'a str>,
pub value: HashMap<String, HashMap<String, Option<String>>>,
}
impl From<String> for Parser<'_> {
fn from(content: String) -> Self {
let mut reader = Ini::new();
Self {
path: None,
value: reader.read(content).force_unwrap(),
}
}
}
impl<'a> Parser<'a> {
pub fn from_file(path: &'a str) -> Self {
let mut reader = Ini::new();
let mut content = String::new();
let mut file = File::open(path).force_unwrap();
file.read_to_string(&mut content).force_unwrap();
Self {
path: Some(path),
value: reader.read(content).force_unwrap(),
}
}
pub fn get_value(&self, key: &str, default_val: String) -> String {
let keys: Vec<&str> = key.split("/").collect();
if let Some(v) = self.value.get(keys[0]) {
if let Some(v) = v.get(keys[1]) {
if let Some(v) = v {
return v.to_string();
}
}
}
default_val
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn test_parser() {
let parser = Parser::from_file("examples/fql.server.ini");
println!("{:#?}", parser);
}
}