libpna 0.32.1

PNA(Portable-Network-Archive) decoding and encoding library
Documentation
use super::{ChunkType, Crc32};

/// A trait representing a chunk in a PNA archive.
///
/// A chunk is the basic unit of data storage in a PNA archive. Each chunk consists of:
/// - A length field (4 bytes)
/// - A chunk type (4 bytes)
/// - The chunk data (variable length)
/// - A CRC32 checksum (4 bytes)
///
/// This trait provides the basic interface for working with chunks in a PNA archive.
///
/// # Examples
/// ```no_run
/// use libpna::{Chunk, ChunkType, RawChunk};
///
/// fn process_chunk<C: Chunk>(chunk: &C) {
///     println!("Chunk type: {:?}", chunk.ty());
///     println!("Data length: {}", chunk.length());
///     println!("CRC32: {:08x}", chunk.crc());
/// }
/// ```
pub trait Chunk {
    /// Returns the length of the chunk's data payload in bytes.
    ///
    /// This value corresponds to the `length` field stored in the chunk structure
    /// and indicates the size of the data returned by the `data()` method.
    ///
    /// # Returns
    ///
    /// The length of the chunk's data.
    #[inline]
    fn length(&self) -> u32 {
        self.data().len() as u32
    }

    /// Returns the type of the chunk.
    ///
    /// # Returns
    ///
    /// The [`ChunkType`] identifying the kind of this chunk.
    fn ty(&self) -> ChunkType;

    /// Returns the data of the chunk.
    ///
    /// # Returns
    ///
    /// A reference to the chunk data.
    fn data(&self) -> &[u8];

    /// Returns the CRC32 checksum of the chunk.
    ///
    /// # Returns
    ///
    /// The CRC32 checksum, typically calculated over the chunk's type and data.
    #[inline]
    fn crc(&self) -> u32 {
        let mut crc = Crc32::new();
        crc.update(self.ty().as_bytes());
        crc.update(self.data());
        crc.finalize()
    }
}