37 lines
790 B
Rust
37 lines
790 B
Rust
use std::fs::File;
|
|
use std::io::prelude::*;
|
|
use std::error::Error;
|
|
|
|
pub struct Config {
|
|
query: String,
|
|
filename: String,
|
|
}
|
|
|
|
pub fn run(conf: Config) -> Result<(), Box<Error>> {
|
|
println!("{:?} {:?}", conf.query, conf.filename);
|
|
|
|
let mut f = File::open(&conf.filename)?;
|
|
|
|
let mut contents = String::new();
|
|
f.read_to_string(&mut contents)?;
|
|
|
|
println!("Searching for '{}'", conf.query);
|
|
println!("In file '{}'", conf.filename);
|
|
println!("With text:\n{}", contents);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
impl Config {
|
|
pub fn new(args: &[String]) -> Result<Config, &'static str> {
|
|
if args.len() < 3 {
|
|
return Err("too few arguments")
|
|
}
|
|
|
|
Ok(Config {
|
|
query: args[1].clone(),
|
|
filename: args[2].clone(),
|
|
})
|
|
}
|
|
}
|