extern crate regex;
extern crate clap;
use regex::Regex;
use clap::{App, Arg};
use std::fs::File;
use std::io;
use std::io::BufReader;
use std::io::prelude::*;
//use std::ops::{Add};
fn main() {
// let a = 10;
// let b: i32 = 20;
//
// let c = add(a, b);
// println!("a + b = {}", c);
// let twenty = 20;
// let twenty_one: i32 = twenty + 1;
// let floats_okay = 21.0;
// let million = 1_000_000;
//
// println!("{} {} {} {}", twenty, twenty_one, floats_okay, million);
//
// let three = 0b11;
// let thirty = 0o36;
// let three_hundred = 0x12c;
//
// println!("{} {} {}", three, thirty, three_hundred);
// println!("{:b} {:b} {:b}", three, thirty, three_hundred);
// println!("{:o} {:o} {:o}", three, thirty, three_hundred);
// println!("{:x} {:x} {:x}", three, thirty, three_hundred);
// //let needle = 42;
// let haystack = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862];
//
// for reference in haystack.iter() {
// let item = *reference;
//
// //if item == needle {
// // println!("{}", item);
// //}
//
// //if reference == &needle {
// // println!("{}", reference);
// //}
//
// let result = match item {
// 42 | 132 => "hit",
// _ => "miss",
// };
//
// println!("{}", result);
// }
// let (a, b) = (1.2, 3.4);
// let (x, y) = (10, 20);
// let c = add(a, b);
// let z = add(x, y);
// println!("{} {}", c, z);
let args = App::new("ch2")
.version("0.1")
.about("searches for patterns")
.arg(Arg::with_name("pattern")
.help("the pattern to search for")
.takes_value(true)
.required(true))
.arg(Arg::with_name("input")
.help("file to search")
.takes_value(true))
.get_matches();
let pattern = args.value_of("pattern").unwrap();
let re = Regex::new(pattern).unwrap();
let input = args.value_of("input").unwrap_or("-");
if input == "-" {
let stdin = io::stdin();
let reader = stdin.lock();
process_lines(reader, re);
}
else {
let file = File::open(input).unwrap();
let reader = BufReader::new(file);
process_lines(reader, re);
}
}
fn process_lines<T: BufRead + Sized>(reader: T, re: Regex) {
for line_ in reader.lines() {
let line = line_.unwrap();
match re.find(&line) {
Some(_) => print!("-> "),
None => print!(" "),
};
println!("{}", line);
}
}
//fn add<T: Add<Output = T>>(i: T, j: T) -> T {
// i + j
//}
//fn add(i: i32, j: i32) -> i32 {
// i + j
//}