summaryrefslogtreecommitdiff
path: root/meap/ch2/src/main.rs
blob: 4d16a7aa80377956a8b911fd642be789a76d4780 (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
//}