mizu_core/cartridge/
error.rs

1use super::mappers::MapperType;
2use std::convert::From;
3use std::fmt::Debug;
4use std::io::{Error as ioError, ErrorKind as ioErrorKind};
5
6/// An error that may occur when loading a new Cartridge file.
7#[derive(thiserror::Error, Debug)]
8pub enum CartridgeError {
9    /// File error happened while reading the ROM file.
10    #[error("File error: {0}")]
11    FileError(ioError),
12    /// The ROM file does not have valid extension.
13    #[error("The file ends with an invalid extension, should end with '.gb' or '.gbc'")]
14    ExtensionError,
15    /// The rom file does not contain a valid Nintendo logo data at `0x104`.
16    #[error("The rom file does not contain a valid Nintendo logo data at 0x104")]
17    InvalidNintendoLogo,
18    /// The game title contain invalid UTF-8 characters.
19    #[error("The game title contain invalid UTF-8 characters")]
20    InvalidGameTitle,
21    /// Rom file contain unsupported cartridge type.
22    #[error("Rom file contain unsupported cartridge type")]
23    InvalidCartridgeType,
24    /// The rom file header contain invalid `rom_size` value.
25    #[error("The rom size index {0} is invalid")]
26    InvalidRomSizeIndex(u8),
27    /// The rom file header contain invalid `ram_size` value.
28    #[error("The ram size index {0} is invalid")]
29    InvalidRamSizeIndex(u8),
30    /// The rom file size does not match the rom size provided in the rom header.
31    #[error("The file size does not match the rom size {0} bytes indicated inside the header")]
32    InvalidRomSize(usize),
33    /// The cartridge type suggest the cartridge has ram, but it is not present.
34    #[error("The cartridge type suggest the cartridge has ram, but it is not present")]
35    RamNotPresentError,
36    /// The cartridge type suggest the cartridge does not have ram, but it is present
37    #[error("The cartridge type suggest the cartridge does not have ram, but it is present")]
38    NotNeededRamPresentError,
39    /// The header of the cartridge checksum does not match the provided value in the header.
40    #[error("The header of the cartridge checksum {got} does not match the expected {expected}")]
41    InvalidChecksum { expected: u8, got: u8 },
42    /// The mapper type is not supported by the emulator.
43    #[error("The mapper {0:?} is not yet implemented")]
44    MapperNotImplemented(MapperType),
45}
46
47impl From<ioError> for CartridgeError {
48    fn from(from: ioError) -> Self {
49        Self::FileError(from)
50    }
51}
52
53#[derive(thiserror::Error, Debug)]
54pub enum SramError {
55    #[error("Could not load cartridge save file")]
56    NoSramFileFound,
57    #[error("There is a conflict in the size of SRAM save file in the Cartridge header and the file in disk")]
58    SramFileSizeDoesNotMatch,
59    #[error("Could not save cartridge save file")]
60    FailedToSaveSramFile,
61    #[error("Unknown error occured while trying to save/load cartridge save file")]
62    Others,
63}
64
65impl From<ioError> for SramError {
66    fn from(from: ioError) -> Self {
67        match from.kind() {
68            ioErrorKind::NotFound => Self::NoSramFileFound,
69            ioErrorKind::PermissionDenied => Self::FailedToSaveSramFile,
70            _ => Self::Others,
71        }
72    }
73}