dotfiles/src/config.rs

35 lines
1.0 KiB
Rust
Raw Normal View History

2023-01-20 15:43:17 +00:00
use std::collections::HashMap;
use serde::Deserialize;
#[derive(Clone, Debug, Deserialize)]
pub struct Module {
#[serde(default)]
pub ignore: Vec<String>,
pub content: HashMap<String, String>
}
#[derive(Debug, Deserialize)]
pub struct ModToml {
pub modules: HashMap<String, Module>
}
pub fn parse() -> Result<ModToml, String> {
let mut files: ModToml = toml::from_str(std::fs::read_to_string("mod.toml").unwrap().as_str())
.map_err(|e| format!("Error while parsing files.toml:\n{}", e.to_string()))?;
let home_path = std::env::var("HOME").unwrap();
for (_, m) in files.modules.iter_mut() {
m.content = m.content.iter().map(|(n, p)| (
n.clone(),
p.replace("~", home_path.as_str())
)).collect()
}
for (name, _path) in files.modules.iter().flat_map(|(_, m)| m.content.iter()) {
let p = std::path::Path::new(name);
if !p.exists() {
return Err(format!("Missing module entry {}", name));
}
}
Ok(files)
}