Compare commits

..

No commits in common. "master" and "v0.0.0" have entirely different histories.

5 changed files with 1 additions and 139 deletions

4
Cargo.lock generated
View File

@ -1,4 +0,0 @@
[[package]]
name = "grep"
version = "0.1.0"

View File

@ -31,10 +31,4 @@ History
```bash
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
```

View File

@ -1,9 +0,0 @@
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!

View File

@ -1,100 +0,0 @@
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)
);
}
}

View File

@ -1,22 +1,3 @@
extern crate grep;
use std::env;
use std::process;
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();
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);
}
println!("Hello, world!");
}