nds_cart 0.4.1

NDS 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::Read;
use std::io::Seek;

/// Represents the metadata held by the NDS internal header.
#[derive(Debug)]
pub struct Cart<T: Read + Seek> {
    pub metadata: Metadata,
    io: T,
}
impl<T: Read + Seek> Cart<T> {
    /// Returns a new Cart instance, using the provided Readable and Seekable object as the
    /// underlying data source representing the cart.
    ///
    /// # Errors
    ///
    /// See [`MetadataRead::read_nds_header`] for error deetails.
    pub fn try_from(io: T) -> Result<Self, Error> {
        let mut io = io;
        Ok(Self {
            metadata: io.read_nds_header()?,
            io,
        })
    }

    /// Computes the checksum of the NDS header.
    ///
    /// # Errors
    /// Returns [`Error::Io`] if there are any issues reading or seeking the underlying data.
    pub fn header_checksum(&mut self) -> Result<u16, Error> {
        self.metadata.header_checksum(&mut self.io)
    }

    /// Returns the NDS icon a 32x32 RGBA8888 image.
    ///
    /// # Errors
    /// Returns [`Error::Io`] if there are any issues reading or seeking the underlying data.
    pub fn extract_rgba_icon(&mut self) -> Result<Vec<u8>, Error> {
        self.metadata.extract_rgba_icon(&mut self.io)
    }
}