Add filename extension checker to config parser

pull/404/head
Matt Amend 5 months ago
parent 6dd2ffe096
commit 141b0e53ba

@ -53,25 +53,38 @@ pub struct Config {
pub keymap_table: HashMap<Key, Vec<KeymapEntry>>,
}
enum ConfigFiletype {
Yaml,
Toml,
}
fn get_file_ext(filename: &PathBuf) -> ConfigFiletype {
match filename.extension() {
Some(f) => {
if f.to_str().unwrap_or("").to_lowercase() == "toml" {
ConfigFiletype::Toml
} else {
ConfigFiletype::Yaml
}
},
_ => ConfigFiletype::Yaml,
}
}
pub fn load_configs(filenames: &Vec<PathBuf>) -> Result<Config, Box<dyn error::Error>> {
// Assumes filenames is non-empty
let config_contents = fs::read_to_string(&filenames[0])?;
let mut is_yaml = true;
let mut config: Config = {
match serde_yaml::from_str(&config_contents) {
Ok(c) => c,
Err(_) => { is_yaml = false; toml::from_str(&config_contents)? }
}
let mut config: Config = match get_file_ext(&filenames[0]) {
ConfigFiletype::Yaml => serde_yaml::from_str(&config_contents)?,
ConfigFiletype::Toml => toml::from_str(&config_contents)?,
};
for filename in &filenames[1..] {
let config_contents = fs::read_to_string(&filename)?;
let c: Config = {
match is_yaml {
true => serde_yaml::from_str(&config_contents)?,
false => toml::from_str(&config_contents)?
}
let c: Config = match get_file_ext(&filename) {
ConfigFiletype::Yaml => serde_yaml::from_str(&config_contents)?,
ConfigFiletype::Toml => serde_yaml::from_str(&config_contents)?,
};
config.modmap.extend(c.modmap);

@ -523,8 +523,6 @@ fn test_toml_fail_on_data_outside_of_config_model() {
"})
}
fn toml_assert_parse(toml: &str) {
let result: Result<Config, toml::de::Error> = toml::from_str(toml);
if let Err(e) = result {

Loading…
Cancel
Save