Skip to main content

rullst_core/
frontend.rs

1//! Modular Frontend Engines & SSR Adapters for Rullst.
2//!
3//! Provides small HTML response wrappers used by Rullst frontend compatibility
4//! profiles. These types do not install browser assets, establish a WebSocket
5//! lifecycle, hydrate Wasm, load Pico.css, or render Tera templates by
6//! themselves; those integrations remain explicit.
7
8use axum::response::{Html, IntoResponse, Response};
9
10/// Render adapter trait for frontend frameworks and components.
11pub trait RenderAdapter: Send + Sync {
12    /// Renders the component view into an HTML string.
13    fn render_to_html(&self) -> String;
14}
15
16/// HTML adapter for raw HTML string responses (`rullst::html!`).
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct HtmlAdapter(pub String);
19
20impl IntoResponse for HtmlAdapter {
21    fn into_response(self) -> Response {
22        Html(self.0).into_response()
23    }
24}
25
26/// HTML response compatibility wrapper for a page that may load Pico.css.
27#[derive(Clone, Debug, PartialEq, Eq)]
28pub struct PicoAdapter(pub String);
29
30impl IntoResponse for PicoAdapter {
31    fn into_response(self) -> Response {
32        Html(self.0).into_response()
33    }
34}
35
36/// Compatibility alias for Topcoat adapter.
37pub type TopcoatAdapter = PicoAdapter;
38
39/// HTML response compatibility wrapper for output rendered by a template engine.
40#[derive(Clone, Debug, PartialEq, Eq)]
41pub struct TemplateAdapter(pub String);
42
43impl IntoResponse for TemplateAdapter {
44    fn into_response(self) -> Response {
45        Html(self.0).into_response()
46    }
47}
48
49/// HTML response wrapper; the LiveView WebSocket lifecycle is configured separately.
50#[derive(Clone, Debug, PartialEq, Eq)]
51pub struct LiveViewAdapter<T> {
52    /// Inner component or live view state.
53    pub view: T,
54}
55
56impl<T: std::fmt::Display> LiveViewAdapter<T> {
57    /// Creates a new LiveView adapter.
58    pub fn new(view: T) -> Self {
59        Self { view }
60    }
61}
62
63impl<T: std::fmt::Display> IntoResponse for LiveViewAdapter<T> {
64    fn into_response(self) -> Response {
65        Html(self.view.to_string()).into_response()
66    }
67}
68
69/// HTML response wrapper for Wasm Island mount markup.
70#[derive(Clone, Debug, PartialEq, Eq)]
71pub struct WasmIslandAdapter<T> {
72    /// Inner component or island markup.
73    pub component: T,
74}
75
76impl<T: std::fmt::Display> WasmIslandAdapter<T> {
77    /// Creates a new Wasm Island adapter.
78    pub fn new(component: T) -> Self {
79        Self { component }
80    }
81}
82
83impl<T: std::fmt::Display> IntoResponse for WasmIslandAdapter<T> {
84    fn into_response(self) -> Response {
85        Html(self.component.to_string()).into_response()
86    }
87}
88
89/// Compatibility alias for Leptos pattern views.
90pub type LeptosAdapter<T> = LiveViewAdapter<T>;
91
92/// Compatibility alias for Dioxus pattern components.
93pub type DioxusAdapter<T> = WasmIslandAdapter<T>;