#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate rmp_serde;
extern crate regex;
extern crate serde_json;

use env;
use regex::RegexSet;
use std::io::BufRead;
use std::process::{Command,Stdio};

#[derive(Serialize, Debug)]
struct Entry {
    source: u32,
    level: u8,
    time: u64,
    event: String,
    details: String
}

fn main() {
    let args: Vec<String> = env::args().collect();    

    let prog = &args[1];
    let prog_args = ["biglog"];
    let prog_dir = "testdata";
    
    let child = Command::new(prog)
        .args(&prog_args)
        .current_dir(prog_dir)
        .stdout(Stdio::piped())
        .spawn()
        .expect("Spawn failed");
    
    let lineset = [r"^ "];
    
    let regset = RegexSet::new(&lineset).unwrap();

    let reader = std::io::BufReader::new(child.stdout.unwrap());
    for line in reader.lines() {
        let line = line.unwrap();
        let matches: Vec<_> = regset.matches(&line).into_iter().collect();
        match matches.len() {
            0 => {
                println!("UNKNOWN: {}", &line);
            },
            1 => {
                println!("EXACT: {}", &line);
            },
            _ => {
                print!("FAIL(");
                for ri in matches {
                    print!("{},", ri);
                }
                println!("): {}", &line);
            }
        }
   }
}
