use serde::Serialize; use serde::Deserialize; use std::fs; use std::env; #[derive(Serialize, Deserialize, Debug)] pub struct Config { pub streams: Streams, } #[derive(Serialize, Deserialize, Debug)] pub struct Streams { pub input: Stream, pub output: Stream, } #[derive(Serialize, Deserialize, Debug)] pub struct Stream { pub engine: Engine, pub format: Option, } #[derive(Serialize, Deserialize, Debug)] pub struct Engine { pub name: String, pub kafka: Option, pub device: Option, pub udp: Option, } #[derive(Serialize, Deserialize, Debug)] pub struct Device { } #[derive(Serialize, Deserialize, Debug)] pub struct Kafka { pub addr: String, pub topic: String, pub consumer_group: String, pub crt: String, } #[derive(Serialize, Deserialize, Debug)] pub struct UDP { pub host: Option, pub port: i32, } pub fn build_config() -> Result { let config_path = env::var("CONFIG_PATH"); match config_path { Ok(p) => return build_config_yaml(p), Err(_) => return Ok(build_config_std()), } } fn build_config_yaml(path: String) -> Result { match fs::read_to_string(&path) { Ok(buffer) => match serde_yaml::from_str(&buffer) { Ok(result) => Ok(result), Err(err) => Err(err.to_string()), }, Err(err) => Err(err.to_string()), } } fn build_config_std() -> Config { return Config { streams: Streams{ input: Stream { format: None, engine: Engine{ name: String::from("gui"), kafka: None, device: None, udp: None, }, }, output: Stream { format: Some(String::from("%s")), engine: Engine{ name: String::from("stdout"), kafka: None, device: None, udp: None, }, }, }, }; } #[cfg(test)] mod tests { use super::*; #[test] fn test_build_config_std() { build_config_std(); } #[test] fn test_build_config_yaml() { build_config_yaml("./src/testdata/config-kinesis-to-kafka.json".to_string()).unwrap(); build_config_yaml("./src/testdata/config-stdin-to-stdout.yaml".to_string()).unwrap(); } }