v0.1.2: move main fns to lib
This commit is contained in:
parent
e72fab8dd1
commit
e08b9a3b8a
37
src/lib.rs
37
src/lib.rs
|
@ -1,3 +1,36 @@
|
||||||
pub fn run() {
|
use std::fs::File;
|
||||||
println!("hello")
|
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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
41
src/main.rs
41
src/main.rs
|
@ -1,26 +1,18 @@
|
||||||
extern crate grep;
|
extern crate grep;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
struct Config {
|
|
||||||
query: String,
|
|
||||||
filename: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
let conf = Config::new(&args).unwrap_or_else(|err| {
|
let conf = grep::Config::new(&args).unwrap_or_else(|err| {
|
||||||
println!("Problem parsing arguments: {}", err);
|
println!("Problem parsing arguments: {}", err);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
});
|
});
|
||||||
//let filename = &conf.filename.clone();
|
//let filename = &conf.filename.clone();
|
||||||
|
|
||||||
if let Err(e) = run(conf) {
|
if let Err(e) = grep::run(conf) {
|
||||||
println!("Big bad bada boom!\n{:?}", e);
|
println!("Big bad bada boom!\n{:?}", e);
|
||||||
//println!("Big bad bada boom!\n{:?}", e.kind());
|
//println!("Big bad bada boom!\n{:?}", e.kind());
|
||||||
//println!("Big bad bada boom!\n{}", e.description());
|
//println!("Big bad bada boom!\n{}", e.description());
|
||||||
|
@ -28,32 +20,3 @@ fn main() {
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//fn run(conf: Config) -> Result<(), Box<std::io::Error>>
|
|
||||||
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 {
|
|
||||||
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(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue