Compare commits
8 Commits
c77249b6c8
...
d4772b1c0b
Author | SHA1 | Date | |
---|---|---|---|
d4772b1c0b | |||
7d2b213b47 | |||
2a31c81ee7 | |||
ecc50583b9 | |||
d61f038555 | |||
636c12e4c0 | |||
3530d47a9b | |||
1a7550137b |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[submodule "hello"]
|
||||||
|
path = hello
|
||||||
|
url = https://gitea.adnangonzagaci.com/adnangonzaga/hello-rust.git
|
1
hello
Submodule
1
hello
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 9c0c38add2f3ced36a64ba21c426f3dad13c51fc
|
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]
|
86
minigrep/src/lib.rs
Normal file
86
minigrep/src/lib.rs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs;
|
||||||
|
pub struct Config {
|
||||||
|
pub query: String,
|
||||||
|
pub file_path: String,
|
||||||
|
pub ignore_case: bool,
|
||||||
|
}
|
||||||
|
impl Config {
|
||||||
|
pub fn build(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 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
|
||||||
|
}
|
||||||
|
pub fn search_case_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
|
||||||
|
}
|
||||||
|
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 case_sensitive() {
|
||||||
|
let query = "duct";
|
||||||
|
let contents = "\
|
||||||
|
Rust:
|
||||||
|
safe, fast, productive.
|
||||||
|
Pick three.
|
||||||
|
Duct tape.";
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
17
minigrep/src/main.rs
Normal file
17
minigrep/src/main.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
use minigrep::Config;
|
||||||
|
use std::env;
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
let config = Config::build(&args).unwrap_or_else(|err| {
|
||||||
|
eprintln!("problem parsing arguments: {err}");
|
||||||
|
std::process::exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
println!("Searching for {}", config.query);
|
||||||
|
println!("In file {}", config.file_path);
|
||||||
|
|
||||||
|
if let Err(e) = minigrep::run(config) {
|
||||||
|
eprintln!("Application error: {e}");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
10
minigrep/src/poem.txt
Normal file
10
minigrep/src/poem.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
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!
|
||||||
|
|
Loading…
Reference in New Issue
Block a user