From b76781799ae81c310192377b0235d815063498e2 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Thu, 6 Apr 2023 23:16:38 +0100 Subject: [PATCH] Use anyhow for errors --- agb-gbafix/Cargo.lock | 7 +++++++ agb-gbafix/Cargo.toml | 3 ++- agb-gbafix/src/main.rs | 14 ++++++++------ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/agb-gbafix/Cargo.lock b/agb-gbafix/Cargo.lock index e23beada..6510f171 100644 --- a/agb-gbafix/Cargo.lock +++ b/agb-gbafix/Cargo.lock @@ -6,11 +6,18 @@ version = 3 name = "agb-gbafix" version = "0.1.0" dependencies = [ + "anyhow", "bytemuck", "elf", "gbafix", ] +[[package]] +name = "anyhow" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" + [[package]] name = "bytemuck" version = "1.13.1" diff --git a/agb-gbafix/Cargo.toml b/agb-gbafix/Cargo.toml index de91941c..deb9792b 100644 --- a/agb-gbafix/Cargo.toml +++ b/agb-gbafix/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] elf = "0.7" gbafix = "1" -bytemuck = "1" \ No newline at end of file +bytemuck = "1" +anyhow = "1" \ No newline at end of file diff --git a/agb-gbafix/src/main.rs b/agb-gbafix/src/main.rs index 263a8011..0bade60f 100644 --- a/agb-gbafix/src/main.rs +++ b/agb-gbafix/src/main.rs @@ -1,10 +1,12 @@ +use anyhow::{anyhow, bail, ensure, Result}; + use std::{ - error, fs, + fs, io::{BufWriter, Write}, path::PathBuf, }; -fn main() -> Result<(), Box> { +fn main() -> Result<()> { let mut output = BufWriter::new(fs::File::create("out.gba")?); let path = PathBuf::from("tests/text_render"); @@ -17,12 +19,12 @@ fn main() -> Result<(), Box> { Ok(()) } -fn write_gba_file(input: &[u8], output: &mut W) -> Result<(), Box> { +fn write_gba_file(input: &[u8], output: &mut W) -> Result<()> { let elf_file = elf::ElfBytes::::minimal_parse(input)?; let section_headers = elf_file .section_headers() - .expect("Expected section headers"); + .ok_or_else(|| anyhow!("Failed to parse as elf file"))?; let mut header = gbafix::GBAHeader::default(); @@ -46,13 +48,13 @@ fn write_gba_file(input: &[u8], output: &mut W) -> Result<(), Box GBA_HEADER_SIZE, "first section must be at least as big as the gba header" );