rename engine to stream to capture more config

master
Bel LaPointe 2023-03-23 10:59:44 -06:00
parent 321630a945
commit ca9623b61d
4 changed files with 76 additions and 73 deletions

View File

@ -17,6 +17,7 @@ pub struct Streams {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Stream { pub struct Stream {
pub engine: Engine, pub engine: Engine,
pub format: Option<String>,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -67,6 +68,7 @@ fn build_config_std() -> Config {
return Config { return Config {
streams: Streams{ streams: Streams{
input: Stream { input: Stream {
format: None,
engine: Engine{ engine: Engine{
name: String::from("gui"), name: String::from("gui"),
kafka: None, kafka: None,
@ -75,6 +77,7 @@ fn build_config_std() -> Config {
}, },
}, },
output: Stream { output: Stream {
format: None,
engine: Engine{ engine: Engine{
name: String::from("stdout"), name: String::from("stdout"),
kafka: None, kafka: None,

View File

@ -5,13 +5,13 @@ use iced::event;
use iced::subscription; use iced::subscription;
use iced::{Alignment, Element, Application, Settings, Subscription, Theme, Command}; use iced::{Alignment, Element, Application, Settings, Subscription, Theme, Command};
use crate::engine::OutputEngine; use crate::stream::OutputStream;
pub fn main(outputEngine: Box<dyn OutputEngine>) -> iced::Result { pub fn main(outputStream: Box<dyn OutputStream>) -> iced::Result {
let def: iced::Settings<()> = Settings::default(); let def: iced::Settings<()> = Settings::default();
let settings = Settings{ let settings = Settings{
flags: Flags { flags: Flags {
outputEngine: outputEngine, outputStream: outputStream,
}, },
antialiasing: def.antialiasing, antialiasing: def.antialiasing,
default_font: def.default_font, default_font: def.default_font,
@ -33,7 +33,7 @@ struct Main {
} }
struct Flags { struct Flags {
outputEngine: Box<dyn OutputEngine>, outputStream: Box<dyn OutputStream>,
} }
struct Inputs { struct Inputs {
@ -153,7 +153,7 @@ impl Application for Main {
_ => String::from(""), _ => String::from(""),
}; };
if s.len() > 0 { if s.len() > 0 {
self.flags.outputEngine.put(s.chars().collect()); self.flags.outputStream.put(s.chars().collect());
} }
}, },
_ => {}, _ => {},

View File

@ -1,21 +1,21 @@
mod config; mod config;
mod engine; mod stream;
mod gui; mod gui;
fn main() { fn main() {
let cfg = config::build_config().unwrap(); let cfg = config::build_config().unwrap();
if cfg.streams.input.engine.name == "gui" { if cfg.streams.input.engine.name == "gui" {
let output_engine = engine::build_output_engine(&cfg.streams.output.engine); let output_stream = stream::build_output_stream(&cfg.streams.output);
gui::main(output_engine).unwrap(); gui::main(output_stream).unwrap();
} else { } else {
main_cli(cfg); main_cli(cfg);
} }
} }
fn main_cli(cfg: config::Config) { fn main_cli(cfg: config::Config) {
let mut input_engine = engine::build_input_engine(&cfg.streams.input.engine); let mut input_stream = stream::build_input_stream(&cfg.streams.input);
let mut output_engine = engine::build_output_engine(&cfg.streams.output.engine); let mut output_stream = stream::build_output_stream(&cfg.streams.output);
loop { loop {
output_engine.put(input_engine.get()); output_stream.put(input_stream.get());
} }
} }

View File

@ -1,38 +1,38 @@
use crate::config::Engine; use crate::config::Stream;
use hidapi::HidApi; use hidapi::HidApi;
use rusb::UsbContext; use rusb::UsbContext;
use gilrs::{Gilrs, Button, Event}; use gilrs::{Gilrs, Button, Event};
pub trait InputEngine { pub trait InputStream {
fn get(&mut self) -> Vec<char>; fn get(&mut self) -> Vec<char>;
} }
pub fn build_input_engine(cfg: &Engine) -> Box<dyn InputEngine> { pub fn build_input_stream(cfg: &Stream) -> Box<dyn InputStream> {
match cfg.name.as_str() { match cfg.engine.name.as_str() {
"stdin" => { "stdin" => {
return Box::new(InputEngineStdin{}); return Box::new(InputStreamStdin{});
}, },
"kafka" => { "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()), "udp" => return Box::new(build_input_stream_udp(&cfg).unwrap()),
"device" => return Box::new(build_input_engine_device(&cfg).unwrap()), "device" => return Box::new(build_input_stream_device(&cfg).unwrap()),
_ => {}, _ => {},
}; };
assert!(false); assert!(false);
Box::new(InputEngineStdin{}) Box::new(InputStreamStdin{})
} }
pub struct InputEngineDevice { pub struct InputStreamDevice {
} }
pub fn build_input_engine_device(cfg: &Engine) -> Result<InputEngineDevice, String> { pub fn build_input_stream_device(cfg: &Stream) -> Result<InputStreamDevice, String> {
return build_input_engine_device_gilrs(cfg) return build_input_stream_device_gilrs(cfg)
} }
pub fn build_input_engine_device_gilrs(cfg: &Engine) -> Result<InputEngineDevice, String> { pub fn build_input_stream_device_gilrs(cfg: &Stream) -> Result<InputStreamDevice, String> {
let _device_cfg = cfg.device.as_ref().unwrap(); let _device_cfg = cfg.engine.device.as_ref().unwrap();
let mut gilrs = Gilrs::new().unwrap(); let mut gilrs = Gilrs::new().unwrap();
@ -65,8 +65,8 @@ pub fn build_input_engine_device_gilrs(cfg: &Engine) -> Result<InputEngineDevice
return Err("do what".to_string()); return Err("do what".to_string());
} }
pub fn build_input_engine_device_hidapi(cfg: &Engine) -> Result<InputEngineDevice, String> { pub fn build_input_stream_device_hidapi(cfg: &Stream) -> Result<InputStreamDevice, String> {
let _device_cfg = cfg.device.as_ref().unwrap(); let _device_cfg = cfg.engine.device.as_ref().unwrap();
match HidApi::new() { match HidApi::new() {
Ok(api) => { Ok(api) => {
@ -81,8 +81,8 @@ pub fn build_input_engine_device_hidapi(cfg: &Engine) -> Result<InputEngineDevic
return Err("do what".to_string()); return Err("do what".to_string());
} }
pub fn build_input_engine_device_rusb(cfg: &Engine) -> Result<InputEngineDevice, String> { pub fn build_input_stream_device_rusb(cfg: &Stream) -> Result<InputStreamDevice, String> {
let _device_cfg = cfg.device.as_ref().unwrap(); let _device_cfg = cfg.engine.device.as_ref().unwrap();
assert!(rusb::has_capability()); assert!(rusb::has_capability());
@ -100,26 +100,26 @@ pub fn build_input_engine_device_rusb(cfg: &Engine) -> Result<InputEngineDevice,
return Err("do what".to_string()); return Err("do what".to_string());
} }
impl InputEngine for InputEngineDevice { impl InputStream for InputStreamDevice {
fn get(&mut self) -> Vec<char> { fn get(&mut self) -> Vec<char> {
Err("end".to_string()).unwrap() Err("end".to_string()).unwrap()
} }
} }
pub struct InputEngineUDP { pub struct InputStreamUDP {
last_socket: Option<std::net::UdpSocket>, last_socket: Option<std::net::UdpSocket>,
port: i32, port: i32,
} }
pub fn build_input_engine_udp(cfg: &Engine) -> Result<InputEngineUDP, String> { pub fn build_input_stream_udp(cfg: &Stream) -> Result<InputStreamUDP, String> {
let udp_cfg = cfg.udp.as_ref().unwrap(); let udp_cfg = cfg.engine.udp.as_ref().unwrap();
return Ok(InputEngineUDP{ return Ok(InputStreamUDP{
last_socket: None, last_socket: None,
port: udp_cfg.port, port: udp_cfg.port,
}); });
} }
impl InputEngine for InputEngineUDP { impl InputStream for InputStreamUDP {
fn get(&mut self) -> Vec<char> { fn get(&mut self) -> Vec<char> {
let addr = "0.0.0.0:".to_string() + &self.port.to_string(); 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()); 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 mut buf = [0; 128];
let result = self.last_socket.as_ref().unwrap().recv_from(&mut buf); let result = self.last_socket.as_ref().unwrap().recv_from(&mut buf);
if result.is_err() { if result.is_err() {
println!("InputEngineUDP: failed to recv: {:?}", result.err()); println!("InputStreamUDP: failed to recv: {:?}", result.err());
self.last_socket = None; self.last_socket = None;
return Vec::<char>::new(); return Vec::<char>::new();
} }
@ -140,23 +140,23 @@ impl InputEngine for InputEngineUDP {
} }
} }
pub struct InputEngineKafka { pub struct InputStreamKafka {
} }
pub fn build_input_engine_kafka(cfg: &Engine) -> Result<InputEngineKafka, String> { pub fn build_input_stream_kafka(cfg: &Stream) -> Result<InputStreamKafka, String> {
let _kafka_cfg = cfg.kafka.as_ref().unwrap(); let _kafka_cfg = cfg.engine.kafka.as_ref().unwrap();
return Err("do what".to_string()); return Err("do what".to_string());
} }
impl InputEngine for InputEngineKafka { impl InputStream for InputStreamKafka {
fn get(&mut self) -> Vec<char> { fn get(&mut self) -> Vec<char> {
Err("end".to_string()).unwrap() Err("end".to_string()).unwrap()
} }
} }
pub struct InputEngineStdin {} pub struct InputStreamStdin {}
impl InputEngine for InputEngineStdin { impl InputStream for InputStreamStdin {
fn get(&mut self) -> Vec<char> { fn get(&mut self) -> Vec<char> {
let stdin = std::io::stdin(); let stdin = std::io::stdin();
let mut result = String::new(); let mut result = String::new();
@ -169,45 +169,45 @@ impl InputEngine for InputEngineStdin {
mod test_input { mod test_input {
use super::*; use super::*;
struct InputEngineTest {} struct InputStreamTest {}
impl InputEngine for InputEngineTest { impl InputStream for InputStreamTest {
fn get(&mut self) -> Vec<char> { fn get(&mut self) -> Vec<char> {
return "hello world".chars().collect(); return "hello world".chars().collect();
} }
} }
#[test] #[test]
fn test_input_engine_impl() { fn test_input_stream_impl() {
let mut input_engine_test = InputEngineTest{}; let mut input_stream_test = InputStreamTest{};
_test_input_engine_impl(&mut input_engine_test); _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::<String>()); assert_eq!("hello world".to_string(), engine.get().iter().cloned().collect::<String>());
} }
} }
pub trait OutputEngine { pub trait OutputStream {
fn put(&mut self, v: Vec<char>); fn put(&mut self, v: Vec<char>);
} }
pub fn build_output_engine(cfg: &Engine) -> Box<dyn OutputEngine> { pub fn build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> {
return _build_output_engine(cfg) return _build_output_stream(cfg)
} }
pub fn _build_output_engine(cfg: &Engine) -> Box<dyn OutputEngine> { pub fn _build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> {
if cfg.name.as_str() == "stdout" { if cfg.engine.name.as_str() == "stdout" {
return Box::new(OutputEngineStdout{}); return Box::new(OutputStreamStdout{});
} else if cfg.name.as_str() == "udp" { } else if cfg.engine.name.as_str() == "udp" {
return Box::new(build_output_engine_udp(&cfg).unwrap()); return Box::new(build_output_stream_udp(&cfg).unwrap());
} }
assert!(false); 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<char>) { fn put(&mut self, v: Vec<char>) {
println!("{}", v.iter().cloned().collect::<String>()); println!("{}", v.iter().cloned().collect::<String>());
} }
@ -217,50 +217,50 @@ impl OutputEngine for OutputEngineStdout {
mod test_output { mod test_output {
use super::*; use super::*;
struct OutputEngineTest {} struct OutputStreamTest {}
impl OutputEngine for OutputEngineTest { impl OutputStream for OutputStreamTest {
fn put(&mut self, _v: Vec<char>) { fn put(&mut self, _v: Vec<char>) {
} }
} }
#[test] #[test]
fn test_output_engine_impl() { fn test_output_stream_impl() {
let mut output_engine_test = OutputEngineTest{}; let mut output_stream_test = OutputStreamTest{};
_test_output_engine_impl(&mut output_engine_test); _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()); engine.put("teehee".to_string().chars().collect());
} }
} }
pub struct OutputEngineUDP { pub struct OutputStreamUDP {
last_socket: Option<std::net::UdpSocket>, last_socket: Option<std::net::UdpSocket>,
host: String, host: String,
port: i32, port: i32,
} }
pub fn build_output_engine_udp(cfg: &Engine) -> Result<OutputEngineUDP, String> { pub fn build_output_stream_udp(cfg: &Stream) -> Result<OutputStreamUDP, String> {
let udp_cfg = cfg.udp.as_ref().unwrap(); let udp_cfg = cfg.engine.udp.as_ref().unwrap();
return Ok(OutputEngineUDP{ return Ok(OutputStreamUDP{
last_socket: None, last_socket: None,
host: udp_cfg.host.clone().unwrap(), host: udp_cfg.host.clone().unwrap(),
port: udp_cfg.port, port: udp_cfg.port,
}); });
} }
impl OutputEngine for OutputEngineUDP { impl OutputStream for OutputStreamUDP {
fn put(&mut self, v: Vec<char>) { fn put(&mut self, v: Vec<char>) {
if self.last_socket.is_none() { if self.last_socket.is_none() {
let result = std::net::UdpSocket::bind("127.0.0.1:".to_string() + &(self.port+10).to_string()); let result = std::net::UdpSocket::bind("127.0.0.1:".to_string() + &(self.port+10).to_string());
if result.is_err() { 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; return;
} }
self.last_socket = Some(result.unwrap()); self.last_socket = Some(result.unwrap());
let result = self.last_socket.as_ref().unwrap().connect(self.host.to_string() + ":" + &self.port.to_string()); let result = self.last_socket.as_ref().unwrap().connect(self.host.to_string() + ":" + &self.port.to_string());
if result.is_err() { 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; self.last_socket = None;
return; return;
} }
@ -268,7 +268,7 @@ impl OutputEngine for OutputEngineUDP {
let result = self.last_socket.as_ref().unwrap().send(&v.iter().cloned().collect::<String>().as_bytes()); let result = self.last_socket.as_ref().unwrap().send(&v.iter().cloned().collect::<String>().as_bytes());
if result.is_err() { if result.is_err() {
println!("OutputEngineUDP: failed to send: {:?}", result.err()); println!("OutputStreamUDP: failed to send: {:?}", result.err());
self.last_socket = None; self.last_socket = None;
} }
} }