Compare commits
	
		
			No commits in common. "master" and "v0.1.0" have entirely different histories.
		
	
	
		
	
		
							
								
								
									
										101
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										101
									
								
								src/lib.rs
									
									
									
									
									
								
							@ -1,100 +1,3 @@
 | 
			
		||||
use std::fs;
 | 
			
		||||
//use std::fs::File;
 | 
			
		||||
//use std::io::prelude::*;
 | 
			
		||||
use std::error::Error;
 | 
			
		||||
use std::env;
 | 
			
		||||
 | 
			
		||||
pub struct Config {
 | 
			
		||||
    query: String,
 | 
			
		||||
    filename: String,
 | 
			
		||||
    insensitive: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn run(conf: Config) -> Result<(), Box<dyn Error>> {
 | 
			
		||||
    let contents = fs::read_to_string(conf.filename)?;
 | 
			
		||||
 | 
			
		||||
    let results = if conf.insensitive {
 | 
			
		||||
        search(&conf.query, &contents)
 | 
			
		||||
    } else {
 | 
			
		||||
        search_insensitive(&conf.query, &contents)
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    for line in results {
 | 
			
		||||
        println!("{}", line);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
 | 
			
		||||
    let mut results = Vec::new();
 | 
			
		||||
 | 
			
		||||
    for line in contents.lines() {
 | 
			
		||||
        if line.contains(query) {
 | 
			
		||||
          results.push(line);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    results
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn search_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
 | 
			
		||||
    let query = query.to_lowercase();
 | 
			
		||||
    let mut results = Vec::new();
 | 
			
		||||
 | 
			
		||||
    for line in contents.lines() {
 | 
			
		||||
        if line.to_lowercase().contains(&query) {
 | 
			
		||||
          results.push(line);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    results
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Config {
 | 
			
		||||
    pub fn new(args: &[String]) -> Result<Config, &'static str> {
 | 
			
		||||
        if args.len() < 3 {
 | 
			
		||||
            return Err("too few arguments")
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(Config {
 | 
			
		||||
            query: args[1].clone(),
 | 
			
		||||
            filename: args[2].clone(),
 | 
			
		||||
            insensitive: env::var("CASE_INSENSITIVE").is_err(),
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn case_sensitive() {
 | 
			
		||||
        let query = "duct";
 | 
			
		||||
        let contents = "\
 | 
			
		||||
Rust:
 | 
			
		||||
safe, fast, productive.
 | 
			
		||||
Pick three.";
 | 
			
		||||
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            vec!["safe, fast, productive."],
 | 
			
		||||
            search(query, contents)
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn case_insensitive() {
 | 
			
		||||
        let query = "rUsT";
 | 
			
		||||
        let contents = "\
 | 
			
		||||
Rust:
 | 
			
		||||
safe, fast, productive.
 | 
			
		||||
Pick three.
 | 
			
		||||
Trust me.";
 | 
			
		||||
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            vec!["Rust:", "Trust me."],
 | 
			
		||||
            search_insensitive(query, contents)
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
pub fn run() {
 | 
			
		||||
    println!("hello")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										38
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								src/main.rs
									
									
									
									
									
								
							@ -1,22 +1,38 @@
 | 
			
		||||
extern crate grep;
 | 
			
		||||
 | 
			
		||||
use std::env;
 | 
			
		||||
use std::process;
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::io::prelude::*;
 | 
			
		||||
 | 
			
		||||
struct Config {
 | 
			
		||||
    query: String,
 | 
			
		||||
    filename: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let args: Vec<String> = env::args().collect();
 | 
			
		||||
 | 
			
		||||
    let conf = grep::Config::new(&args).unwrap_or_else(|err| {
 | 
			
		||||
        eprintln!("Problem parsing arguments: {}", err);
 | 
			
		||||
        process::exit(1);
 | 
			
		||||
    });
 | 
			
		||||
    //let filename = &conf.filename.clone();
 | 
			
		||||
    let conf = parse_config(&args);
 | 
			
		||||
 | 
			
		||||
    if let Err(e) = grep::run(conf) {
 | 
			
		||||
        eprintln!("Big bad bada boom!\n{:?}", e);
 | 
			
		||||
        //println!("Big bad bada boom!\n{:?}", e.kind());
 | 
			
		||||
        //println!("Big bad bada boom!\n{}", e.description());
 | 
			
		||||
    println!("{:?} {:?}", conf.query, conf.filename);
 | 
			
		||||
 | 
			
		||||
        process::exit(1);
 | 
			
		||||
    let mut f = File::open(&conf.filename).expect(&("file not found: ".to_owned() + &conf.filename));
 | 
			
		||||
    //let mut f = File::open(filename).expect(&format!("file not found: {}", &filename));
 | 
			
		||||
 | 
			
		||||
    let mut contents = String::new();
 | 
			
		||||
    f.read_to_string(&mut contents)
 | 
			
		||||
        .expect("something went wrong reading the file");
 | 
			
		||||
 | 
			
		||||
    println!("Searching for '{}'", conf.query);
 | 
			
		||||
    println!("In file '{}'", conf.filename);
 | 
			
		||||
    println!("With text:\n{}", contents);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//fn parse_config(args: &[String]) -> (&String, &String)
 | 
			
		||||
//fn parse_config(args: &[String]) -> (&str, &str)
 | 
			
		||||
fn parse_config(args: &[String]) -> Config {
 | 
			
		||||
    Config {
 | 
			
		||||
        query: args[1].clone(),
 | 
			
		||||
        filename: args[2].clone(),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user