diff --git a/Cargo.lock b/Cargo.lock index 00ddcd6..3d05799 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -764,7 +764,7 @@ dependencies = [ [[package]] name = "rivet-head-cli" -version = "1.3.0" +version = "1.4.0" dependencies = [ "clap", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index dc859d4..21e4ecd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rivet-head-cli" -version = "1.3.0" +version = "1.4.0" edition = "2021" description = "The CLI for interacting with the Rivet Head API" readme = "README.md" diff --git a/src/main.rs b/src/main.rs index 6a88c72..3b412c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,41 @@ // dependencies use clap::Parser; -use reqwest::Error; use std::collections::HashMap; use std::io::{self, Write}; -// command line arguments +// a enum type to represent possible application errors +#[derive(Debug)] +enum AppError { + ReqwestError(reqwest::Error), + IOError(std::io::Error), +} + +// implement the From trait to convert a reqwest::Error type to an AppError type +impl From for AppError { + fn from(err: reqwest::Error) -> Self { + AppError::ReqwestError(err) + } +} + +// implement the From trait to convert a std::io::Error type to an AppError type +impl From for AppError { + fn from(err: std::io::Error) -> Self { + AppError::IOError(err) + } +} + +// implement the Display trait for the AppError type +impl std::fmt::Display for AppError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AppError::ReqwestError(err) => write!(f, "Request Error: {}", err), + AppError::IOError(err) => write!(f, "IO Error: {}", err), + } + } +} + +// struct type to represent the command line arguments #[derive(Parser, Debug)] #[command(name = "Rivet Head CLI", author = "Jeff Mitchell", version = "1.2", about = "CLI app to work with the Rivet Head API", long_about = None)] struct Args { @@ -20,8 +50,9 @@ struct Args { thoughts: Option, } +// main function #[tokio::main] -async fn main() -> Result<(), Error> { +async fn main() -> Result<(), AppError> { // parse the command line arguments let args = Args::parse(); @@ -40,7 +71,7 @@ async fn main() -> Result<(), Error> { let mut stdout = io::stdout(); writeln!(stdout, "Status: {}", res.status()).expect("Unable to write to stdout"); let body = res.text().await?; - writeln!(stdout, "Body:\n\n{:?}", body).expect("Unable to write to stdout"); + writeln!(stdout, "Body:\n\n{:?}", body)?; } verb if verb == "post" => { @@ -56,9 +87,9 @@ async fn main() -> Result<(), Error> { // print the status code and body let mut stdout = io::stdout(); - writeln!(stdout, "Status: {}", res.status()).expect("Unable to write to stdout"); + writeln!(stdout, "Status: {}", res.status())?; let body = res.text().await?; - writeln!(stdout, "Body: {:?}", body).expect("Unable to write to stdout"); + writeln!(stdout, "Body: {:?}", body)?; } verb if verb == "put" => { @@ -74,16 +105,15 @@ async fn main() -> Result<(), Error> { // print the status code and body let mut stdout = io::stdout(); - writeln!(stdout, "Status: {}", res.status()).expect("Unable to write to stdout"); + writeln!(stdout, "Status: {}", res.status())?; let body = res.text().await?; - writeln!(stdout, "Body: {:?}", body).expect("Unable to write to stdout"); + writeln!(stdout, "Body: {:?}", body)?; } // handle incorrectly spelled verbs _ => { let mut stdout = io::stdout(); - writeln!(stdout, "Invalid verb, enter get, post, put or delete") - .expect("Unable to write to stdout"); + writeln!(stdout, "Invalid verb, enter get, post, put or delete")?; } }