gba_cart 0.4.2

GBA file header library and utilities.
Documentation
// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR MPL-2.0
// SPDX-FileCopyrightText: 2026 Gabriel Marcano <gabemarcano@yahoo.com>

use crate::error::Error;
use crate::metadata::Metadata;
use crate::metadata::MetadataRead;

use std::io;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;

/// Representation of a readable GBA cartridge.
pub struct Cart<T: Read + Seek> {
    /// Metadata associated with the GBA cartridge, primarily its header.
    pub metadata: Metadata,
    /// IO object associated with the cartridge.
    io: T,
}

impl<T: Read + Seek> Cart<T> {
    /// Creates a [`Cart`] from the provided IO object.
    ///
    /// # Errors
    ///
    /// See [`MetadataRead::read_gba_metadata`] for details.
    pub fn try_from(io: T) -> Result<Self, Error> {
        let mut io = io;
        Ok(Self {
            metadata: io.read_gba_metadata()?,
            io,
        })
    }

    /// Computers the header checksum.
    ///
    /// # Errors
    ///
    /// See [`Metadata::header_checksum`] for details.
    pub fn header_checksum(&mut self) -> Result<u8, Error> {
        self.metadata.header_checksum(&mut self.io)
    }
}

impl<T: Read + Seek> Read for Cart<T> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.io.read(buf)
    }
}

impl<T: Read + Seek> Seek for Cart<T> {
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        self.io.seek(pos)
    }
}