mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 13:04:18 -06:00
124 lines
2.9 KiB
Rust
124 lines
2.9 KiB
Rust
use std::env;
|
|
use std::process;
|
|
// refactor 9
|
|
// use std::fs;
|
|
// use std::error::Error;
|
|
use minigrep::Config;
|
|
|
|
fn main() {
|
|
// refactor 13
|
|
// let args: Vec<String> = env::args().collect();
|
|
|
|
// dbg!(args);
|
|
|
|
// let query = &args[1];
|
|
// let file_path = &args[2];
|
|
|
|
// refactor 1
|
|
// let (query, file_path) = parse_config(&args);
|
|
|
|
// refactor 2
|
|
// let config = parse_config(&args)
|
|
|
|
// refactor 3
|
|
// let config = Config::new(&args);
|
|
|
|
// // recfactor 6
|
|
// let config = Config::build(&args).unwrap_or_else(|err| {
|
|
// // refactor 14
|
|
// // println!("Problem parsing arguments: {err}");
|
|
// // process::exit(1);
|
|
// eprintln!("Problem parsing arguments: {err}");
|
|
// process::exit(1);
|
|
// });
|
|
|
|
// refactor 13
|
|
let config = Config::build(env::args()).unwrap_or_else(|err| {
|
|
eprintln!("Problem parsing arguments: {err}");
|
|
process::exit(1);
|
|
});
|
|
|
|
// refactor 10
|
|
// println!("Searching for {}", config.query);
|
|
// println!("In the file {}", config.file_path);
|
|
|
|
// refactor 8
|
|
if let Err(e) = minigrep::run(config) {
|
|
// needed for helping the user
|
|
// refactpr 14
|
|
// println!("Application error: {e}");
|
|
// process::exit(1);
|
|
eprintln!("Application error: {e}");
|
|
process::exit(1);
|
|
}
|
|
|
|
// refactor 7
|
|
// // --snip--
|
|
// let contents = fs::read_to_string(config.file_path).expect("Should have been able to read the file");
|
|
|
|
// println!("With text:\n{contents}");
|
|
|
|
// refactor 8
|
|
// run(config);
|
|
}
|
|
|
|
// refactor 9
|
|
// // refactor 7
|
|
// fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
|
// let contents = fs::read_to_string(config.file_path)?;
|
|
|
|
// println!("With text:\n{contents}")
|
|
|
|
// Ok(())
|
|
// }
|
|
|
|
// refactor 9
|
|
// // refactor 3
|
|
// struct Config {
|
|
// query: String,
|
|
// file_path: String,
|
|
// }
|
|
|
|
// refactor 1
|
|
// fn parse_config(args: &[String]) -> (&str, &str) {
|
|
// let query = &args[1];
|
|
// let file_path = &args[2];
|
|
|
|
// (query, file_path)
|
|
// }
|
|
|
|
// refactor 2
|
|
// fn parse_config(args: &[String]) -> Config {
|
|
// let query = args[1].clone();
|
|
// let file_path = args[2].clone();
|
|
|
|
// Config { query, file_path }
|
|
// }
|
|
|
|
// refactor 9
|
|
// refactor 3
|
|
// impl Config {
|
|
// // // refactor 3
|
|
// // fn new(args: &[String]) -> Config {
|
|
// // // refactor 4
|
|
// // if args.len() < 3 {
|
|
// // panic!("not enough arguments");
|
|
// // }
|
|
// // let query = args[1].clone();
|
|
// // let file_path = args[2].clone();
|
|
|
|
// // Config { query, file_path }
|
|
// // }
|
|
|
|
// // refactor 5
|
|
// 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();
|
|
|
|
// Ok(Config { query, file_path })
|
|
// }
|
|
// }
|