pkmn_rom_extract/
lib.rs

1//! <style>
2//! .rustdoc-hidden { display: none; }
3//! </style>
4#![doc = include_str!("../README.md")]
5#![cfg_attr(not(feature = "std"), no_std)]
6#![forbid(unsafe_code)]
7
8extern crate alloc;
9
10use core::fmt;
11use core::result;
12
13pub mod gba;
14pub mod graphics;
15pub mod lzss;
16
17use graphics::GraphicsError;
18
19/// Error type used by this crate.
20#[derive(Debug)]
21pub enum RomError {
22    #[cfg(feature = "std")]
23    IoError(std::io::Error),
24    OutOfBounds,
25    LzssError,
26    HeaderFormat,
27    UnsupportedGame([u8; 4]),
28    GraphicsError(GraphicsError),
29    Filesize,
30    BadFile,
31}
32
33// TODO improve these messages
34impl fmt::Display for RomError {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        match self {
37            #[cfg(feature = "std")]
38            RomError::IoError(e) => e.fmt(f),
39            RomError::OutOfBounds => write!(f, "Out of bounds."),
40            RomError::LzssError => write!(f, "LZSS error."),
41            RomError::HeaderFormat => write!(f, "Header error."),
42            RomError::UnsupportedGame(c) => {
43                write!(f, "unsupported game code: {}", c.escape_ascii())
44            }
45            RomError::GraphicsError(e) => e.fmt(f),
46            RomError::Filesize => write!(f, "File size error."),
47            RomError::BadFile => write!(f, "Error in parsing ROM data."),
48        }
49    }
50}
51
52#[cfg(feature = "std")]
53impl From<std::io::Error> for RomError {
54    fn from(err: std::io::Error) -> RomError {
55        RomError::IoError(err)
56    }
57}
58
59impl From<GraphicsError> for RomError {
60    fn from(err: GraphicsError) -> RomError {
61        RomError::GraphicsError(err)
62    }
63}
64
65/// Result type used by this crate.
66pub type Result<T> = result::Result<T, RomError>;