mod default;
use serde_derive::Deserialize;
use std::collections::HashMap;
use toml::from_str;
use home::home_dir;
use std::fs;
#[derive(Deserialize, Debug, Clone)]
pub enum FontFormat {
Bold,
Italic,
Regular,
}
#[derive(Deserialize, Debug, Clone)]
pub enum ColorFormat {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
BrightBlack,
BrightRed,
BrightGreen,
BrightYellow,
BrightBlue,
BrightMagenta,
BrightCyan,
BrightWhite,
}
#[derive(Deserialize, Debug, Clone)]
pub struct FileTypeToml {
pub name: String,
pub symbol: String,
pub color: ColorFormat,
pub bg_color: Option<ColorFormat>,
pub font: FontFormat,
pub track: Vec<String>,
}
impl FileTypeToml {
pub fn new(
name: String,
symbol: String,
color: ColorFormat,
bg_color: Option<ColorFormat>,
font: FontFormat,
track: Vec<String>,
) -> Self {
Self {
name,
symbol,
color,
bg_color,
font,
track,
}
}
}
#[derive(Deserialize, Debug)]
pub struct Config {
pub file_type: Vec<FileTypeToml>,
}
fn track_hash(config: &Config) -> HashMap<String, FileTypeToml> {
let mut conf_hash: HashMap<String, FileTypeToml> = default::creat_default();
for file_types in &config.file_type {
for tracks in &file_types.track {
if let Some(config_type) = conf_hash.get_mut(tracks) {
*config_type = file_types.clone();
} else {
conf_hash.insert(tracks.to_string(), file_types.clone());
}
}
}
conf_hash
}
pub fn toml_read() -> HashMap<String, FileTypeToml> {
let home_diroctory = home_dir();
let config: Option<String> = match home_diroctory {
Some(dir) => {
let mut new_dir = dir.as_os_str().to_str().unwrap().to_string();
new_dir.push_str("/.config/lh.toml");
match fs::read_to_string(new_dir) {
Ok(f) => Some(f),
Err(_) => None,
}
}
None => None,
};
match config {
Some(conf_str) => match from_str(&conf_str) {
Ok(config) => track_hash(&config),
Err(_) => default::creat_default(),
},
None => default::creat_default(),
}
}