some update

This commit is contained in:
mindfreq 2026-04-29 21:07:03 +02:00
parent a290c3f218
commit f9799b7bdb
2 changed files with 10 additions and 10 deletions

View file

@ -8,9 +8,9 @@ pub struct Config {
} }
impl Config { impl Config {
pub fn build(mut args: Vec<String>) -> Result<Self, &'static str> { pub fn build(mut args: &mut Vec<String>) -> Result<Self, &'static str> {
args.remove(0); // Remove program path args.remove(0); // Remove program path
let ignore_case = Self::ignore_case(&mut args); let ignore_case = Self::ignore_case(args);
if args.len() < 2 { if args.len() < 2 {
return Err("Argument not enough!"); return Err("Argument not enough!");
@ -74,7 +74,7 @@ mod tests {
use super::*; use super::*;
fn run_search(args: Vec<String>) -> Vec<String> { fn run_search(args: &mut Vec<String>) -> Vec<String> {
let config = Config::build(args).unwrap(); let config = Config::build(args).unwrap();
let file_content = std::fs::read_to_string(&config.file_path) let file_content = std::fs::read_to_string(&config.file_path)
@ -87,10 +87,10 @@ mod tests {
#[test] #[test]
fn test_search() { fn test_search() {
let args: Vec<String> = vec!["target/debug/m-grep", "Who", "poem.txt"] let mut args: Vec<String> = vec!["target/debug/m-grep", "Who", "poem.txt"]
.iter().map(|&x| x.into()).collect(); .iter().map(|&x| x.into()).collect();
let result = run_search(args); let result = run_search(&mut args);
for line in result { for line in result {
println!("{line}"); println!("{line}");
@ -100,10 +100,10 @@ mod tests {
#[test] #[test]
fn test_search_ignorecase() { fn test_search_ignorecase() {
let args: Vec<String> = vec!["target/debug/m-grep", "who", "poem.txt", "-i"] let mut args: Vec<String> = vec!["target/debug/m-grep", "who", "poem.txt", "-i"]
.iter().map(|&x| x.into()).collect(); .iter().map(|&x| x.into()).collect();
let result = run_search(args); let result = run_search(&mut args);
for line in result { for line in result {
assert_eq!(line, format!("I'm nobody! {} are you?", "Who".green())); assert_eq!(line, format!("I'm nobody! {} are you?", "Who".green()));

View file

@ -4,15 +4,15 @@ use std::process;
use s_grep::{Config, search}; use s_grep::{Config, search};
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let mut args: Vec<String> = std::env::args().collect();
if let Err(err) = run(args) { if let Err(err) = run(&mut args) {
eprintln!("{}: {}", "Error".red().bold(), err.to_string().red()); eprintln!("{}: {}", "Error".red().bold(), err.to_string().red());
process::exit(1); process::exit(1);
} }
} }
fn run(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { fn run(args: &mut Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
let config = Config::build(args)?; let config = Config::build(args)?;
let file_path = &config.file_path; let file_path = &config.file_path;