use crate::fields::sanity_check;
use anyhow;
use std::{
fs::File,
io::{BufRead, BufReader},
};
#[derive(Debug)]
pub struct Config {
pub file_type: String,
pub path: std::path::PathBuf,
pub mode: String,
pub user: String,
pub group: String,
pub age: String,
pub argument: String,
}
pub fn parse(config_path: &std::path::Path) -> Result<Vec<Config>, anyhow::Error> {
let mut return_config = Vec::new();
let tmpfiles_config = read_config(config_path);
match tmpfiles_config {
Ok(cfg) => {
for line in cfg {
if line.len() >= 1 {
let line: Vec<&str> = line.iter().map(|item| item.as_str()).collect();
if line[0].chars().next().unwrap().to_string() == "#" {
continue;
}
return_config.push(Config {
file_type: String::from(line[0]),
path: line[1].into(),
mode: String::from(line[2]),
user: String::from(line[3]),
group: String::from(line[4]),
age: String::from(line[5]),
argument: String::from(line[6]),
});
}
}
}
Err(e) => {
anyhow::bail!("Parsing failed with error:\n{}", e);
}
}
for config in &return_config {
match sanity_check(config) {
Ok(..) => (),
Err(e) => anyhow::bail!("Parser sanity check failed with error:\n{}", e),
}
}
Ok(return_config)
}
fn read_config(config_path: &std::path::Path) -> Result<Vec<Vec<String>>, anyhow::Error> {
let mut return_vector = Vec::new();
let file = File::open(config_path)?;
for line in BufReader::new(file).lines().map_while(Result::ok) {
let line: Vec<String> = line
.split_whitespace()
.map(|item| item.to_string())
.collect();
return_vector.push(line);
}
Ok(return_vector)
}