mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 01:21:34 +11:00
Use anyhow for errors
This commit is contained in:
parent
d278d50528
commit
b76781799a
7
agb-gbafix/Cargo.lock
generated
7
agb-gbafix/Cargo.lock
generated
|
@ -6,11 +6,18 @@ version = 3
|
||||||
name = "agb-gbafix"
|
name = "agb-gbafix"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
"elf",
|
"elf",
|
||||||
"gbafix",
|
"gbafix",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.70"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytemuck"
|
name = "bytemuck"
|
||||||
version = "1.13.1"
|
version = "1.13.1"
|
||||||
|
|
|
@ -8,4 +8,5 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
elf = "0.7"
|
elf = "0.7"
|
||||||
gbafix = "1"
|
gbafix = "1"
|
||||||
bytemuck = "1"
|
bytemuck = "1"
|
||||||
|
anyhow = "1"
|
|
@ -1,10 +1,12 @@
|
||||||
|
use anyhow::{anyhow, bail, ensure, Result};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
error, fs,
|
fs,
|
||||||
io::{BufWriter, Write},
|
io::{BufWriter, Write},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
fn main() -> Result<()> {
|
||||||
let mut output = BufWriter::new(fs::File::create("out.gba")?);
|
let mut output = BufWriter::new(fs::File::create("out.gba")?);
|
||||||
|
|
||||||
let path = PathBuf::from("tests/text_render");
|
let path = PathBuf::from("tests/text_render");
|
||||||
|
@ -17,12 +19,12 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
Ok(())
|
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 elf_file = elf::ElfBytes::<elf::endian::AnyEndian>::minimal_parse(input)?;
|
||||||
|
|
||||||
let section_headers = elf_file
|
let section_headers = elf_file
|
||||||
.section_headers()
|
.section_headers()
|
||||||
.expect("Expected section headers");
|
.ok_or_else(|| anyhow!("Failed to parse as elf file"))?;
|
||||||
|
|
||||||
let mut header = gbafix::GBAHeader::default();
|
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(§ion_header)?;
|
let (mut data, compression) = elf_file.section_data(§ion_header)?;
|
||||||
if let Some(compression) = compression {
|
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 {
|
if address == GBA_START_ADDRESS {
|
||||||
const GBA_HEADER_SIZE: usize = 192;
|
const GBA_HEADER_SIZE: usize = 192;
|
||||||
|
|
||||||
assert!(
|
ensure!(
|
||||||
data.len() > GBA_HEADER_SIZE,
|
data.len() > GBA_HEADER_SIZE,
|
||||||
"first section must be at least as big as the gba header"
|
"first section must be at least as big as the gba header"
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue