Compare commits
No commits in common. "21970c04d5cf4ca24f4079af859a4a5e49be91d9" and "fe8201df1e4b84085ce5a51137ce5d6cf9dd2a82" have entirely different histories.
21970c04d5
...
fe8201df1e
7
minigrep/Cargo.lock
generated
7
minigrep/Cargo.lock
generated
@ -1,7 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "minigrep"
|
|
||||||
version = "0.1.0"
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "minigrep"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
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!
|
|
||||||
传遍整个无聊的沼泽!
|
|
||||||
@ -1,103 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
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