diff --git a/src/lib.rs b/src/lib.rs index 620f54a..0a21adc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,16 @@ -use std::fs::File; -use std::io::Read; -use std::path::Path; - use colored::Colorize; #[derive(Debug)] pub struct Config { - query: String, - file_path: String, - ignor_case: bool, + pub query: String, + pub file_path: String, + pub ignore_case: bool, } impl Config { pub fn build(mut args: Vec) -> Result { args.remove(0); // Remove program path - let ignor_case = Self::ignore_case(&mut args); + let ignore_case = Self::ignore_case(&mut args); if args.len() < 2 { return Err("Argument not enough!"); @@ -24,7 +20,7 @@ impl Config { Ok(Self { query, file_path, - ignor_case, + ignore_case, }) } @@ -39,22 +35,12 @@ impl Config { } } -pub fn search(config: Config) -> Result, &'static str> { - let file_path = config.file_path; - if !Path::new(&file_path).exists() { - return Err("File not exists!"); - } - +pub fn search(config: Config, file_content: String) -> Result, &'static str> { let query = config.query; - let mut file = File::open(file_path).map_err(|_| "Faild to open file")?; - let mut file_content = String::new(); - file.read_to_string(&mut file_content) - .map_err(|_| "Faild to read file")?; - let mut str_result: Vec = Vec::new(); - if config.ignor_case { + if config.ignore_case { for line in file_content.lines() { let search_lower = query.to_lowercase(); let line_lower = line.to_lowercase(); @@ -62,7 +48,11 @@ pub fn search(config: Config) -> Result, &'static str> { if line_lower.contains(&search_lower) { let highlighted = line_lower .find(&search_lower) - .map(|i| line[..i].to_string() + &line[i..i+query.len()].green().to_string() + &line[i+query.len()..]) + .map(|i| { + line[..i].to_string() + + &line[i..i + query.len()].green().to_string() + + &line[i + query.len()..] + }) .unwrap_or(line.to_string()); str_result.push(highlighted); @@ -78,3 +68,5 @@ pub fn search(config: Config) -> Result, &'static str> { } Ok(str_result) } + + diff --git a/src/main.rs b/src/main.rs index 49df74f..0d8fa10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ -use std::process; - use colored::Colorize; +use std::io::Read; +use std::process; use m_grep::{Config, search}; @@ -15,7 +15,18 @@ fn main() { fn run(args: Vec) -> Result<(), Box> { let config = Config::build(args)?; - let result = search(config)?; + let file_path = &config.file_path; + // if !std::path::Path::new(&file_path).exists() { + // return Err("File not exists!"); + // } + + let mut file = std::fs::File::open(file_path).map_err(|_| "Failed to open file")?; + let mut file_content = String::new(); + + file.read_to_string(&mut file_content) + .map_err(|_| "Failed to read file")?; + + let result = search(config, file_content)?; for line in result { println!("{line}");