poo
This commit is contained in:
@@ -3,8 +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;
|
||||
use serde_json::json;
|
||||
use handlebars::Handlebars;
|
||||
|
||||
pub trait InputStream {
|
||||
fn get(&mut self) -> Vec<char>;
|
||||
@@ -197,37 +197,39 @@ pub fn build_output_stream(cfg: &Stream) -> Box<dyn OutputStream> {
|
||||
return build_formatted_output_stream(cfg, _build_output_stream(cfg))
|
||||
}
|
||||
|
||||
pub struct OutputStreamFormatted {
|
||||
format: Option<String>,
|
||||
pub struct OutputStreamFormatted<'a> {
|
||||
format: Option<Box<&'a dyn Fn(Vec<char>) -> Vec<char>>>,
|
||||
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(),
|
||||
format: match cfg.format.clone() {
|
||||
Some(x) => {
|
||||
let x = x.clone();
|
||||
Some(Box::new(
|
||||
&|v: Vec<char>| {
|
||||
let mut reg = Handlebars::new();
|
||||
reg.register_template_string("x", x.clone());
|
||||
reg.render("x", &json!({"x": v})).unwrap().chars().collect()
|
||||
},
|
||||
))
|
||||
},
|
||||
None => None,
|
||||
},
|
||||
stream: stream,
|
||||
});
|
||||
}
|
||||
|
||||
// TODO use https://crates.io/crates/handlebars instead
|
||||
impl OutputStream for OutputStreamFormatted {
|
||||
impl<'a> OutputStream for OutputStreamFormatted<'a> {
|
||||
fn put(&mut self, v: Vec<char>) {
|
||||
match self.format.as_ref() {
|
||||
Some(x) => self.stream.put(sprintf(x, v)),
|
||||
Some(x) => self.stream.put(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> {
|
||||
if cfg.engine.name.as_str() == "stdout" {
|
||||
return Box::new(OutputStreamStdout{});
|
||||
@@ -266,8 +268,9 @@ mod test_output {
|
||||
engine.put("teehee".to_string().chars().collect());
|
||||
}
|
||||
|
||||
/*
|
||||
#[test]
|
||||
fn test_sprintf() {
|
||||
fn test_output_stream_formatted() {
|
||||
assert_eq!(
|
||||
"hello world".to_string().chars().collect::<Vec<char>>(),
|
||||
sprintf(
|
||||
@@ -276,6 +279,7 @@ mod test_output {
|
||||
),
|
||||
);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
pub struct OutputStreamUDP {
|
||||
|
||||
Reference in New Issue
Block a user