stdin readline ok
parent
877ec04cbe
commit
60b5574ca7
|
|
@ -5,36 +5,41 @@ use std::env;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
streams: Streams,
|
pub streams: Streams,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
struct Streams {
|
pub struct Streams {
|
||||||
input: Stream,
|
pub input: Stream,
|
||||||
output: Stream,
|
pub output: Stream,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
struct Stream {
|
pub struct Stream {
|
||||||
engine: Engine,
|
pub engine: Engine,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
struct Engine {
|
pub struct Engine {
|
||||||
name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_config() -> Config {
|
pub fn build_config() -> Result<Config, String> {
|
||||||
let config_path = env::var("CONFIG_PATH");
|
let config_path = env::var("CONFIG_PATH");
|
||||||
match config_path {
|
match config_path {
|
||||||
Ok(p) => return build_config_yaml(p),
|
Ok(p) => return build_config_yaml(p),
|
||||||
Err(_) => return build_config_std(),
|
Err(_) => return Ok(build_config_std()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_config_yaml(path: String) -> Config {
|
fn build_config_yaml(path: String) -> Result<Config, String> {
|
||||||
let buffer: String = fs::read_to_string(path).unwrap();
|
match fs::read_to_string(&path) {
|
||||||
return serde_yaml::from_str(&buffer).unwrap();
|
Ok(buffer) => match serde_yaml::from_str(&buffer) {
|
||||||
|
Ok(result) => Ok(result),
|
||||||
|
Err(_) => Err("failed to read ".to_string()+&path.as_str()),
|
||||||
|
},
|
||||||
|
Err(_) => Err("failed to read ".to_string()+&path.as_str()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_config_std() -> Config {
|
fn build_config_std() -> Config {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,24 @@
|
||||||
|
use crate::config::Engine;
|
||||||
|
|
||||||
pub trait InputEngine {
|
pub trait InputEngine {
|
||||||
fn get(&self) -> String;
|
fn get(&self) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn build_input_engine(cfg: &Engine) -> Result<Box<dyn InputEngine>, String> {
|
||||||
|
match cfg.name.as_str() {
|
||||||
|
"stdin" => return Ok(Box::new(InputEngineStdin{})),
|
||||||
|
_ => return Err("unknown input engine name".to_string() + &cfg.name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct InputEngineStdin {}
|
struct InputEngineStdin {}
|
||||||
|
|
||||||
impl InputEngine for InputEngineStdin {
|
impl InputEngine for InputEngineStdin {
|
||||||
fn get(&self) -> String {
|
fn get(&self) -> String {
|
||||||
return "hello world".to_string();
|
let stdin = std::io::stdin();
|
||||||
|
let mut result = String::new();
|
||||||
|
stdin.read_line(&mut result).unwrap();
|
||||||
|
return result.trim().to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
10
src/main.rs
10
src/main.rs
|
|
@ -1,7 +1,9 @@
|
||||||
pub mod config;
|
mod config;
|
||||||
pub mod engine;
|
mod engine;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cfg = config::build_config();
|
let cfg = config::build_config().unwrap();
|
||||||
println!("{:?}", cfg);
|
let input_engine = engine::build_input_engine(&cfg.streams.input.engine);
|
||||||
|
println!("{:?} => {}", cfg.streams.input.engine.name, input_engine.is_ok());
|
||||||
|
println!("{}", input_engine.unwrap().get());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue