1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Reorganize parsing

This commit is contained in:
Pierre HUBERT 2021-10-16 17:29:51 +02:00
parent ffc6778822
commit 30f427b61c

View File

@ -59,16 +59,27 @@ pub struct Config {
static mut CONF: Option<Config> = None; static mut CONF: Option<Config> = None;
impl Config { impl Config {
fn yaml_u64(parsed: &Yaml, name: &str) -> u64 { fn yaml_str(parsed: &Yaml, name: &str) -> String {
parsed[name].as_i64().expect(format!("{} is missing (int)", name).as_str()) as u64 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 { fn yaml_u64(parsed: &Yaml, name: &str) -> u64 {
parsed[name].as_str().expect(format!("{} is missing (str)", name).as_str()).to_string() Self::yaml_str(parsed, name).parse().unwrap()
} }
fn yaml_bool(parsed: &Yaml, name: &str) -> bool { 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 /// Load the configuration from a given path