Añade soporte para responder páginas HTML

- Amplia la estructura "Page" para trabajar el renderizado con regiones
  de componentes para componer la página.
- Añade acciones "BeforeRenderBody" y "AfterRenderBody" para alterar el
  contenido de la página antes y después del renderizado.
- Actualiza "Context" para admitir parámetros dinámicos y mejorar la
  gestión de temas.
- Implementa el manejo de errores HTTP respondiendo páginas.
- Mejora la documentación y reorganiza el código en varios módulos.
This commit is contained in:
Manuel Cillero 2025-07-27 21:24:49 +02:00
parent f23c8d5c4c
commit a1bb6cd12d
17 changed files with 669 additions and 143 deletions

View file

@ -13,3 +13,5 @@ pub type FnActionWithComponent<C> = fn(component: &mut C, cx: &mut Context);
pub mod component;
pub mod theme;
pub mod page;

View file

@ -22,7 +22,7 @@ impl<C: ComponentTrait> ActionDispatcher for AfterRender<C> {
self.referer_id.get()
}
/// Devuelve el peso para definir el orden de aplicación.
/// Devuelve el peso para definir el orden de ejecución.
fn weight(&self) -> Weight {
self.weight
}

View file

@ -22,7 +22,7 @@ impl<C: ComponentTrait> ActionDispatcher for BeforeRender<C> {
self.referer_id.get()
}
/// Devuelve el peso para definir el orden de aplicación.
/// Devuelve el peso para definir el orden de ejecución.
fn weight(&self) -> Weight {
self.weight
}

View file

@ -27,7 +27,7 @@ impl<C: ComponentTrait> ActionDispatcher for IsRenderable<C> {
self.referer_id.get()
}
/// Devuelve el peso para definir el orden de aplicación.
/// Devuelve el peso para definir el orden de ejecución.
fn weight(&self) -> Weight {
self.weight
}

17
src/base/action/page.rs Normal file
View file

@ -0,0 +1,17 @@
//! Acciones para alterar el contenido de las páginas a renderizar.
use crate::response::page::Page;
/// Tipo de función para manipular una página durante su construcción o renderizado.
///
/// Se emplea en acciones orientadas a modificar o inspeccionar una instancia de [`Page`]
/// directamente, sin acceder a los componentes individuales ni al contexto de renderizado.
///
/// Recibe una referencia mutable (`&mut`) a la página en cuestión.
pub type FnActionWithPage = fn(page: &mut Page);
mod before_render_body;
pub use before_render_body::*;
mod after_render_body;
pub use after_render_body::*;

View file

@ -0,0 +1,46 @@
use crate::prelude::*;
use crate::base::action::page::FnActionWithPage;
/// Ejecuta [`FnActionWithPage`](crate::base::action::page::FnActionWithPage) después de renderizar
/// el cuerpo de la página.
///
/// Este tipo de acción se despacha después de renderizar el contenido principal de la página
/// (`<body>`), permitiendo ajustes finales sobre la instancia [`Page`].
///
/// Las acciones se ejecutan en orden según el [`Weight`] asignado.
pub struct AfterRenderBody {
f: FnActionWithPage,
weight: Weight,
}
impl ActionDispatcher for AfterRenderBody {
/// Devuelve el peso para definir el orden de ejecución.
fn weight(&self) -> Weight {
self.weight
}
}
impl AfterRenderBody {
/// Permite [registrar](ExtensionTrait::actions) una nueva acción
/// [`FnActionWithPage`](crate::base::action::page::FnActionWithPage).
pub fn new(f: FnActionWithPage) -> Self {
AfterRenderBody { f, weight: 0 }
}
/// Opcional. Acciones con pesos más bajos se aplican antes. Se pueden usar valores negativos.
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
// Despacha las acciones.
#[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions(
&ActionKey::new(UniqueId::of::<Self>(), None, None, None),
|action: &Self| (action.f)(page),
);
}
}

View file

@ -0,0 +1,46 @@
use crate::prelude::*;
use crate::base::action::page::FnActionWithPage;
/// Ejecuta [`FnActionWithPage`](crate::base::action::page::FnActionWithPage) antes de renderizar
/// el cuerpo de la página.
///
/// Este tipo de acción se despacha antes de renderizar el contenido principal de la página
/// (`<body>`), permitiendo ajustes sobre la instancia [`Page`].
///
/// Las acciones se ejecutan en orden según el [`Weight`] asignado.
pub struct BeforeRenderBody {
f: FnActionWithPage,
weight: Weight,
}
impl ActionDispatcher for BeforeRenderBody {
/// Devuelve el peso para definir el orden de ejecución.
fn weight(&self) -> Weight {
self.weight
}
}
impl BeforeRenderBody {
/// Permite [registrar](ExtensionTrait::actions) una nueva acción
/// [`FnActionWithPage`](crate::base::action::page::FnActionWithPage).
pub fn new(f: FnActionWithPage) -> Self {
BeforeRenderBody { f, weight: 0 }
}
/// Opcional. Acciones con pesos más bajos se aplican antes. Se pueden usar valores negativos.
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
// Despacha las acciones.
#[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions(
&ActionKey::new(UniqueId::of::<Self>(), None, None, None),
|action: &Self| (action.f)(page),
);
}
}

View file

@ -77,8 +77,8 @@ impl<C: ComponentTrait> Typed<C> {
// Typed HELPERS *******************************************************************************
/// Convierte el componente tipado en un [`Child`].
fn to_child(&self) -> Child {
// Convierte el componente tipado en un [`Child`].
fn into_child(self) -> Child {
Child(self.0.clone())
}
}
@ -155,12 +155,12 @@ impl Children {
#[builder_fn]
pub fn with_typed<C: ComponentTrait + Default>(mut self, op: TypedOp<C>) -> Self {
match op {
TypedOp::Add(typed) => self.add(typed.to_child()),
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.to_child()),
TypedOp::InsertBeforeId(id, typed) => self.insert_before_id(id, typed.to_child()),
TypedOp::Prepend(typed) => self.prepend(typed.to_child()),
TypedOp::Add(typed) => self.add(typed.into_child()),
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.into_child()),
TypedOp::InsertBeforeId(id, typed) => self.insert_before_id(id, typed.into_child()),
TypedOp::Prepend(typed) => self.prepend(typed.into_child()),
TypedOp::RemoveById(id) => self.remove_by_id(id),
TypedOp::ReplaceById(id, typed) => self.replace_by_id(id, typed.to_child()),
TypedOp::ReplaceById(id, typed) => self.replace_by_id(id, typed.into_child()),
TypedOp::Reset => self.reset(),
}
}
@ -174,6 +174,48 @@ impl Children {
self
}
// Children GETTERS ****************************************************************************
/// Devuelve el número de componentes hijo de la lista.
pub fn len(&self) -> usize {
self.0.len()
}
/// Indica si la lista está vacía.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Devuelve el primer componente hijo con el identificador indicado, si existe.
pub fn get_by_id(&self, id: impl AsRef<str>) -> Option<&Child> {
let id = Some(id.as_ref());
self.0.iter().find(|c| c.id().as_deref() == id)
}
/// Devuelve un iterador sobre los componentes hijo con el identificador indicado.
pub fn iter_by_id<'a>(&'a self, id: &'a str) -> impl Iterator<Item = &'a Child> + 'a {
self.0.iter().filter(move |c| c.id().as_deref() == Some(id))
}
/// Devuelve un iterador sobre los componentes hijo con el identificador de tipo ([`UniqueId`])
/// indicado.
pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator<Item = &Child> {
self.0.iter().filter(move |&c| c.type_id() == type_id)
}
// Children RENDER *****************************************************************************
/// Renderiza todos los componentes hijo, en orden.
pub fn render(&self, cx: &mut Context) -> Markup {
html! {
@for c in &self.0 {
(c.render(cx))
}
}
}
// Children HELPERS ****************************************************************************
// Inserta un hijo después del componente con el `id` dado, o al final si no se encuentra.
#[inline]
fn insert_after_id(&mut self, id: impl AsRef<str>, child: Child) -> &mut Self {
@ -232,46 +274,6 @@ impl Children {
self.0.clear();
self
}
// Children GETTERS ****************************************************************************
/// Devuelve el número de componentes hijo de la lista.
pub fn len(&self) -> usize {
self.0.len()
}
/// Indica si la lista está vacía.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Devuelve el primer componente hijo con el identificador indicado, si existe.
pub fn get_by_id(&self, id: impl AsRef<str>) -> Option<&Child> {
let id = Some(id.as_ref());
self.0.iter().find(|c| c.id().as_deref() == id)
}
/// Devuelve un iterador sobre los componentes hijo con el identificador indicado.
pub fn iter_by_id<'a>(&'a self, id: &'a str) -> impl Iterator<Item = &'a Child> + 'a {
self.0.iter().filter(move |c| c.id().as_deref() == Some(id))
}
/// Devuelve un iterador sobre los componentes hijo con el identificador tipo ([`UniqueId`])
/// indicado.
pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator<Item = &Child> {
self.0.iter().filter(move |&c| c.type_id() == type_id)
}
// Children RENDER *****************************************************************************
/// Renderiza todos los componentes hijo, en orden.
pub fn render(&self, cx: &mut Context) -> Markup {
html! {
@for c in &self.0 {
(c.render(cx))
}
}
}
}
impl IntoIterator for Children {

View file

@ -22,3 +22,6 @@ pub(crate) use regions::ChildrenInRegions;
pub use regions::InRegion;
pub(crate) mod all;
/// Nombre de la región por defecto: `content`.
pub const CONTENT_REGION_NAME: &str = "content";

View file

@ -20,8 +20,8 @@ pub static DEFAULT_THEME: LazyLock<ThemeRef> =
// TEMA POR NOMBRE *********************************************************************************
/// Devuelve el tema identificado por su [`short_name`](AnyInfo::short_name).
pub fn theme_by_short_name(short_name: impl AsRef<str>) -> Option<ThemeRef> {
let short_name = short_name.as_ref().to_lowercase();
pub fn theme_by_short_name(short_name: &'static str) -> Option<ThemeRef> {
let short_name = short_name.to_lowercase();
match THEMES
.read()
.iter()

View file

@ -1,5 +1,9 @@
use crate::core::extension::ExtensionTrait;
use crate::core::theme::CONTENT_REGION_NAME;
use crate::global;
use crate::html::{html, Markup};
use crate::locale::L10n;
use crate::response::page::Page;
/// Representa una referencia a un tema.
///
@ -30,6 +34,66 @@ pub type ThemeRef = &'static dyn ThemeTrait;
/// ```
pub trait ThemeTrait: ExtensionTrait + Send + Sync {
fn regions(&self) -> Vec<(&'static str, L10n)> {
vec![("content", L10n::l("content"))]
vec![(CONTENT_REGION_NAME, L10n::l("content"))]
}
#[allow(unused_variables)]
fn before_render_page_body(&self, page: &mut Page) {}
fn render_page_body(&self, page: &mut Page) -> Markup {
html! {
body id=[page.body_id().get()] class=[page.body_classes().get()] {
@for (region_name, _) in self.regions() {
@let output = page.render_region(region_name);
@if !output.is_empty() {
div id=(region_name) class={ "region-container region-" (region_name) } {
(output)
}
}
}
}
}
}
#[allow(unused_variables)]
fn after_render_page_body(&self, page: &mut Page) {}
fn render_page_head(&self, page: &mut Page) -> Markup {
let viewport = "width=device-width, initial-scale=1, shrink-to-fit=no";
html! {
head {
meta charset="utf-8";
@if let Some(title) = page.title() {
title { (global::SETTINGS.app.name) (" | ") (title) }
} @else {
title { (global::SETTINGS.app.name) }
}
@if let Some(description) = page.description() {
meta name="description" content=(description);
}
meta name="viewport" content=(viewport);
@for (name, content) in page.metadata() {
meta name=(name) content=(content) {}
}
meta http-equiv="X-UA-Compatible" content="IE=edge";
@for (property, content) in page.properties() {
meta property=(property) content=(content) {}
}
(page.render_assets())
}
}
}
fn error403(&self, _page: &mut Page) -> Markup {
html! { div { h1 { ("FORBIDDEN ACCESS") } } }
}
fn error404(&self, _page: &mut Page) -> Markup {
html! { div { h1 { ("RESOURCE NOT FOUND") } } }
}
}

View file

@ -1,5 +1,5 @@
use crate::core::component::{Child, ChildOp, Children};
use crate::core::theme::ThemeRef;
use crate::core::theme::{ThemeRef, CONTENT_REGION_NAME};
use crate::{builder_fn, AutoDefault, UniqueId};
use parking_lot::RwLock;
@ -7,45 +7,43 @@ use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::LazyLock;
// Regiones globales con componentes para un tema dado.
static THEME_REGIONS: LazyLock<RwLock<HashMap<UniqueId, ChildrenInRegions>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));
// Regiones globales con componentes para cualquier tema.
static COMMON_REGIONS: LazyLock<RwLock<ChildrenInRegions>> =
LazyLock::new(|| RwLock::new(ChildrenInRegions::default()));
// Estructura interna para mantener los componentes de cada región dada.
// Estructura interna para mantener los componentes de una región.
#[derive(AutoDefault)]
pub struct ChildrenInRegions(HashMap<&'static str, Children>);
impl ChildrenInRegions {
pub fn new() -> Self {
ChildrenInRegions::default()
}
pub fn with(region_id: &'static str, child: Child) -> Self {
ChildrenInRegions::default().with_in_region(region_id, ChildOp::Add(child))
pub fn with(region_name: &'static str, child: Child) -> Self {
ChildrenInRegions::default().with_child_in_region(region_name, ChildOp::Add(child))
}
#[builder_fn]
pub fn with_in_region(mut self, region_id: &'static str, op: ChildOp) -> Self {
if let Some(region) = self.0.get_mut(region_id) {
pub fn with_child_in_region(mut self, region_name: &'static str, op: ChildOp) -> Self {
if let Some(region) = self.0.get_mut(region_name) {
region.alter_child(op);
} else {
self.0.insert(region_id, Children::new().with_child(op));
self.0.insert(region_name, Children::new().with_child(op));
}
self
}
pub fn all_in_region(&self, theme: ThemeRef, region_id: &str) -> Children {
pub fn merge_all_components(&self, theme_ref: ThemeRef, region_name: &'static str) -> Children {
let common = COMMON_REGIONS.read();
if let Some(r) = THEME_REGIONS.read().get(&theme.type_id()) {
if let Some(r) = THEME_REGIONS.read().get(&theme_ref.type_id()) {
Children::merge(&[
common.0.get(region_id),
self.0.get(region_id),
r.0.get(region_id),
common.0.get(region_name),
self.0.get(region_name),
r.0.get(region_name),
])
} else {
Children::merge(&[common.0.get(region_id), self.0.get(region_id)])
Children::merge(&[common.0.get(region_name), self.0.get(region_name)])
}
}
}
@ -90,19 +88,22 @@ impl InRegion {
InRegion::Content => {
COMMON_REGIONS
.write()
.alter_in_region("region-content", ChildOp::Add(child));
.alter_child_in_region(CONTENT_REGION_NAME, ChildOp::Add(child));
}
InRegion::Named(name) => {
COMMON_REGIONS
.write()
.alter_in_region(name, ChildOp::Add(child));
.alter_child_in_region(name, ChildOp::Add(child));
}
InRegion::OfTheme(region, theme) => {
InRegion::OfTheme(region_name, theme_ref) => {
let mut regions = THEME_REGIONS.write();
if let Some(r) = regions.get_mut(&theme.type_id()) {
r.alter_in_region(region, ChildOp::Add(child));
if let Some(r) = regions.get_mut(&theme_ref.type_id()) {
r.alter_child_in_region(region_name, ChildOp::Add(child));
} else {
regions.insert(theme.type_id(), ChildrenInRegions::with(region, child));
regions.insert(
theme_ref.type_id(),
ChildrenInRegions::with(region_name, child),
);
}
}
}

View file

@ -61,37 +61,42 @@ impl Error for ErrorParam {}
/// hojas de estilo ([`StyleSheet`]) y *scripts* ([`JavaScript`]), así como parámetros de contexto
/// definidos en tiempo de ejecución.
///
/// # Ejemplo
/// # Ejemplos
///
/// Crea un nuevo contexto asociado a una solicitud HTTP:
///
/// ```rust
/// use pagetop::prelude::*;
///
/// fn configure_context(cx: &mut Context) {
/// fn new_context(request: HttpRequest) -> Context {
/// Context::new(request)
/// // Establece el idioma del documento a español.
/// cx.alter_langid(LangMatch::langid_or_default("es-ES"))
/// .with_langid(LangMatch::langid_or_default("es-ES"))
/// // Selecciona un tema (por su nombre corto).
/// .alter_theme("aliner")
/// // Añade un parámetro dinámico al contexto.
/// .alter_param("usuario_id", 42)
/// .with_theme("aliner")
/// // Asigna un favicon.
/// .alter_assets(AssetsOp::SetFavicon(Some(
/// Favicon::new().with_icon("/icons/favicon.ico")
/// )))
/// .with_assets(AssetsOp::SetFavicon(Some(Favicon::new().with_icon("/favicon.ico"))))
/// // Añade una hoja de estilo externa.
/// .alter_assets(AssetsOp::AddStyleSheet(
/// StyleSheet::from("/css/style.css")
/// ))
/// .with_assets(AssetsOp::AddStyleSheet(StyleSheet::from("/css/style.css")))
/// // Añade un script JavaScript.
/// .alter_assets(AssetsOp::AddJavaScript(
/// JavaScript::defer("/js/main.js")
/// ));
/// .with_assets(AssetsOp::AddJavaScript(JavaScript::defer("/js/main.js")))
/// // Añade un parámetro dinámico al contexto.
/// .with_param("usuario_id", 42)
/// }
/// ```
///
/// Y hace operaciones con un contexto dado:
///
/// ```rust
/// use pagetop::prelude::*;
///
/// fn use_context(cx: &mut Context) {
/// // Recupera el tema seleccionado.
/// let active_theme = cx.theme();
/// assert_eq!(active_theme.short_name(), "aliner");
///
/// // Recupera el parámetro a su tipo original.
/// let id: i32 = cx.param("usuario_id").unwrap();
/// let id: i32 = cx.get_param("usuario_id").unwrap();
/// assert_eq!(id, 42);
///
/// // Genera un identificador para un componente de tipo `Menu`.
@ -103,31 +108,32 @@ impl Error for ErrorParam {}
#[rustfmt::skip]
pub struct Context {
request : HttpRequest, // Solicitud HTTP de origen.
langid : &'static LanguageIdentifier, // Identificador del idioma.
langid : &'static LanguageIdentifier, // Identificador de idioma.
theme : ThemeRef, // Referencia al tema para renderizar.
layout : &'static str, // Composición del documento para renderizar.
params : HashMap<String, String>, // Parámetros definidos en tiempo de ejecución.
favicon : Option<Favicon>, // Favicon, si se ha definido.
stylesheets: Assets<StyleSheet>, // Hojas de estilo CSS.
javascripts: Assets<JavaScript>, // Scripts JavaScript.
params : HashMap<&'static str, String>, // Parámetros definidos en tiempo de ejecución.
id_counter : usize, // Contador para generar identificadores únicos.
}
impl Context {
// Crea un nuevo contexto asociado a una solicitud HTTP.
//
// El contexto inicializa el idioma por defecto, sin favicon ni recursos cargados.
/// Crea un nuevo contexto asociado a una solicitud HTTP.
///
/// El contexto inicializa el idioma, tema y composición por defecto, sin favicon ni recursos
/// cargados.
#[rustfmt::skip]
pub(crate) fn new(request: HttpRequest) -> Self {
pub fn new(request: HttpRequest) -> Self {
Context {
request,
langid : &DEFAULT_LANGID,
theme : *DEFAULT_THEME,
layout : "default",
params : HashMap::<String, String>::new(),
favicon : None,
stylesheets: Assets::<StyleSheet>::new(),
javascripts: Assets::<JavaScript>::new(),
params : HashMap::<&str, String>::new(),
id_counter : 0,
}
}
@ -141,37 +147,24 @@ impl Context {
self
}
/// Establece el tema que se usará para renderizar el documento.
/// Modifica el tema que se usará para renderizar el documento.
///
/// Localiza el tema por su [`short_name`](crate::core::AnyInfo::short_name), y si no aplica
/// ninguno entonces usará el tema por defecto.
#[builder_fn]
pub fn with_theme(mut self, theme_name: impl AsRef<str>) -> Self {
pub fn with_theme(mut self, theme_name: &'static str) -> Self {
self.theme = theme_by_short_name(theme_name).unwrap_or(*DEFAULT_THEME);
self
}
/// Define el tipo de composición usado para renderizar el documento.
/// Modifica la composición para renderizar el documento.
#[builder_fn]
pub fn with_layout(mut self, layout_name: &'static str) -> Self {
self.layout = layout_name;
self
}
/// Añade o modifica un parámetro del contexto almacenando el valor como [`String`].
#[builder_fn]
pub fn with_param<T: ToString>(mut self, key: impl AsRef<str>, value: T) -> Self {
self.params
.insert(key.as_ref().to_string(), value.to_string());
self
}
/// Elimina un parámetro del contexto. Devuelve `true` si existía y se eliminó.
pub fn remove_param(&mut self, key: impl AsRef<str>) -> bool {
self.params.remove(key.as_ref()).is_some()
}
/// Modifica información o recursos del contexto usando [`AssetsOp`].
/// Define los recursos del contexto usando [`AssetsOp`].
#[builder_fn]
pub fn with_assets(mut self, op: AssetsOp) -> Self {
match op {
@ -209,7 +202,7 @@ impl Context {
&self.request
}
/// Devuelve el identificador del idioma asociado al documento.
/// Devuelve el identificador de idioma asociado al documento.
pub fn langid(&self) -> &LanguageIdentifier {
self.langid
}
@ -219,23 +212,11 @@ impl Context {
self.theme
}
/// Devuelve el tipo de composición usado para renderizar el documento. El valor predeterminado
/// es `"default"`.
/// Devuelve la composición para renderizar el documento. Por defecto es `"default"`.
pub fn layout(&self) -> &str {
self.layout
}
/// Recupera un parámetro del contexto convertido al tipo especificado.
///
/// Devuelve un error si el parámetro no existe ([`ErrorParam::NotFound`]) o la conversión falla
/// ([`ErrorParam::ParseError`]).
pub fn param<T: FromStr>(&self, key: impl AsRef<str>) -> Result<T, ErrorParam> {
self.params
.get(key.as_ref())
.ok_or(ErrorParam::NotFound)
.and_then(|v| T::from_str(v).map_err(|_| ErrorParam::ParseError(v.clone())))
}
// Context RENDER ******************************************************************************
/// Renderiza los recursos del contexto.
@ -249,6 +230,31 @@ impl Context {
}
}
// Context PARAMS ******************************************************************************
/// Añade o modifica un parámetro del contexto almacenando el valor como [`String`].
#[builder_fn]
pub fn with_param<T: ToString>(mut self, key: &'static str, value: T) -> Self {
self.params.insert(key, value.to_string());
self
}
/// Recupera un parámetro del contexto convertido al tipo especificado.
///
/// Devuelve un error si el parámetro no existe ([`ErrorParam::NotFound`]) o la conversión falla
/// ([`ErrorParam::ParseError`]).
pub fn get_param<T: FromStr>(&self, key: &'static str) -> Result<T, ErrorParam> {
self.params
.get(key)
.ok_or(ErrorParam::NotFound)
.and_then(|v| T::from_str(v).map_err(|_| ErrorParam::ParseError(v.clone())))
}
/// Elimina un parámetro del contexto. Devuelve `true` si existía y se eliminó.
pub fn remove_param(&mut self, key: &'static str) -> bool {
self.params.remove(key).is_some()
}
// Context EXTRAS ******************************************************************************
/// Genera un identificador único si no se proporciona uno explícito.

View file

@ -43,7 +43,7 @@ pub use crate::core::component::*;
pub use crate::core::extension::*;
pub use crate::core::theme::*;
pub use crate::response::{json::*, redirect::*, ResponseError};
pub use crate::response::{json::*, page::*, redirect::*, ResponseError};
pub use crate::base::action;
pub use crate::base::component::*;

View file

@ -2,6 +2,8 @@
pub use actix_web::ResponseError;
pub mod page;
pub mod json;
pub mod redirect;

249
src/response/page.rs Normal file
View file

@ -0,0 +1,249 @@
mod error;
pub use error::ErrorPage;
pub use actix_web::Result as ResultPage;
use crate::base::action;
use crate::builder_fn;
use crate::core::component::{Child, ChildOp, ComponentTrait};
use crate::core::theme::{ChildrenInRegions, ThemeRef, CONTENT_REGION_NAME};
use crate::html::{html, AssetsOp, Context, Markup, DOCTYPE};
use crate::html::{ClassesOp, OptionClasses, OptionId, OptionTranslated};
use crate::locale::{CharacterDirection, L10n, LanguageIdentifier};
use crate::service::HttpRequest;
/// Representa una página HTML completa lista para renderizar.
///
/// Una instancia de `Page` se compone dinámicamente permitiendo establecer título, descripción,
/// regiones donde disponer los componentes, atributos de `<body>` y otros aspectos del contexto de
/// renderizado.
#[rustfmt::skip]
pub struct Page {
title : OptionTranslated,
description : OptionTranslated,
metadata : Vec<(&'static str, &'static str)>,
properties : Vec<(&'static str, &'static str)>,
context : Context,
body_id : OptionId,
body_classes: OptionClasses,
regions : ChildrenInRegions,
}
impl Page {
/// Crea una nueva instancia de página con contexto basado en la petición HTTP.
#[rustfmt::skip]
pub fn new(request: HttpRequest) -> Self {
Page {
title : OptionTranslated::default(),
description : OptionTranslated::default(),
metadata : Vec::default(),
properties : Vec::default(),
context : Context::new(request),
body_id : OptionId::default(),
body_classes: OptionClasses::default(),
regions : ChildrenInRegions::default(),
}
}
// Page BUILDER ********************************************************************************
/// Establece el título de la página como un valor traducible.
#[builder_fn]
pub fn with_title(mut self, title: L10n) -> Self {
self.title.alter_value(title);
self
}
/// Establece la descripción de la página como un valor traducible.
#[builder_fn]
pub fn with_description(mut self, description: L10n) -> Self {
self.description.alter_value(description);
self
}
/// Añade una entrada `<meta name="..." content="...">` al `<head>`.
#[builder_fn]
pub fn with_metadata(mut self, name: &'static str, content: &'static str) -> Self {
self.metadata.push((name, content));
self
}
/// Añade una entrada `<meta property="..." content="...">` al `<head>`.
#[builder_fn]
pub fn with_property(mut self, property: &'static str, content: &'static str) -> Self {
self.metadata.push((property, content));
self
}
/// Modifica el identificador de idioma de la página ([`Context::with_langid`]).
#[builder_fn]
pub fn with_langid(mut self, langid: &'static LanguageIdentifier) -> Self {
self.context.alter_langid(langid);
self
}
/// Modifica el tema que se usará para renderizar la página ([`Context::with_theme`]).
#[builder_fn]
pub fn with_theme(mut self, theme_name: &'static str) -> Self {
self.context.alter_theme(theme_name);
self
}
/// Modifica la composición para renderizar la página ([`Context::with_layout`]).
#[builder_fn]
pub fn with_layout(mut self, layout_name: &'static str) -> Self {
self.context.alter_layout(layout_name);
self
}
/// Define los recursos de la página usando [`AssetsOp`].
#[builder_fn]
pub fn with_assets(mut self, op: AssetsOp) -> Self {
self.context.alter_assets(op);
self
}
/// Establece el atributo `id` del elemento `<body>`.
#[builder_fn]
pub fn with_body_id(mut self, id: impl AsRef<str>) -> Self {
self.body_id.alter_value(id);
self
}
/// Modifica las clases CSS del elemento `<body>` con una operación sobre [`OptionClasses`].
#[builder_fn]
pub fn with_body_classes(mut self, op: ClassesOp, classes: impl AsRef<str>) -> Self {
self.body_classes.alter_value(op, classes);
self
}
/// Añade un componente a la región de contenido por defecto.
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.regions
.alter_child_in_region(CONTENT_REGION_NAME, ChildOp::Add(Child::with(component)));
self
}
/// Añade un componente en una región (`region_name`) de la página.
pub fn with_component_in(
mut self,
region_name: &'static str,
component: impl ComponentTrait,
) -> Self {
self.regions
.alter_child_in_region(region_name, ChildOp::Add(Child::with(component)));
self
}
/// Opera con [`ChildOp`] en una región (`region_name`) de la página.
#[builder_fn]
pub fn with_child_in_region(mut self, region_name: &'static str, op: ChildOp) -> Self {
self.regions.alter_child_in_region(region_name, op);
self
}
// Page GETTERS ********************************************************************************
/// Devuelve el título traducido para el idioma activo, si existe.
pub fn title(&mut self) -> Option<String> {
self.title.using(self.context.langid())
}
/// Devuelve la descripción traducida para el idioma activo, si existe.
pub fn description(&mut self) -> Option<String> {
self.description.using(self.context.langid())
}
/// Devuelve la lista de metadatos `<meta name=...>`.
pub fn metadata(&self) -> &Vec<(&str, &str)> {
&self.metadata
}
/// Devuelve la lista de propiedades `<meta property=...>`.
pub fn properties(&self) -> &Vec<(&str, &str)> {
&self.properties
}
/// Devuelve la solicitud HTTP asociada.
pub fn request(&self) -> &HttpRequest {
self.context.request()
}
/// Devuelve el identificador de idioma asociado.
pub fn langid(&self) -> &LanguageIdentifier {
self.context.langid()
}
/// Devuelve el tema que se usará para renderizar la página.
pub fn theme(&self) -> ThemeRef {
self.context.theme()
}
/// Devuelve la composición para renderizar la página. Por defecto es `"default"`.
pub fn layout(&self) -> &str {
self.context.layout()
}
/// Devuelve el identificador del elemento `<body>`.
pub fn body_id(&self) -> &OptionId {
&self.body_id
}
/// Devuelve las clases CSS del elemento `<body>`.
pub fn body_classes(&self) -> &OptionClasses {
&self.body_classes
}
// Page RENDER *********************************************************************************
/// Renderiza los componentes de una región (`regiona_name`) de la página.
pub fn render_region(&mut self, region_name: &'static str) -> Markup {
self.regions
.merge_all_components(self.context.theme(), region_name)
.render(&mut self.context)
}
/// Renderiza los recursos de la página.
pub fn render_assets(&self) -> Markup {
self.context.render_assets()
}
/// Renderiza la página completa en formato HTML.
///
/// Ejecuta las acciones correspondientes antes y después de renderizar el `<body>`,
/// así como del `<head>`, e inserta los atributos `lang` y `dir` en la etiqueta `<html>`.
pub fn render(&mut self) -> ResultPage<Markup, ErrorPage> {
// Acciones específicas del tema antes de renderizar el <body>.
self.context.theme().before_render_page_body(self);
// Acciones de las extensiones antes de renderizar el <body>.
action::page::BeforeRenderBody::dispatch(self);
// Renderiza el <body>.
let body = self.context.theme().render_page_body(self);
// Acciones específicas del tema después de renderizar el <body>.
self.context.theme().after_render_page_body(self);
// Acciones de las extensiones después de renderizar el <body>.
action::page::AfterRenderBody::dispatch(self);
// Renderiza el <head>.
let head = self.context.theme().render_page_head(self);
// Compone la página incluyendo los atributos de idioma y dirección del texto.
let lang = &self.context.langid().language;
let dir = match self.context.langid().character_direction() {
CharacterDirection::LTR => "ltr",
CharacterDirection::RTL => "rtl",
CharacterDirection::TTB => "auto",
};
Ok(html! {
(DOCTYPE)
html lang=(lang) dir=(dir) {
(head)
(body)
}
})
}
}

View file

@ -0,0 +1,88 @@
use crate::base::component::Html;
use crate::locale::L10n;
use crate::response::ResponseError;
use crate::service::http::{header::ContentType, StatusCode};
use crate::service::{HttpRequest, HttpResponse};
use super::Page;
use std::fmt;
#[derive(Debug)]
pub enum ErrorPage {
NotModified(HttpRequest),
BadRequest(HttpRequest),
AccessDenied(HttpRequest),
NotFound(HttpRequest),
PreconditionFailed(HttpRequest),
InternalError(HttpRequest),
Timeout(HttpRequest),
}
impl fmt::Display for ErrorPage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
// Error 304.
ErrorPage::NotModified(_) => write!(f, "Not Modified"),
// Error 400.
ErrorPage::BadRequest(_) => write!(f, "Bad Client Data"),
// Error 403.
ErrorPage::AccessDenied(request) => {
let mut error_page = Page::new(request.clone());
let error403 = error_page.theme().error403(&mut error_page);
if let Ok(page) = error_page
.with_title(L10n::n("Error FORBIDDEN"))
.with_layout("error")
.with_component(Html::with(error403))
.render()
{
write!(f, "{}", page.into_string())
} else {
write!(f, "Access Denied")
}
}
// Error 404.
ErrorPage::NotFound(request) => {
let mut error_page = Page::new(request.clone());
let error404 = error_page.theme().error404(&mut error_page);
if let Ok(page) = error_page
.with_title(L10n::n("Error RESOURCE NOT FOUND"))
.with_layout("error")
.with_component(Html::with(error404))
.render()
{
write!(f, "{}", page.into_string())
} else {
write!(f, "Not Found")
}
}
// Error 412.
ErrorPage::PreconditionFailed(_) => write!(f, "Precondition Failed"),
// Error 500.
ErrorPage::InternalError(_) => write!(f, "Internal Error"),
// Error 504.
ErrorPage::Timeout(_) => write!(f, "Timeout"),
}
}
}
impl ResponseError for ErrorPage {
fn error_response(&self) -> HttpResponse {
HttpResponse::build(self.status_code())
.insert_header(ContentType::html())
.body(self.to_string())
}
#[rustfmt::skip]
fn status_code(&self) -> StatusCode {
match self {
ErrorPage::NotModified(_) => StatusCode::NOT_MODIFIED,
ErrorPage::BadRequest(_) => StatusCode::BAD_REQUEST,
ErrorPage::AccessDenied(_) => StatusCode::FORBIDDEN,
ErrorPage::NotFound(_) => StatusCode::NOT_FOUND,
ErrorPage::PreconditionFailed(_) => StatusCode::PRECONDITION_FAILED,
ErrorPage::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
ErrorPage::Timeout(_) => StatusCode::GATEWAY_TIMEOUT,
}
}
}