sprintf not happenin we go prefix/postfix now with templating

master
Bel LaPointe 2023-03-23 11:25:07 -06:00
parent 3f0648dc3d
commit b3aa36b257
4 changed files with 63 additions and 2 deletions

17
Cargo.lock generated
View File

@ -789,6 +789,12 @@ dependencies = [
"byteorder", "byteorder",
] ]
[[package]]
name = "gcc"
version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
[[package]] [[package]]
name = "gethostname" name = "gethostname"
version = "0.2.3" version = "0.2.3"
@ -1745,6 +1751,16 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" 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]] [[package]]
name = "proc-macro-crate" name = "proc-macro-crate"
version = "1.3.1" version = "1.3.1"
@ -1937,6 +1953,7 @@ dependencies = [
"hidapi", "hidapi",
"iced", "iced",
"iced_native", "iced_native",
"printf",
"rdkafka", "rdkafka",
"rusb", "rusb",
"serde", "serde",

View File

@ -14,3 +14,4 @@ hidapi = "0.5.0"
gilrs = "0.10.1" gilrs = "0.10.1"
iced = "0.8.0" iced = "0.8.0"
iced_native = "0.9.1" iced_native = "0.9.1"
printf = "0.1.0"

View File

@ -77,7 +77,7 @@ fn build_config_std() -> Config {
}, },
}, },
output: Stream { output: Stream {
format: None, format: Some(String::from("%s")),
engine: Engine{ engine: Engine{
name: String::from("stdout"), name: String::from("stdout"),
kafka: None, kafka: None,

View File

@ -3,6 +3,8 @@ 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};
use printf;
use std::os::raw::c_void;
pub trait InputStream { pub trait InputStream {
fn get(&mut self) -> Vec<char>; fn get(&mut self) -> Vec<char>;
@ -192,7 +194,37 @@ pub trait OutputStream {
} }
pub fn build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> { pub fn build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> {
return _build_output_stream(cfg) return build_formatted_output_stream(cfg, _build_output_stream(cfg))
}
pub struct OutputStreamFormatted {
format: Option<String>,
stream: Box<dyn OutputStream>,
}
pub fn build_formatted_output_stream(cfg: &Stream, stream: Box<dyn OutputStream>) -> Box<dyn OutputStream> {
return Box::new(OutputStreamFormatted{
format: cfg.format.clone(),
stream: stream,
});
}
impl OutputStream for OutputStreamFormatted {
fn put(&mut self, v: Vec<char>) {
match self.format.as_ref() {
Some(x) => self.stream.put(sprintf(x, v)),
None => self.stream.put(v),
};
}
}
fn sprintf(format: &String, arg: Vec<char>) -> Vec<char> {
unsafe {
return printf::printf(
format.as_bytes().as_ptr() as *const i8,
arg.iter().cloned().collect::<String>().as_bytes().as_ptr() as *mut c_void,
).chars().collect();
}
} }
pub fn _build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> { pub fn _build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> {
@ -232,6 +264,17 @@ mod test_output {
fn _test_output_stream_impl(engine: &mut dyn OutputStream) { fn _test_output_stream_impl(engine: &mut dyn OutputStream) {
engine.put("teehee".to_string().chars().collect()); engine.put("teehee".to_string().chars().collect());
} }
#[test]
fn test_sprintf() {
assert_eq!(
"hello world".to_string().chars().collect::<Vec<char>>(),
sprintf(
&String::from("hello %s"),
String::from("world").chars().collect(),
),
);
}
} }
pub struct OutputStreamUDP { pub struct OutputStreamUDP {