From c56cdc37fcf840e8699719a427cd54a89bd6b8ca Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Sat, 11 Mar 2023 22:34:45 +0000 Subject: [PATCH] Up to 12.4 --- .gitignore | 1 + Cargo.lock | 7 +++++++ poem.txt | 9 +++++++++ src/lib.rs | 28 ++++++++++++++++++++++++++++ src/main.rs | 22 ++++++++++++++++++++-- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 poem.txt create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..1ec6ded --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "minigrep" +version = "0.1.0" diff --git a/poem.txt b/poem.txt new file mode 100644 index 0000000..3a260bf --- /dev/null +++ b/poem.txt @@ -0,0 +1,9 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..84dd812 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,28 @@ +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 { + 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> { + let contents = fs::read_to_string(config.file_path)?; + + println!("With text:\n{}", contents); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..bb73f0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,21 @@ +use std::env; +use std::process; + +use minigrep::Config; + fn main() { - println!("Hello, world!"); -} + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|err| { + println!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} \ No newline at end of file