Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
70adbe6c9b | |||
4d72618bb9 | |||
e08b9a3b8a | |||
e72fab8dd1 |
101
src/lib.rs
101
src/lib.rs
@ -1,3 +1,100 @@
|
|||||||
pub fn run() {
|
use std::fs;
|
||||||
println!("hello")
|
//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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
38
src/main.rs
38
src/main.rs
@ -1,38 +1,22 @@
|
|||||||
extern crate grep;
|
extern crate grep;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::process;
|
||||||
use std::io::prelude::*;
|
|
||||||
|
|
||||||
struct Config {
|
|
||||||
query: String,
|
|
||||||
filename: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
let conf = parse_config(&args);
|
let conf = grep::Config::new(&args).unwrap_or_else(|err| {
|
||||||
|
eprintln!("Problem parsing arguments: {}", err);
|
||||||
|
process::exit(1);
|
||||||
|
});
|
||||||
|
//let filename = &conf.filename.clone();
|
||||||
|
|
||||||
println!("{:?} {:?}", conf.query, conf.filename);
|
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());
|
||||||
|
|
||||||
let mut f = File::open(&conf.filename).expect(&("file not found: ".to_owned() + &conf.filename));
|
process::exit(1);
|
||||||
//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