#![allow(clippy::result_large_err)]
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use std::{fs, io};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct KconfigFile {
pub root_dir: PathBuf,
pub file: PathBuf,
pub global_vars: Rc<HashMap<String, String>>,
pub local_vars: Rc<HashMap<String, String>>,
pub external_functions: Rc<HashMap<String, String>>,
pub depth: usize,
pub parent_file: Option<PathBuf>,
}
impl KconfigFile {
pub fn new(root_dir: PathBuf, file: PathBuf) -> Self {
Self {
root_dir,
file,
global_vars: Rc::new(HashMap::new()),
local_vars: Rc::new(HashMap::new()),
external_functions: Rc::new(HashMap::new()),
depth: 0,
parent_file: None,
}
}
pub fn new_with_vars<S: AsRef<str>>(
root_dir: PathBuf,
file: PathBuf,
global_vars: &HashMap<S, S>,
local_vars: &HashMap<S, S>,
) -> Self {
Self {
root_dir,
file,
global_vars: Rc::new(
global_vars
.iter()
.map(|(s1, s2)| (s1.as_ref().to_string(), s2.as_ref().to_string()))
.collect(),
),
local_vars: Rc::new(
local_vars
.iter()
.map(|(s1, s2)| (s1.as_ref().to_string(), s2.as_ref().to_string()))
.collect(),
),
external_functions: Rc::new(HashMap::new()),
depth: 0,
parent_file: None,
}
}
pub fn with_external_functions(mut self, external_functions: &HashMap<String, String>) -> Self {
self.external_functions = Rc::new(external_functions.clone());
self
}
pub fn new_source_file(&self, path: PathBuf) -> Self {
let mut copied = self.clone();
copied.file = path;
copied.depth += 1;
copied.parent_file = Some(self.file.clone());
copied
}
pub fn vars(&self) -> HashMap<String, String> {
let mut variables = (*self.global_vars).clone();
variables.extend((*self.local_vars).clone());
variables
}
pub fn global_vars(&self) -> &HashMap<String, String> {
&self.global_vars
}
pub fn full_path(&self) -> PathBuf {
self.root_dir.join(&self.file)
}
pub fn read_to_string(&self) -> io::Result<String> {
fs::read_to_string(self.full_path()).map(|content| self.preprocess_content(content))
}
pub fn set_global_vars<S: AsRef<str>>(&mut self, vars: &[(S, S)]) {
self.global_vars = Rc::new(
vars.iter()
.map(|(s1, s2)| (s1.as_ref().to_string(), s2.as_ref().to_string()))
.collect(),
);
}
pub fn add_local_var<S: AsRef<str>>(&mut self, key: S, value: S) {
let mut new_map = (*self.local_vars).clone();
new_map.insert(key.as_ref().to_string(), value.as_ref().to_string());
self.local_vars = Rc::new(new_map);
}
pub fn add_local_vars(&mut self, new_vars: HashMap<String, String>) {
if new_vars.is_empty() {
return;
}
let mut new_map = (*self.local_vars).clone();
new_map.extend(new_vars);
self.local_vars = Rc::new(new_map);
}
pub fn preprocess_content(&self, content: String) -> String {
let variables = self.vars();
if variables.is_empty() {
return content;
}
let mut file_copy = content.clone();
for (var_name, var_value) in variables {
file_copy = file_copy.replace(&format!("$({var_name})"), &var_value);
file_copy = file_copy.replace(&format!("${{{var_name}}}"), &var_value);
}
file_copy
}
}