From b3aa36b257a1b2253b60b369f3f01193e0b69927 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 23 Mar 2023 11:25:07 -0600 Subject: [PATCH] sprintf not happenin we go prefix/postfix now with templating --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + src/config.rs | 2 +- src/stream.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 163ba9c..3b4317c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -789,6 +789,12 @@ dependencies = [ "byteorder", ] +[[package]] +name = "gcc" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" + [[package]] name = "gethostname" version = "0.2.3" @@ -1745,6 +1751,16 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "printf" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15343a566c054c72bdc3f3f138002a87e5320d7389c9713112c9b77e83539141" +dependencies = [ + "gcc", + "libc", +] + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -1937,6 +1953,7 @@ dependencies = [ "hidapi", "iced", "iced_native", + "printf", "rdkafka", "rusb", "serde", diff --git a/Cargo.toml b/Cargo.toml index 56dfbfc..5d95794 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ hidapi = "0.5.0" gilrs = "0.10.1" iced = "0.8.0" iced_native = "0.9.1" +printf = "0.1.0" diff --git a/src/config.rs b/src/config.rs index 15ceafe..7357973 100644 --- a/src/config.rs +++ b/src/config.rs @@ -77,7 +77,7 @@ fn build_config_std() -> Config { }, }, output: Stream { - format: None, + format: Some(String::from("%s")), engine: Engine{ name: String::from("stdout"), kafka: None, diff --git a/src/stream.rs b/src/stream.rs index 9b49584..7b32a7c 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -3,6 +3,8 @@ use crate::config::Stream; use hidapi::HidApi; use rusb::UsbContext; use gilrs::{Gilrs, Button, Event}; +use printf; +use std::os::raw::c_void; pub trait InputStream { fn get(&mut self) -> Vec; @@ -192,7 +194,37 @@ pub trait OutputStream { } pub fn build_output_stream(cfg: &Stream) -> Box { - return _build_output_stream(cfg) + return build_formatted_output_stream(cfg, _build_output_stream(cfg)) +} + +pub struct OutputStreamFormatted { + format: Option, + stream: Box, +} + +pub fn build_formatted_output_stream(cfg: &Stream, stream: Box) -> Box { + return Box::new(OutputStreamFormatted{ + format: cfg.format.clone(), + stream: stream, + }); +} + +impl OutputStream for OutputStreamFormatted { + fn put(&mut self, v: Vec) { + match self.format.as_ref() { + Some(x) => self.stream.put(sprintf(x, v)), + None => self.stream.put(v), + }; + } +} + +fn sprintf(format: &String, arg: Vec) -> Vec { + unsafe { + return printf::printf( + format.as_bytes().as_ptr() as *const i8, + arg.iter().cloned().collect::().as_bytes().as_ptr() as *mut c_void, + ).chars().collect(); + } } pub fn _build_output_stream(cfg: &Stream) -> Box { @@ -232,6 +264,17 @@ mod test_output { fn _test_output_stream_impl(engine: &mut dyn OutputStream) { engine.put("teehee".to_string().chars().collect()); } + + #[test] + fn test_sprintf() { + assert_eq!( + "hello world".to_string().chars().collect::>(), + sprintf( + &String::from("hello %s"), + String::from("world").chars().collect(), + ), + ); + } } pub struct OutputStreamUDP {