Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 2 additions & 35 deletions listings/ch12-an-io-project/listing-12-13/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,3 @@
// ANCHOR: here
use std::error::Error;
use std::fs;

pub struct Config {
pub query: String,
pub file_path: String,
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
unimplemented!();
}

impl Config {
pub fn build(args: &[String]) -> Result<Config, &'static str> {
// --snip--
// ANCHOR_END: here
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 })
// ANCHOR: here
}
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
// --snip--
// ANCHOR_END: here
let contents = fs::read_to_string(config.file_path)?;

println!("With text:\n{contents}");

Ok(())
// ANCHOR: here
}
// ANCHOR_END: here
28 changes: 28 additions & 0 deletions listings/ch12-an-io-project/listing-12-13/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::env;
use std::error::Error;
use std::fs;
use std::process;

fn main() {
Expand All @@ -17,3 +19,29 @@ fn main() {
process::exit(1);
}
}

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

fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;

println!("With text:\n{contents}");

Ok(())
}
29 changes: 2 additions & 27 deletions listings/ch12-an-io-project/listing-12-14/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
use std::error::Error;
use std::fs;

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

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;

println!("With text:\n{contents}");

Ok(())
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
unimplemented!();
}
50 changes: 40 additions & 10 deletions listings/ch12-an-io-project/listing-12-14/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// ANCHOR: here
use std::env;
use std::error::Error;
use std::fs;
use std::process;

use minigrep::Config;
// ANCHOR: here
// --snip--
use minigrep::search;

fn main() {
// --snip--
Expand All @@ -14,16 +17,43 @@ fn main() {
process::exit(1);
});

println!("Searching for {}", config.query);
println!("In file {}", config.file_path);

// ANCHOR: here
if let Err(e) = minigrep::run(config) {
// --snip--
// ANCHOR_END: here
if let Err(e) = run(config) {
println!("Application error: {e}");
process::exit(1);
// ANCHOR: here
}
// ANCHOR: here
}

// --snip--

// ANCHOR_END: here

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

// ANCHOR: here
fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;

for line in search(&config.query, &contents) {
println!("{line}");
}

Ok(())
}
// ANCHOR_END: here
29 changes: 4 additions & 25 deletions listings/ch12-an-io-project/listing-12-15/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
use std::error::Error;
use std::fs;

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

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;

Ok(())
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
unimplemented!();
}

// ANCHOR: here
// --snip--

#[cfg(test)]
mod tests {
use super::*;
Expand Down
34 changes: 32 additions & 2 deletions listings/ch12-an-io-project/listing-12-15/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::env;
use std::error::Error;
use std::fs;
use std::process;

use minigrep::Config;
use minigrep::search;

fn main() {
let args: Vec<String> = env::args().collect();
Expand All @@ -11,8 +13,36 @@ fn main() {
process::exit(1);
});

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

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

fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;

for line in search(&config.query, &contents) {
println!("{line}");
}

Ok(())
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-16/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ failures:

---- tests::one_result stdout ----

thread 'tests::one_result' panicked at src/lib.rs:44:9:
thread 'tests::one_result' panicked at src/lib.rs:17:9:
assertion `left == right` failed
left: ["safe, fast, productive."]
right: []
Expand Down
27 changes: 0 additions & 27 deletions listings/ch12-an-io-project/listing-12-16/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
use std::error::Error;
use std::fs;

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

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;

Ok(())
}

// ANCHOR: here
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
vec![]
Expand Down
34 changes: 32 additions & 2 deletions listings/ch12-an-io-project/listing-12-16/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::env;
use std::error::Error;
use std::fs;
use std::process;

use minigrep::Config;
use minigrep::search;

fn main() {
let args: Vec<String> = env::args().collect();
Expand All @@ -11,8 +13,36 @@ fn main() {
process::exit(1);
});

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

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

fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;

for line in search(&config.query, &contents) {
println!("{line}");
}

Ok(())
}
27 changes: 0 additions & 27 deletions listings/ch12-an-io-project/listing-12-17/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
use std::error::Error;
use std::fs;

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

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;

Ok(())
}

// ANCHOR: here
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
for line in contents.lines() {
Expand Down
Loading