rusty-pipe/src/engine.rs

101 lines
2.4 KiB
Rust

use crate::config::Engine;
pub trait InputEngine {
fn get(&mut self) -> Vec<char>;
}
pub fn build_input_engine(cfg: &Engine) -> Result<Box<dyn InputEngine>, String> {
match cfg.name.as_str() {
"stdin" => return Ok(Box::new(InputEngineStdin{})),
"kafka" => return build_input_engine_kafka(&cfg),
_ => return Err("unknown input engine name".to_string() + &cfg.name),
}
}
struct InputEngineKafka {
}
pub fn build_input_engine_kafka(cfg: &Engine) -> Result<Box<dyn InputEngine>, String> {
let kafka_cfg = cfg.kafka.as_ref().unwrap();
return Err("do what".to_string());
}
impl InputEngine for InputEngineKafka {
fn get(&mut self) -> Vec<char> {
Err("end".to_string()).unwrap()
}
}
struct InputEngineStdin {}
impl InputEngine for InputEngineStdin {
fn get(&mut self) -> Vec<char> {
let stdin = std::io::stdin();
let mut result = String::new();
stdin.read_line(&mut result).unwrap();
return result.trim().chars().collect();
}
}
#[cfg(test)]
mod test_input {
use super::*;
struct InputEngineTest {}
impl InputEngine for InputEngineTest {
fn get(&mut self) -> Vec<char> {
return "hello world".chars().collect();
}
}
#[test]
fn test_input_engine_impl() {
let input_engine_test = InputEngineTest{};
_test_input_engine_impl(&input_engine_test);
}
fn _test_input_engine_impl(engine: &dyn InputEngine) {
assert_eq!("hello world".to_string(), engine.get().iter().cloned().collect::<String>());
}
}
pub trait OutputEngine {
fn put(&self, v: Vec<char>);
}
pub fn build_output_engine(cfg: &Engine) -> Result<Box<dyn OutputEngine>, String> {
match cfg.name.as_str() {
"stdout" => return Ok(Box::new(OutputEngineStdout{})),
_ => return Err("unknown output engine name".to_string() + &cfg.name),
}
}
struct OutputEngineStdout {}
impl OutputEngine for OutputEngineStdout {
fn put(&self, v: Vec<char>) {
println!("{}", v.iter().cloned().collect::<String>());
}
}
#[cfg(test)]
mod test_output {
use super::*;
struct OutputEngineTest {}
impl OutputEngine for OutputEngineTest {
fn put(&self, _v: Vec<char>) {
}
}
#[test]
fn test_output_engine_impl() {
let output_engine_test = OutputEngineTest{};
_test_output_engine_impl(&output_engine_test);
}
fn _test_output_engine_impl(engine: &dyn OutputEngine) {
engine.put("teehee".to_string().chars().collect());
}
}