~irimi1/learning-rust

17e01c254581932e826d56e00e62b7c58bd56f85 — Manuel Groß 2 years ago d736e3c
minigrep: Use case insensitive search depending on env
1 files changed, 12 insertions(+), 2 deletions(-)

M projects/minigrep/src/lib.rs
M projects/minigrep/src/lib.rs => projects/minigrep/src/lib.rs +12 -2
@@ 1,10 1,17 @@
use std::error::Error;
use std::env;
use std::fs;

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.filename)?;

    for line in search(&config.query, &contents) {
    let results = if config.case_sensitive {
        search(&config.query, &contents)
    } else {
        search_case_insensitive(&config.query, &contents)
    };

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



@@ 39,6 46,7 @@ pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a st
pub struct Config {
    pub query: String,
    pub filename: String,
    pub case_sensitive: bool,
}

impl Config {


@@ 49,7 57,9 @@ impl Config {
        let query = args[1].clone();
        let filename = args[2].clone();

        Ok(Config { query, filename })
        let case_sensitive = env::var("CASE_INSENSITIVE").is_err();

        Ok(Config { query, filename, case_sensitive })
    }
}