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
/*!
This crate provides a program called `picogrep`, a greatly simplified version of the standard UNIX
`grep` program. It supports searching for a plaintext phrase or regular expresion.

# Usage

```sh
$ picogrep [string-or-pattern] [path]
```

# Example: Find a Lines Beginning With a Date

```sh
$ picogrep '^{4}-\d{2}-\d{2}' error.log
2018-03-19: An old log entry
2018-03-20: A log entry
```
*/

extern crate regex;

mod searcher;
mod config;

use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
pub use config::Config;

/// Run `picogrep` with tthe provided arguments
pub fn run(config: Config) -> Result<(), Box<Error>> {
    let mut f = File::open(config.filename)?;
    let mut contents = String::new();
    f.read_to_string(&mut contents)?;

    let results = searcher::search(&config.query, &contents)?;

    for line in results {
        println!("{}", line);
    }

    Ok(())
}