From 77dd32e5d0c4bf9e4dae6ed8fe1c532f33cc7d8e Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Thu, 16 May 2024 17:48:44 +0200 Subject: [PATCH] remove `anyhow` dependency --- Cargo.lock | 7 ------- Cargo.toml | 1 - src/main.rs | 14 +++++++------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 374c2c5..f081537 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,12 +50,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "anyhow" -version = "1.0.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" - [[package]] name = "clap" version = "4.5.0" @@ -118,7 +112,6 @@ checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" name = "json2env" version = "0.2.0" dependencies = [ - "anyhow", "clap", "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 0927352..2ab9393 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.71" clap = { version = "4.5.0", features = ["derive", "color"] } serde_json = "1.0.97" diff --git a/src/main.rs b/src/main.rs index d2975d4..888f85f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,21 @@ use std::{ + error::Error, fmt::Display, fs::File, io::{BufRead, BufReader, BufWriter, Read, Write}, }; -use anyhow::{Context, Result}; use clap::Parser; use serde_json::Value; -fn main() -> Result<()> { +fn main() -> Result<(), Box> { let args = Args::parse(); let mut reader: Box = match args.input { None => Box::new(std::io::stdin().lock()), Some(ref filename) => { let file = File::open(filename) - .with_context(|| format!("Could not open file `{filename}`"))?; + .inspect_err(|_| eprintln!("Error: Could not open file `{filename}`"))?; Box::new(BufReader::new(file)) } @@ -26,10 +26,10 @@ fn main() -> Result<()> { let input = args.input.unwrap_or("STDIN".to_string()); reader .read_to_string(&mut buffer) - .with_context(|| format!("Could not read `{input}`"))?; + .inspect_err(|_| eprintln!("Error: Could not read `{input}`"))?; let json: Value = serde_json::from_str(&buffer) - .with_context(|| format!("`{input}` does not contain valid JSON"))?; + .inspect_err(|_| eprintln!("Error: `{input}` does not contain valid JSON"))?; let mut vars: Vec = vec![]; JsonParser::parse(&mut vars, "", &json, &args.separator); @@ -44,7 +44,7 @@ fn main() -> Result<()> { None => Box::new(std::io::stdout().lock()), Some(ref filename) => { let file = File::create(filename) - .with_context(|| format!("Could not open file `{filename}`"))?; + .inspect_err(|_| eprintln!("Error: Could not open file `{filename}`"))?; Box::new(BufWriter::new(file)) } @@ -53,7 +53,7 @@ fn main() -> Result<()> { let output = args.output.unwrap_or("STDOUT".to_string()); writer .write_all(environ.as_bytes()) - .with_context(|| format!("Could not write to `{output}`"))?; + .inspect_err(|_| eprintln!("Error: Could not write to `{output}`"))?; Ok(()) }