Skip to content

Commit

Permalink
added test case for search and implemented search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanushrajgp committed Jul 6, 2024
1 parent 756dcfc commit c3f8dd3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
30 changes: 28 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{error::Error, fs};

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let content = fs::read_to_string(config.file_path)?;
println!("Content:\n {}", content);
let _content = fs::read_to_string(config.file_path)?;
Ok(())
}

Expand All @@ -21,3 +20,30 @@ impl Config {
Ok(Config { query, file_path })
}
}

pub fn search<'a>(query: &str, content: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();

for line in content.lines() {
if line.contains(query) {
results.push(line);
}
}
results
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn one_result() {
let query = "duct";
let content = "\
Rust:
safe,fast,productive.
pick three.";

assert_eq!(vec!["safe,fast,productive."], search(query, content))
}
}
3 changes: 0 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ fn main() {
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)
Expand Down

0 comments on commit c3f8dd3

Please sign in to comment.