Skip to content

Commit

Permalink
moved all the definitions into lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanushrajgp committed Jul 6, 2024
1 parent cfc15b0 commit 756dcfc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::{error::Error, fs};

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let content = fs::read_to_string(config.file_path)?;
println!("Content:\n {}", content);
Ok(())
}

pub struct Config {
pub query: String,
pub file_path: String,
}

impl Config {
pub 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 })
}
}
27 changes: 2 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use minigrep::Config;
use std::env;
use std::error::Error;
use std::fs;
use std::process;

fn main() {
Expand All @@ -15,30 +14,8 @@ fn main() {
println!("Searching for {}", config.query);
println!("In file {}", config.file_path);

if let Err(e) = run(config) {
if let Err(e) = minigrep::run(config) {
println!("Application error: {e}");
process::exit(1)
}
}

fn run(config: Config) -> Result<(), Box<dyn Error>> {
let content = fs::read_to_string(config.file_path)?;
println!("Content:\n {}", content);
Ok(())
}

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 })
}
}

0 comments on commit 756dcfc

Please sign in to comment.