sugarloaf 0.2.21

Sugarloaf is Rio rendering engine, designed to be multiplatform. It is based on WebGPU, Rust library for Desktops and WebAssembly for Web (JavaScript). This project is created and maintained for Rio terminal purposes but feel free to use it.
Documentation
use std::sync::Arc;
use tracing::{debug, warn};

/// Simple font operations for resource management
/// Performs operations synchronously since they're already fast enough
pub struct FontOps;

impl FontOps {
    /// Create a new font operations handler
    pub fn new() -> Self {
        Self
    }

    /// Release font data synchronously
    /// Memory deallocation is already very fast (~microseconds)
    pub fn release_font_data(&self, font_data: Vec<Arc<Vec<u8>>>) {
        if !font_data.is_empty() {
            let count = font_data.len();
            debug!("Releasing {} font data entries", count);

            // Simply dropping the Arc<Vec<u8>> will deallocate the memory
            // This is extremely fast and doesn't need background threading
            drop(font_data);
        }
    }

    /// Preload fonts synchronously
    /// Currently just validates paths - real loading would happen in font loader
    pub fn preload_fonts(&self, font_paths: Vec<String>) {
        if !font_paths.is_empty() {
            let count = font_paths.len();
            debug!("Validating {} font paths", count);

            for path in &font_paths {
                if let Err(e) = std::fs::metadata(path) {
                    warn!("Font path not accessible: {}: {}", path, e);
                }
            }
        }
    }

    /// Cleanup cache synchronously
    /// Cache operations are fast HashMap/LRU operations
    pub fn cleanup_cache(&self) {
        debug!("Performing cache cleanup");
        // Cache cleanup operations are fast enough to do synchronously
        // Real implementation would clean up expired cache entries
    }
}

impl Default for FontOps {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    #[test]
    fn test_font_release() {
        let ops = FontOps::new();

        // Create some dummy font data
        let font_data = vec![Arc::new(vec![0u8; 1024]), Arc::new(vec![1u8; 2048])];

        // Should complete immediately without blocking
        ops.release_font_data(font_data);
    }

    #[test]
    fn test_font_preload() {
        let ops = FontOps::new();

        let font_paths = vec![
            "/System/Library/Fonts/Arial.ttf".to_string(),
            "/System/Library/Fonts/Helvetica.ttc".to_string(),
            "/nonexistent/path.ttf".to_string(), // Should warn but not fail
        ];

        // Should complete immediately
        ops.preload_fonts(font_paths);
    }

    #[test]
    fn test_cache_cleanup() {
        let ops = FontOps::new();

        // Should complete immediately
        ops.cleanup_cache();
    }

    #[test]
    fn test_empty_operations() {
        let ops = FontOps::new();

        // Empty operations should be no-ops
        ops.release_font_data(vec![]);
        ops.preload_fonts(vec![]);
        ops.cleanup_cache();
    }
}