From 30f427b61c728ed9a201ad4f9abc67b24d367b93 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 16 Oct 2021 17:29:51 +0200 Subject: [PATCH] Reorganize parsing --- src/data/config.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/data/config.rs b/src/data/config.rs index 82d151f..ab6d21f 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -59,16 +59,27 @@ pub struct Config { static mut CONF: Option = None; impl Config { - fn yaml_u64(parsed: &Yaml, name: &str) -> u64 { - parsed[name].as_i64().expect(format!("{} is missing (int)", name).as_str()) as u64 + fn yaml_str(parsed: &Yaml, name: &str) -> String { + match &parsed[name] { + Yaml::Real(r) | Yaml::String(r) => r.to_string(), + Yaml::Integer(i) => i.to_string(), + Yaml::Boolean(true) => "true".to_string(), + Yaml::Boolean(false) => "false".to_string(), + Yaml::Array(_) | Yaml::Hash(_) | Yaml::Alias(_) | Yaml::Null => { + panic!("{} can not be converted to string", name); + } + Yaml::BadValue => { + panic!("{} is missing", name); + } + }.to_string() } - fn yaml_str(parsed: &Yaml, name: &str) -> String { - parsed[name].as_str().expect(format!("{} is missing (str)", name).as_str()).to_string() + fn yaml_u64(parsed: &Yaml, name: &str) -> u64 { + Self::yaml_str(parsed, name).parse().unwrap() } fn yaml_bool(parsed: &Yaml, name: &str) -> bool { - parsed[name].as_bool().expect(format!("{} is missing (bool)", name).as_str()) + Self::yaml_str(parsed, name).to_lowercase().eq("true") } /// Load the configuration from a given path