rusty-pipe/src/config.rs

195 lines
6.2 KiB
Rust

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<String>,
pub debug: bool,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Engine {
pub name: String,
pub kafka: Option<Kafka>,
pub device: Option<Device>,
pub udp: Option<UDP>,
pub gui: Option<GUI>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Device {
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GUI {
pub buttons: Buttons,
pub release_prefix: String,
pub format_keys: Option<String>,
pub user: String,
pub feedback: GUIFeedback,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GUIFeedback {
pub url_read: Option<String>,
pub url_say: Option<String>,
pub url_send: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Buttons {
pub up: String,
pub down: String,
pub left: String,
pub right: String,
pub a: String,
pub b: String,
pub x: String,
pub y: String,
pub start: String,
pub l: String,
pub r: String,
}
#[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<String>,
pub port: i32,
}
pub fn build_config() -> Result<Config, String> {
match env::var("CONFIG_PATH") {
Ok(p) => return build_config_yaml(p),
Err(_) => match build_config_yaml(String::from("./rusty-pipe.yaml")) {
Ok(v) => return Ok(v),
Err(_) => return Ok(build_config_std()),
},
};
}
fn build_config_yaml(path: String) -> Result<Config, String> {
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 {
debug: env::var("DEBUG").unwrap_or(String::from("false")) == "true",
format: match env::var("INPUT_FORMAT") {
Ok(x) => Some(x),
Err(_) => None,
},
engine: Engine{
name: env::var("INPUT_NAME").unwrap_or(String::from("stdin")),
kafka: None,
gui: Some(GUI{
buttons: Buttons{
up: env::var("INPUT_GUI_BUTTON_UP").unwrap_or(String::from("w")),
left: env::var("INPUT_GUI_BUTTON_LEFT").unwrap_or(String::from("a")),
down: env::var("INPUT_GUI_BUTTON_DOWN").unwrap_or(String::from("s")),
right: env::var("INPUT_GUI_BUTTON_RIGHT").unwrap_or(String::from("d")),
a: env::var("INPUT_GUI_BUTTON_A").unwrap_or(String::from("1")),
b: env::var("INPUT_GUI_BUTTON_B").unwrap_or(String::from("2")),
x: env::var("INPUT_GUI_BUTTON_X").unwrap_or(String::from("3")),
y: env::var("INPUT_GUI_BUTTON_Y").unwrap_or(String::from("4")),
start: env::var("INPUT_GUI_BUTTON_START").unwrap_or(String::from("5")),
l: env::var("INPUT_GUI_BUTTON_L").unwrap_or(String::from("q")),
r: env::var("INPUT_GUI_BUTTON_R").unwrap_or(String::from("e")),
},
release_prefix: env::var("INPUT_GUI_RELEASE_PREFIX").unwrap_or(String::from("")),
user: env::var("INPUT_GUI_USER").unwrap_or(String::from("me")),
format_keys: match env::var("INPUT_GUI_FORMAT") {
Ok(x) => Some(x),
Err(_) => match env::var("INPUT_GUI_FORMAT_V01").unwrap_or(String::from("false")) == String::from("true") {
true => Some(String::from("{\"T\":{{ms}},\"U\":\"{{user}}\",\"Y\":\"{{pressed}}\",\"N\":\"{{released}}\"}")),
false => None,
},
},
feedback: GUIFeedback{
url_read: match env::var("INPUT_GUI_FEEDBACK_URL_READ") {
Ok(url) => Some(url),
Err(_) => None,
},
url_say: match env::var("INPUT_GUI_FEEDBACK_URL_SAY") {
Ok(url) => Some(url),
Err(_) => None,
},
url_send: match env::var("INPUT_GUI_FEEDBACK_URL_SEND") {
Ok(url) => Some(url),
Err(_) => None,
},
},
}),
device: None,
udp: Some(UDP{
host: Some(env::var("INPUT_UDP_HOST").unwrap_or(String::from("localhost"))),
port: env::var("INPUT_UDP_PORT").unwrap_or(String::from("17070")).parse().unwrap(),
}),
},
},
output: Stream {
debug: env::var("DEBUG").unwrap_or(String::from("false")) == "true",
format: match env::var("OUTPUT_FORMAT") {
Ok(x) => Some(x),
Err(_) => None,
},
engine: Engine{
name: env::var("OUTPUT_NAME").unwrap_or(String::from("stdout")),
kafka: None,
gui: None,
device: None,
udp: Some(UDP{
host: Some(env::var("OUTPUT_UDP_HOST").unwrap_or(String::from("localhost"))),
port: env::var("OUTPUT_UDP_PORT").unwrap_or(String::from("17070")).parse().unwrap(),
}),
},
},
},
};
}
#[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();
}
}