minigrep/'

40 lines
853 B
Text
Raw Normal View History

2024-08-24 03:20:46 -07:00
use std::env;
use std::fs;
use std::process;
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|err| {
println!("Problem parsing arguments: {err}");
process::exit(1);
});
run(config);
}
fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents =
fs::read_to_string(config.file_path).expect("Should have have been able to read the file!");
println!("With text:\n{contents}");
}
struct Config {
query: String,
file_path: String,
}
impl Config {
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 })
}
}