1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::io;
use std::io::prelude::*;
use std::collections::HashMap;
use std::sync::mpsc::{Receiver, SyncSender};

use processor::{InputProcessor, ConfigurableFilter};

/// # Stdin input
///
/// - reads stdin
///
/// ### catapult.conf
///
/// ```
/// input {
///   stdin
/// }
/// ```
/// ### Parameters
///
/// - none

pub struct Stdin {
  name: String
}

impl Stdin {
  pub fn new(name: String) -> Stdin {
    Stdin{ name: name }
  }
}

impl ConfigurableFilter for Stdin {
  fn human_name(&self) -> &str {
    self.name.as_ref()
  }
}

impl InputProcessor for Stdin {
  fn start(&self, config: &Option<HashMap<String,String>>) -> Receiver<String> {
    self.invoke(config, Stdin::handle_func)
  }
  fn handle_func(tx: SyncSender<String>, _config: Option<HashMap<String,String>>) {
    let stdin = io::stdin();

    for line in stdin.lock().lines() {
      let l = line.unwrap();
      let ll = l.clone();
      match tx.try_send(l) {
        Ok(()) => {},
        Err(e) => {
          println!("Unable to send line to processor: {}", e);
          println!("{}", ll)
        }
      }
    }
  }
}