rocketsplash-formats 0.2.2

Shared data types and serialization formats for Rocketsplash TUI animations
Documentation
// <FILE>crates/rocketsplash-formats/src/limits.rs</FILE>
// <DESC>Safety limits and allocation tracking for serialized assets</DESC>
// <VERS>VERSION: 1.0.0</VERS>
// <WCTX>Runtime library implementation</WCTX>
// <CLOG>Add dimension validation and allocation tracker</CLOG>

/// Maximum width in terminal columns for any glyph or splash.
pub const MAX_WIDTH: u32 = 4096;

/// Maximum height in terminal rows for any glyph or splash.
pub const MAX_HEIGHT: u32 = 4096;

/// Maximum number of glyphs in a font atlas.
pub const MAX_GLYPHS: usize = 65_536;

/// Maximum total memory allocation for a single font atlas (256 MB).
pub const MAX_TOTAL_ALLOCATION: usize = 256 * 1024 * 1024;

/// Validate dimensions before any allocation.
pub fn validate_dimensions(width: u32, height: u32) -> Result<usize, String> {
    if width > MAX_WIDTH {
        return Err(format!("Width {} exceeds maximum {}", width, MAX_WIDTH));
    }
    if height > MAX_HEIGHT {
        return Err(format!("Height {} exceeds maximum {}", height, MAX_HEIGHT));
    }
    let w = width as usize;
    let h = height as usize;
    w.checked_mul(h)
        .ok_or_else(|| format!("Dimensions {}x{} overflow", width, height))
}

/// Track cumulative allocation during font loading.
#[derive(Debug, Default)]
pub struct AllocationTracker {
    total: usize,
    limit: usize,
}

impl AllocationTracker {
    pub fn new() -> Self {
        Self {
            total: 0,
            limit: MAX_TOTAL_ALLOCATION,
        }
    }

    pub fn reserve(&mut self, bytes: usize) -> Result<(), String> {
        let new_total = self
            .total
            .checked_add(bytes)
            .ok_or_else(|| "Allocation overflow".to_string())?;
        if new_total > self.limit {
            return Err(format!(
                "Total allocation {} exceeds limit {} bytes",
                new_total, self.limit
            ));
        }
        self.total = new_total;
        Ok(())
    }
}

// <FILE>crates/rocketsplash-formats/src/limits.rs</FILE>
// <VERS>END OF VERSION: 1.0.0</VERS>