remove anyhow dependency

This commit is contained in:
Marcello 2024-05-16 17:48:44 +02:00
parent 7086225b40
commit 77dd32e5d0
Signed by: m-lamonaca
SSH key fingerprint: SHA256:8db8uii6Gweq7TbKixFBioW2T8CbgtyFETyYL3cr3zk
3 changed files with 7 additions and 15 deletions

7
Cargo.lock generated
View file

@ -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",
]

View file

@ -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"

View file

@ -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<dyn Error>> {
let args = Args::parse();
let mut reader: Box<dyn BufRead> = 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<EnvVar> = 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(())
}