diff --git a/src/config.rs b/src/config.rs index 36dc7d1..15ceafe 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,6 +17,7 @@ pub struct Streams { #[derive(Serialize, Deserialize, Debug)] pub struct Stream { pub engine: Engine, + pub format: Option, } #[derive(Serialize, Deserialize, Debug)] @@ -67,6 +68,7 @@ fn build_config_std() -> Config { return Config { streams: Streams{ input: Stream { + format: None, engine: Engine{ name: String::from("gui"), kafka: None, @@ -75,6 +77,7 @@ fn build_config_std() -> Config { }, }, output: Stream { + format: None, engine: Engine{ name: String::from("stdout"), kafka: None, diff --git a/src/gui.rs b/src/gui.rs index 2dc88f0..9887bcb 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -5,13 +5,13 @@ use iced::event; use iced::subscription; use iced::{Alignment, Element, Application, Settings, Subscription, Theme, Command}; -use crate::engine::OutputEngine; +use crate::stream::OutputStream; -pub fn main(outputEngine: Box) -> iced::Result { +pub fn main(outputStream: Box) -> iced::Result { let def: iced::Settings<()> = Settings::default(); let settings = Settings{ flags: Flags { - outputEngine: outputEngine, + outputStream: outputStream, }, antialiasing: def.antialiasing, default_font: def.default_font, @@ -33,7 +33,7 @@ struct Main { } struct Flags { - outputEngine: Box, + outputStream: Box, } struct Inputs { @@ -153,7 +153,7 @@ impl Application for Main { _ => String::from(""), }; if s.len() > 0 { - self.flags.outputEngine.put(s.chars().collect()); + self.flags.outputStream.put(s.chars().collect()); } }, _ => {}, diff --git a/src/main.rs b/src/main.rs index 875105a..5eba371 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,21 @@ mod config; -mod engine; +mod stream; mod gui; fn main() { let cfg = config::build_config().unwrap(); if cfg.streams.input.engine.name == "gui" { - let output_engine = engine::build_output_engine(&cfg.streams.output.engine); - gui::main(output_engine).unwrap(); + let output_stream = stream::build_output_stream(&cfg.streams.output); + gui::main(output_stream).unwrap(); } else { main_cli(cfg); } } fn main_cli(cfg: config::Config) { - let mut input_engine = engine::build_input_engine(&cfg.streams.input.engine); - let mut output_engine = engine::build_output_engine(&cfg.streams.output.engine); + let mut input_stream = stream::build_input_stream(&cfg.streams.input); + let mut output_stream = stream::build_output_stream(&cfg.streams.output); loop { - output_engine.put(input_engine.get()); + output_stream.put(input_stream.get()); } } diff --git a/src/engine.rs b/src/stream.rs similarity index 60% rename from src/engine.rs rename to src/stream.rs index b750c43..9b49584 100644 --- a/src/engine.rs +++ b/src/stream.rs @@ -1,38 +1,38 @@ -use crate::config::Engine; +use crate::config::Stream; use hidapi::HidApi; use rusb::UsbContext; use gilrs::{Gilrs, Button, Event}; -pub trait InputEngine { +pub trait InputStream { fn get(&mut self) -> Vec; } -pub fn build_input_engine(cfg: &Engine) -> Box { - match cfg.name.as_str() { +pub fn build_input_stream(cfg: &Stream) -> Box { + match cfg.engine.name.as_str() { "stdin" => { - return Box::new(InputEngineStdin{}); + return Box::new(InputStreamStdin{}); }, "kafka" => { - return Box::new(build_input_engine_kafka(&cfg).unwrap()); + return Box::new(build_input_stream_kafka(&cfg).unwrap()); }, - "udp" => return Box::new(build_input_engine_udp(&cfg).unwrap()), - "device" => return Box::new(build_input_engine_device(&cfg).unwrap()), + "udp" => return Box::new(build_input_stream_udp(&cfg).unwrap()), + "device" => return Box::new(build_input_stream_device(&cfg).unwrap()), _ => {}, }; assert!(false); - Box::new(InputEngineStdin{}) + Box::new(InputStreamStdin{}) } -pub struct InputEngineDevice { +pub struct InputStreamDevice { } -pub fn build_input_engine_device(cfg: &Engine) -> Result { - return build_input_engine_device_gilrs(cfg) +pub fn build_input_stream_device(cfg: &Stream) -> Result { + return build_input_stream_device_gilrs(cfg) } -pub fn build_input_engine_device_gilrs(cfg: &Engine) -> Result { - let _device_cfg = cfg.device.as_ref().unwrap(); +pub fn build_input_stream_device_gilrs(cfg: &Stream) -> Result { + let _device_cfg = cfg.engine.device.as_ref().unwrap(); let mut gilrs = Gilrs::new().unwrap(); @@ -65,8 +65,8 @@ pub fn build_input_engine_device_gilrs(cfg: &Engine) -> Result Result { - let _device_cfg = cfg.device.as_ref().unwrap(); +pub fn build_input_stream_device_hidapi(cfg: &Stream) -> Result { + let _device_cfg = cfg.engine.device.as_ref().unwrap(); match HidApi::new() { Ok(api) => { @@ -81,8 +81,8 @@ pub fn build_input_engine_device_hidapi(cfg: &Engine) -> Result Result { - let _device_cfg = cfg.device.as_ref().unwrap(); +pub fn build_input_stream_device_rusb(cfg: &Stream) -> Result { + let _device_cfg = cfg.engine.device.as_ref().unwrap(); assert!(rusb::has_capability()); @@ -100,26 +100,26 @@ pub fn build_input_engine_device_rusb(cfg: &Engine) -> Result Vec { Err("end".to_string()).unwrap() } } -pub struct InputEngineUDP { +pub struct InputStreamUDP { last_socket: Option, port: i32, } -pub fn build_input_engine_udp(cfg: &Engine) -> Result { - let udp_cfg = cfg.udp.as_ref().unwrap(); - return Ok(InputEngineUDP{ +pub fn build_input_stream_udp(cfg: &Stream) -> Result { + let udp_cfg = cfg.engine.udp.as_ref().unwrap(); + return Ok(InputStreamUDP{ last_socket: None, port: udp_cfg.port, }); } -impl InputEngine for InputEngineUDP { +impl InputStream for InputStreamUDP { fn get(&mut self) -> Vec { let addr = "0.0.0.0:".to_string() + &self.port.to_string(); println!("$ echo -n 'hello world' | nc -4u -w0 localhost {}", &self.port.to_string()); @@ -130,7 +130,7 @@ impl InputEngine for InputEngineUDP { let mut buf = [0; 128]; let result = self.last_socket.as_ref().unwrap().recv_from(&mut buf); if result.is_err() { - println!("InputEngineUDP: failed to recv: {:?}", result.err()); + println!("InputStreamUDP: failed to recv: {:?}", result.err()); self.last_socket = None; return Vec::::new(); } @@ -140,23 +140,23 @@ impl InputEngine for InputEngineUDP { } } -pub struct InputEngineKafka { +pub struct InputStreamKafka { } -pub fn build_input_engine_kafka(cfg: &Engine) -> Result { - let _kafka_cfg = cfg.kafka.as_ref().unwrap(); +pub fn build_input_stream_kafka(cfg: &Stream) -> Result { + let _kafka_cfg = cfg.engine.kafka.as_ref().unwrap(); return Err("do what".to_string()); } -impl InputEngine for InputEngineKafka { +impl InputStream for InputStreamKafka { fn get(&mut self) -> Vec { Err("end".to_string()).unwrap() } } -pub struct InputEngineStdin {} +pub struct InputStreamStdin {} -impl InputEngine for InputEngineStdin { +impl InputStream for InputStreamStdin { fn get(&mut self) -> Vec { let stdin = std::io::stdin(); let mut result = String::new(); @@ -169,45 +169,45 @@ impl InputEngine for InputEngineStdin { mod test_input { use super::*; - struct InputEngineTest {} - impl InputEngine for InputEngineTest { + struct InputStreamTest {} + impl InputStream for InputStreamTest { fn get(&mut self) -> Vec { return "hello world".chars().collect(); } } #[test] - fn test_input_engine_impl() { - let mut input_engine_test = InputEngineTest{}; - _test_input_engine_impl(&mut input_engine_test); + fn test_input_stream_impl() { + let mut input_stream_test = InputStreamTest{}; + _test_input_stream_impl(&mut input_stream_test); } - fn _test_input_engine_impl(engine: &mut dyn InputEngine) { + fn _test_input_stream_impl(engine: &mut dyn InputStream) { assert_eq!("hello world".to_string(), engine.get().iter().cloned().collect::()); } } -pub trait OutputEngine { +pub trait OutputStream { fn put(&mut self, v: Vec); } -pub fn build_output_engine(cfg: &Engine) -> Box { - return _build_output_engine(cfg) +pub fn build_output_stream(cfg: &Stream) -> Box { + return _build_output_stream(cfg) } -pub fn _build_output_engine(cfg: &Engine) -> Box { - if cfg.name.as_str() == "stdout" { - return Box::new(OutputEngineStdout{}); - } else if cfg.name.as_str() == "udp" { - return Box::new(build_output_engine_udp(&cfg).unwrap()); +pub fn _build_output_stream(cfg: &Stream) -> Box { + if cfg.engine.name.as_str() == "stdout" { + return Box::new(OutputStreamStdout{}); + } else if cfg.engine.name.as_str() == "udp" { + return Box::new(build_output_stream_udp(&cfg).unwrap()); } assert!(false); - Box::new(OutputEngineStdout{}) + Box::new(OutputStreamStdout{}) } -pub struct OutputEngineStdout {} +pub struct OutputStreamStdout {} -impl OutputEngine for OutputEngineStdout { +impl OutputStream for OutputStreamStdout { fn put(&mut self, v: Vec) { println!("{}", v.iter().cloned().collect::()); } @@ -217,50 +217,50 @@ impl OutputEngine for OutputEngineStdout { mod test_output { use super::*; - struct OutputEngineTest {} - impl OutputEngine for OutputEngineTest { + struct OutputStreamTest {} + impl OutputStream for OutputStreamTest { fn put(&mut self, _v: Vec) { } } #[test] - fn test_output_engine_impl() { - let mut output_engine_test = OutputEngineTest{}; - _test_output_engine_impl(&mut output_engine_test); + fn test_output_stream_impl() { + let mut output_stream_test = OutputStreamTest{}; + _test_output_stream_impl(&mut output_stream_test); } - fn _test_output_engine_impl(engine: &mut dyn OutputEngine) { + fn _test_output_stream_impl(engine: &mut dyn OutputStream) { engine.put("teehee".to_string().chars().collect()); } } -pub struct OutputEngineUDP { +pub struct OutputStreamUDP { last_socket: Option, host: String, port: i32, } -pub fn build_output_engine_udp(cfg: &Engine) -> Result { - let udp_cfg = cfg.udp.as_ref().unwrap(); - return Ok(OutputEngineUDP{ +pub fn build_output_stream_udp(cfg: &Stream) -> Result { + let udp_cfg = cfg.engine.udp.as_ref().unwrap(); + return Ok(OutputStreamUDP{ last_socket: None, host: udp_cfg.host.clone().unwrap(), port: udp_cfg.port, }); } -impl OutputEngine for OutputEngineUDP { +impl OutputStream for OutputStreamUDP { fn put(&mut self, v: Vec) { if self.last_socket.is_none() { let result = std::net::UdpSocket::bind("127.0.0.1:".to_string() + &(self.port+10).to_string()); if result.is_err() { - println!("OutputEngineUDP: failed to bind to 127.0.0.1:{}: {:?}", &(self.port+10).to_string(), result.err()); + println!("OutputStreamUDP: failed to bind to 127.0.0.1:{}: {:?}", &(self.port+10).to_string(), result.err()); return; } self.last_socket = Some(result.unwrap()); let result = self.last_socket.as_ref().unwrap().connect(self.host.to_string() + ":" + &self.port.to_string()); if result.is_err() { - println!("OutputEngineUDP: failed to connect to {}:{}: {:?}", self.host.to_string(), self.port.to_string(), result.err()); + println!("OutputStreamUDP: failed to connect to {}:{}: {:?}", self.host.to_string(), self.port.to_string(), result.err()); self.last_socket = None; return; } @@ -268,7 +268,7 @@ impl OutputEngine for OutputEngineUDP { let result = self.last_socket.as_ref().unwrap().send(&v.iter().cloned().collect::().as_bytes()); if result.is_err() { - println!("OutputEngineUDP: failed to send: {:?}", result.err()); + println!("OutputStreamUDP: failed to send: {:?}", result.err()); self.last_socket = None; } }