From 6d3537c084fe9ed4709e0fdcd7110263dade6b43 Mon Sep 17 00:00:00 2001 From: crunk Date: Sun, 17 Sep 2023 10:57:56 +0200 Subject: [PATCH] rust tutorial: nicer error message --- src/main.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index f4c09ca..1a9a7f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,22 @@ -#![allow(unused)] use clap::Parser; /// Search for a pattern in a file and display the lines that contain it. #[derive(Parser)] struct Cli { - pattern: String, + url: String, path: std::path::PathBuf, } -fn main() { +fn main() -> Result<(), Box> { let args = Cli::parse(); - println!("pattern: {}", args.pattern); - println!( - "path: {}", - args.path.into_os_string().into_string().unwrap() - ); + let result = std::fs::read_to_string(&args.path); + let content = match result { + Ok(content) => content, + Err(error) => { + return Err(error.into()); + } + }; + println!("file content: {}", content); + Ok(()) }