Compare commits
No commits in common. "d4772b1c0bd1b51c1a366560c66041fec9f01b2e" and "c77249b6c84e7af7816704c227751ed231b644c5" have entirely different histories.
d4772b1c0b
...
c77249b6c8
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +0,0 @@
|
|||||||
[submodule "hello"]
|
|
||||||
path = hello
|
|
||||||
url = https://gitea.adnangonzagaci.com/adnangonzaga/hello-rust.git
|
|
1
hello
1
hello
@ -1 +0,0 @@
|
|||||||
Subproject commit 9c0c38add2f3ced36a64ba21c426f3dad13c51fc
|
|
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "minigrep"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
@ -1,86 +0,0 @@
|
|||||||
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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +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!
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user