rustf 1.0.0

A simple MVC web framework inspired by Total.js
Documentation
//! Embedded file provider trait and global registry.
//!
//! This module allows user applications to register their own `rust-embed`-backed
//! file stores (for views and static assets) so that the framework can serve them
//! at runtime without knowing the concrete type at compile time.
//!
//! ## Usage (generated by `auto_discover` macro)
//!
//! ```rust,ignore
//! #[derive(RustEmbed)]
//! #[folder = "views/"]
//! struct AppViews;
//!
//! impl EmbeddedFileProvider for AppViews {
//!     fn get(&self, path: &str) -> Option<Vec<u8>> { ... }
//!     fn files(&self) -> Vec<String> { ... }
//! }
//!
//! register_views_provider(Box::new(AppViews));
//! register_assets_provider(Box::new(AppAssets));
//! ```

use std::sync::OnceLock;

/// A runtime-erased provider for files embedded via `rust-embed`.
///
/// Implement this trait on your `RustEmbed`-derived struct.
pub trait EmbeddedFileProvider: Send + Sync {
    /// Return the raw bytes for `path`, or `None` if not found.
    fn get(&self, path: &str) -> Option<Vec<u8>>;
    /// Return the list of all embedded file paths.
    fn files(&self) -> Vec<String>;
}

// ─── Global registries ───────────────────────────────────────────────────────

static VIEWS_PROVIDER: OnceLock<Box<dyn EmbeddedFileProvider>> = OnceLock::new();
static ASSETS_PROVIDER: OnceLock<Box<dyn EmbeddedFileProvider>> = OnceLock::new();

/// Register the application's embedded views provider.
///
/// Call once at startup (the `auto_discover` macro does this automatically).
/// Calling more than once is a no-op (the first registration wins).
pub fn register_views_provider(provider: Box<dyn EmbeddedFileProvider>) {
    let _ = VIEWS_PROVIDER.set(provider);
}

/// Register the application's embedded static-assets provider.
///
/// Call once at startup (the `auto_discover` macro does this automatically).
/// Calling more than once is a no-op (the first registration wins).
pub fn register_assets_provider(provider: Box<dyn EmbeddedFileProvider>) {
    let _ = ASSETS_PROVIDER.set(provider);
}

/// Get a reference to the registered views provider, if any.
pub fn get_views_provider() -> Option<&'static dyn EmbeddedFileProvider> {
    VIEWS_PROVIDER.get().map(|b| b.as_ref())
}

/// Get a reference to the registered assets provider, if any.
pub fn get_assets_provider() -> Option<&'static dyn EmbeddedFileProvider> {
    ASSETS_PROVIDER.get().map(|b| b.as_ref())
}