add minigrep
This commit is contained in:
parent
c7a16fce92
commit
ec27c2371d
7
minigrep/Cargo.lock
generated
Normal file
7
minigrep/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "minigrep"
|
||||
version = "0.1.0"
|
||||
6
minigrep/Cargo.toml
Normal file
6
minigrep/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "minigrep"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
16
minigrep/poem.txt
Normal file
16
minigrep/poem.txt
Normal file
@ -0,0 +1,16 @@
|
||||
I'm nobody! Who are you?
|
||||
我啥也不是,你呢?
|
||||
Are you nobody, too?
|
||||
牛逼如你也是无名之辈吗?
|
||||
Then there's a pair of us - don't tell!
|
||||
那我们就是天生一对,嘘!别说话!
|
||||
They'd 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!
|
||||
传遍整个无聊的沼泽!
|
||||
103
minigrep/src/lib.rs
Normal file
103
minigrep/src/lib.rs
Normal file
@ -0,0 +1,103 @@
|
||||
use std::error::Error;
|
||||
use std::{env, fs};
|
||||
|
||||
pub struct Config {
|
||||
pub query: String,
|
||||
pub file_path: String,
|
||||
pub ignore_case: bool,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new(args: &[String]) -> Result<Config, &'static str> {
|
||||
if args.len() < 3 {
|
||||
return Err("not enough arguments");
|
||||
}
|
||||
let query = args[1].clone();
|
||||
let file_path = args[2].clone();
|
||||
|
||||
let ignore_case = env::var("IGNORE_CASE").is_ok();
|
||||
|
||||
Ok(Config {
|
||||
query,
|
||||
file_path,
|
||||
ignore_case,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let contents = fs::read_to_string(config.file_path)?;
|
||||
let results = if config.ignore_case {
|
||||
search_case_insensitive(&config.query, &contents)
|
||||
} else {
|
||||
search(&config.query, &contents)
|
||||
};
|
||||
|
||||
for line in results {
|
||||
println!("{line}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn one_result() {
|
||||
let query = "duct";
|
||||
let contents = "\
|
||||
Rust:
|
||||
safe, fast, productive.
|
||||
Pick three.";
|
||||
|
||||
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
|
||||
}
|
||||
|
||||
#[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_case_insensitive(query, contents)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||
let mut result = Vec::new();
|
||||
for line in contents.lines() {
|
||||
if line.contains(&query) {
|
||||
result.push(line);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||
let query = query.to_lowercase();
|
||||
let mut result = Vec::new();
|
||||
|
||||
for line in contents.lines() {
|
||||
if line.to_lowercase().contains(&query) {
|
||||
result.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
20
minigrep/src/main.rs
Normal file
20
minigrep/src/main.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use std::env;
|
||||
use std::process;
|
||||
|
||||
use minigrep::Config;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let config = Config::new(&args).unwrap_or_else(|err| {
|
||||
println!("Problem parsing arguments: {}", err);
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
println!("Searching for {}", config.query);
|
||||
println!("In file {}", config.file_path);
|
||||
|
||||
if let Err(e) = minigrep::run(config) {
|
||||
println!("Application error: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user