diff --git a/rsddns-example.yml b/rsddns-example.yml index 1797bde..dcbf2a7 100644 --- a/rsddns-example.yml +++ b/rsddns-example.yml @@ -4,6 +4,7 @@ server: port: 8080 workers: 4 cloudflare: + email: something@something.com key: somekeyblahblahblahimakey domains: - domain: something.com @@ -12,5 +13,4 @@ ddns: domains: - domain: something.com subdomains: - - ddns ---- \ No newline at end of file + - ddns \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 9f43d9a..9f2f6c8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,36 +1,50 @@ -use yaml_rust::{Yaml, YamlLoader}; +use serde_yaml; use std::error::Error; use std::fmt; +use std::fs::File; +use std::io::prelude::*; +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct ServerConfig { pub host: String, pub port: i64, - pub workers: i64, + pub workers: i64 } + +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct CloudflareDomainConfig { pub domain: String, - pub zone_id: String, + pub zone_id: String } + +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct CloudflareConfig { pub domains: Vec, pub key: String, + pub email: String } + +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct DDNSDomain { pub domain: String, - pub subdomains: Vec, + pub subdomains: Vec } + +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct DDNSConfig { pub domains: Vec } + +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Config { pub server: ServerConfig, pub cloudflare: CloudflareConfig, - pub ddns: DDNSConfig, + pub ddns: DDNSConfig } fn get_default_config() -> Config { @@ -41,6 +55,7 @@ fn get_default_config() -> Config { workers: 4 }, cloudflare: CloudflareConfig { + email: String::from("something@something.com"), key: String::from("IAmNotAKey"), domains: vec![ CloudflareDomainConfig { @@ -73,66 +88,28 @@ impl fmt::Display for ConfigError { } } -fn create_server_config_from_yaml(yaml: &Yaml) -> Result { - Result::Ok(ServerConfig { - host: String::from(yaml["host"].as_str().unwrap()), - port: yaml["port"].as_i64().unwrap(), - workers: yaml["workers"].as_i64().unwrap(), - }) -} - -fn create_cloudflare_domain_config_from_yaml(yaml: &Yaml) -> Result { - Result::Ok(CloudflareDomainConfig { - domain: String::from(yaml["domain"].as_str().unwrap()), - zone_id: String::from(yaml["zone_id"].as_str().unwrap()), - }) -} - -fn create_cloudflare_config_from_yaml(yaml: &Yaml) -> Result { - Result::Ok(CloudflareConfig { - key: String::from(yaml["key"].as_str().unwrap()), - domains: yaml["domains"].as_vec().unwrap().into_iter().map(|y| create_cloudflare_domain_config_from_yaml(&y).unwrap()).collect(), - }) -} - -fn create_ddns_domain_config_from_yaml(yaml: &Yaml) -> Result { - Result::Ok(DDNSDomain { - domain: String::from(yaml["domain"].as_str().unwrap()), - subdomains: yaml["subdomains"].as_vec().unwrap().into_iter().map(|y| String::from(y.as_str().unwrap())).collect(), - }) -} - -fn create_ddns_config_from_yaml(yaml: &Yaml) -> Result { - Result::Ok(DDNSConfig { - domains: yaml["domains"].as_vec().unwrap().into_iter().map(|y| create_ddns_domain_config_from_yaml(&y).unwrap()).collect() - }) -} - -fn create_config_from_yaml(config_yaml: Vec) -> Result { - let config = &config_yaml[0]; - let server_config = match create_server_config_from_yaml(&config["server"]) { - Ok(v) => v, - Err(e) => return Result::Err(e) - }; - let cloudflare_config = match create_cloudflare_config_from_yaml(&config["cloudflare"]) { - Ok(v) => v, - Err(e) => return Result::Err(e) - }; - let ddns_config = match create_ddns_config_from_yaml(&config["ddns"]) { - Ok(v) => v, - Err(e) => return Result::Err(e) - }; - Result::Ok(Config { - server: server_config, - cloudflare: cloudflare_config, - ddns: ddns_config, - }) +fn read_config(yaml_str: &str) -> Result { + match serde_yaml::from_str(yaml_str) { + Ok(v) => Result::Ok(v), + Err(_e) => Result::Err(ConfigError{}) + } } pub fn load_config(path: &str) -> Result { - let yaml_load = YamlLoader::load_from_str(path); - match yaml_load { - Ok(config_yaml) => create_config_from_yaml(config_yaml), + match File::open(path) { + Ok(mut file) => { + let mut contents = String::new(); + match file.read_to_string(&mut contents) { + Ok(c) => { + if c > 0 { + read_config(&contents) + } else { + Result::Err(ConfigError{}) + } + }, + Err(_e) => Result::Err(ConfigError{}) + } + }, Err(_e) => Result::Err(ConfigError{}) } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fbd01da..ddebdbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ extern crate env_logger; extern crate futures; extern crate num_cpus; extern crate serde; -extern crate yaml_rust; +extern crate serde_yaml; use clap::{App, Arg};