Use anyhow for errors

This commit is contained in:
Gwilym Inzani 2023-04-06 23:16:38 +01:00
parent d278d50528
commit b76781799a
3 changed files with 17 additions and 7 deletions

7
agb-gbafix/Cargo.lock generated
View file

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

View file

@ -9,3 +9,4 @@ edition = "2021"
elf = "0.7"
gbafix = "1"
bytemuck = "1"
anyhow = "1"

View file

@ -1,10 +1,12 @@
use anyhow::{anyhow, bail, ensure, Result};
use std::{
error, fs,
fs,
io::{BufWriter, Write},
path::PathBuf,
};
fn main() -> Result<(), Box<dyn error::Error>> {
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<dyn error::Error>> {
Ok(())
}
fn write_gba_file<W: Write>(input: &[u8], output: &mut W) -> Result<(), Box<dyn error::Error>> {
fn write_gba_file<W: Write>(input: &[u8], output: &mut W) -> Result<()> {
let elf_file = elf::ElfBytes::<elf::endian::AnyEndian>::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<W: Write>(input: &[u8], output: &mut W) -> Result<(), Box<dyn
let (mut data, compression) = elf_file.section_data(&section_header)?;
if let Some(compression) = compression {
panic!("Cannot decompress elf content, but got compression header {compression:?}");
bail!("Cannot decompress elf content, but got compression header {compression:?}");
}
if address == GBA_START_ADDRESS {
const GBA_HEADER_SIZE: usize = 192;
assert!(
ensure!(
data.len() > GBA_HEADER_SIZE,
"first section must be at least as big as the gba header"
);