oxblade 0.2.0

Laravel Blade templates for Rust: Blade's markup and directives with Rust expressions, rendering the same HTML as Laravel. Community package, not affiliated with Laravel.
Documentation
use super::{Shape, SourceLocation, TypeDesc, TypeRegistry};

/// A model for a particular Blade view.
pub trait View: Shape + serde::Serialize {
    /// Name of the Blade template.
    const NAME: &'static str;

    /// Wraps this model for the view constructor.
    fn into_view(self) -> ViewModel<Self>
    where
        Self: Sized,
    {
        view(self)
    }
}

impl<T: View + ?Sized> View for &T {
    const NAME: &'static str = T::NAME;
}

/// A typed view and its model, ready for conversion by the framework adapter.
#[derive(Clone, Debug)]
pub struct ViewModel<T: View> {
    /// The Blade template name.
    pub name: &'static str,
    /// The original typed model.
    pub model: T,
}

/// Prepares the view selected by the model's `View` implementation.
pub fn view<T: View>(model: T) -> ViewModel<T> {
    ViewModel {
        name: T::NAME,
        model,
    }
}

/// A model intended to contribute fields to shared view data.
pub trait Shared: Shape + serde::Serialize {}

/// A linked view model and its directional shape entry point.
#[derive(Clone, Copy)]
pub struct ViewRegistration {
    /// Name of the Blade template.
    pub name: &'static str,
    /// Where the model was declared.
    pub source: SourceLocation,
    /// Describes the model's serialized data.
    pub describe: fn(&mut TypeRegistry) -> TypeDesc,
}

/// A linked shared model and its directional shape entry point.
#[derive(Clone, Copy)]
pub struct SharedRegistration {
    /// Qualified Rust name of the shared model.
    pub type_name: fn() -> &'static str,
    /// Where the model was declared.
    pub source: SourceLocation,
    /// Describes the model's serialized data.
    pub describe: fn(&mut TypeRegistry) -> TypeDesc,
}

inventory::collect!(ViewRegistration);
inventory::collect!(SharedRegistration);

/// Iterates over view models linked into this program. Inventory iteration order is unspecified.
pub fn view_models() -> impl Iterator<Item = &'static ViewRegistration> {
    inventory::iter::<ViewRegistration>.into_iter()
}

/// Iterates over shared models linked into this program. Inventory iteration order is unspecified.
pub fn shared_models() -> impl Iterator<Item = &'static SharedRegistration> {
    inventory::iter::<SharedRegistration>.into_iter()
}