livewire-rust 0.1.3

Livewire for Rust: server-rendered reactive components for Laravel for Rust.
Documentation
//! Page components: a component whose view renders the whole document.
//!
//! An embedded component is mounted from a page template with `{{ livewire(..) }}`. A page
//! component has no page template: its own `.livewire` view extends the application layout, and the
//! route answers with the component's rendered markup.
//!
//! ```text
//! {# resources/views/livewire/post-browser.livewire #}
//! {% extends "layouts.app" %}
//! {% block content %}<h1>Posts</h1>{% endblock %}
//! ```
//!
//! The route then mounts it exactly as a page would:
//!
//! ```no_run
//! # use std::sync::Arc;
//! # use livewire::ComponentRegistry;
//! # use livewire::component::Params;
//! # fn run(registry: Arc<ComponentRegistry>) -> livewire::Result<()> {
//! let html = livewire::page::render_page(&registry, "post-browser", Params::default())?;
//! # let _ = html;
//! # Ok(())
//! # }
//! ```
//!
//! # What a component gains, and does not
//!
//! The mount lifecycle, props, and the update endpoint are the same as an embedded component's; only
//! the surrounding document comes from the component. Route parameters and persistent middleware
//! belong to the application's routing layer, which passes them as props.

use std::sync::Arc;

use serde_json::Map;

use crate::component::Params;
use crate::error::LivewireError;
use crate::registry::ComponentRegistry;

/// Mounts `name` as a page and returns the markup the route answers with.
///
/// # Errors
///
/// Fails when the component is not registered, when its mount fails, or when its view renders no
/// single root element — the same failures an embedded mount reports.
pub fn render_page(registry: &Arc<ComponentRegistry>, name: &str, params: Params) -> Result<String, LivewireError> {
    let rendered = crate::render::mount(registry, name, params, None, Map::new())?;

    Ok(rendered.html)
}

#[cfg(test)]
mod tests {
    use super::render_page;
    use crate::component::Params;
    use crate::error::LivewireError;
    use crate::registry::ComponentRegistry;
    use std::sync::Arc;

    #[test]
    fn an_unregistered_page_names_the_component_it_could_not_find() {
        let registry = Arc::new(ComponentRegistry::new(Default::default()));

        let error = render_page(&registry, "missing", Params::default()).expect_err("no such component");

        assert!(
            matches!(error, LivewireError::ComponentNotFound { ref name } if name == "missing"),
            "{error}"
        );
    }
}