Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
70adbe6c9b | |||
4d72618bb9 | |||
e08b9a3b8a | |||
e72fab8dd1 | |||
fa301ca9f1 |
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[[package]]
|
||||||
|
name = "grep"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
@ -31,4 +31,10 @@ History
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo init --bin --name grep
|
cargo init --bin --name grep
|
||||||
|
cargo run
|
||||||
|
|
||||||
|
git clone --depth=1 https://github.com/rust-lang/rust.vim.git ~/.vim/bundle/rust.vim
|
||||||
|
git clone --depth=1 https://github.com/majutsushi/tagbar.git ~/.vim/bundle/tagbar
|
||||||
|
echo 'let g:tagbar_ctags_bin = "/Users/aj/github.com/universal-ctags/ctags/ctags"' >> ~/.vimrc
|
||||||
|
echo 'let g:rustfmt_autosave = 1' >> ~/.vimrc
|
||||||
```
|
```
|
||||||
|
9
poem.txt
Normal file
9
poem.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
I'm nobody! Who are you?
|
||||||
|
Are you nobody, too?
|
||||||
|
Then there's a pair of us - don't tell!
|
||||||
|
Theyd' banish us, you know.
|
||||||
|
|
||||||
|
How dreary to be somebody!
|
||||||
|
How public, like a frog
|
||||||
|
To tell your name the livelong day
|
||||||
|
To an admiring bog!
|
100
src/lib.rs
Normal file
100
src/lib.rs
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
21
src/main.rs
21
src/main.rs
@ -1,3 +1,22 @@
|
|||||||
|
extern crate grep;
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
use std::process;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
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();
|
||||||
|
|
||||||
|
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());
|
||||||
|
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user