use yaml_rust::yaml; use std::fs::File; use std::str::FromStr; use std::io::prelude::*; use std::collections::HashMap; static FILE: &str = "mrprox.conf"; pub struct Servers{ l_servers : HashMap, } impl Servers { pub fn new() -> Self { Self{ l_servers: Self::get_servers(), } } fn get_servers() -> HashMap { let mut f = File::open(&FILE).unwrap(); let mut s = String::new(); let mut ret = HashMap::new(); f.read_to_string(&mut s).unwrap(); let docs = yaml::YamlLoader::load_from_str(&s).unwrap(); let docs2 = match &docs[0]["servers"] { yaml::Yaml::Hash(ref h) => h, _ => return ret, }; for (k, v) in docs2{ ret.insert(String::from(k.as_str().unwrap()), String::from(v.as_str().unwrap())); } ret } fn get_host(&self, host: &String) -> Option<(&String, &String)>{ self.l_servers.get_key_value(host) } pub fn get_server(&self, host: &String) -> Option<(String, u16)>{ let raw: Vec<&str>; match self.get_host(host){ Some(h) => raw=h.1.split(":").collect(), None => return None, } if raw.len() == 2 { match FromStr::from_str(raw[1]) { Ok(p) => return Some((String::from(raw[0]),p)), Err(_e) => return None, } } else { return None; } } }