🧑‍💻 Mejora las operaciones de cambio en contexto

This commit is contained in:
Manuel Cillero 2025-07-24 13:16:39 +02:00
parent ed25a17e80
commit 126fe3a8ea
2 changed files with 98 additions and 69 deletions

View file

@ -10,7 +10,7 @@ pub use assets::stylesheet::{StyleSheet, TargetMedia};
pub(crate) use assets::Assets; pub(crate) use assets::Assets;
mod context; mod context;
pub use context::{Context, ErrorParam}; pub use context::{Context, ContextOp, ErrorParam};
mod opt_id; mod opt_id;
pub use opt_id::OptionId; pub use opt_id::OptionId;

View file

@ -13,6 +13,37 @@ use std::str::FromStr;
use std::fmt; use std::fmt;
/// Operaciones para modificar el contexto ([`Context`]) del documento.
pub enum ContextOp {
/// Modifica el identificador de idioma del documento.
LangId(&'static LanguageIdentifier),
/// Establece 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.
Theme(&'static str),
/// Define el tipo de composición usado para renderizar el documento.
Layout(&'static str),
// Favicon.
/// Define el *favicon* del documento. Sobrescribe cualquier valor anterior.
SetFavicon(Option<Favicon>),
/// Define el *favicon* solo si no se ha establecido previamente.
SetFaviconIfNone(Favicon),
// Stylesheets.
/// Añade una hoja de estilos CSS al documento.
AddStyleSheet(StyleSheet),
/// Elimina una hoja de estilos por su ruta o identificador.
RemoveStyleSheet(&'static str),
// JavaScripts.
/// Añade un *script* JavaScript al documento.
AddJavaScript(JavaScript),
/// Elimina un *script* por su ruta o identificador.
RemoveJavaScript(&'static str),
}
/// Errores de lectura o conversión de parámetros almacenados en el contexto. /// Errores de lectura o conversión de parámetros almacenados en el contexto.
#[derive(Debug)] #[derive(Debug)]
pub enum ErrorParam { pub enum ErrorParam {
@ -33,48 +64,52 @@ impl fmt::Display for ErrorParam {
impl Error for ErrorParam {} impl Error for ErrorParam {}
/// Representa el contexto asociado a un documento HTML. /// Representa el contexto de un documento HTML.
/// ///
/// Esta estructura se crea internamente para recoger información relativa al documento asociado, /// Se crea internamente para manejar información relevante del documento, como la solicitud HTTP de
/// como la solicitud HTTP de origen, el idioma, el tema para renderizar ([`ThemeRef`]), y los /// origen, el idioma, tema y composición para el renderizado, los recursos *favicon* ([`Favicon`]),
/// recursos *favicon* ([`Favicon`]), las hojas de estilo ([`StyleSheet`]) y los *scripts* /// hojas de estilo ([`StyleSheet`]) y *scripts* ([`JavaScript`]), así como parámetros de contexto
/// ([`JavaScript`]). También admite parámetros de contexto definidos en tiempo de ejecución. /// definidos en tiempo de ejecución.
/// ///
/// # Ejemplo /// # Ejemplo
/// ///
/// ```rust /// ```rust
/// use pagetop::prelude::*; /// use pagetop::prelude::*;
/// ///
/// fn configure_context(mut ctx: Context) { /// fn configure_context(mut cx: Context) {
/// // Establece el idioma del documento a español. /// // Establece el idioma del documento a español.
/// ctx.set_langid(LangMatch::langid_or_default("es-ES")); /// cx.alter_assets(ContextOp::LangId(
/// /// LangMatch::langid_or_default("es-ES")
/// ))
/// // Selecciona un tema (por su nombre corto). /// // Selecciona un tema (por su nombre corto).
/// ctx.set_theme("aliner"); /// .alter_assets(ContextOp::Theme("aliner"))
///
/// // Asigna un favicon. /// // Asigna un favicon.
/// ctx.set_favicon(Some(Favicon::new().with_icon("/icons/favicon.ico"))); /// .alter_assets(ContextOp::SetFavicon(Some(
/// /// Favicon::new().with_icon("/icons/favicon.ico")
/// )))
/// // Añade una hoja de estilo externa. /// // Añade una hoja de estilo externa.
/// ctx.add_stylesheet(StyleSheet::from("/css/style.css")); /// .alter_assets(ContextOp::AddStyleSheet(
/// /// StyleSheet::from("/css/style.css")
/// ))
/// // Añade un script JavaScript. /// // Añade un script JavaScript.
/// ctx.add_javascript(JavaScript::defer("/js/main.js")); /// .alter_assets(ContextOp::AddJavaScript(
/// JavaScript::defer("/js/main.js")
/// ));
/// ///
/// // Añade un parámetro dinámico al contexto. /// // Añade un parámetro dinámico al contexto.
/// ctx.set_param("usuario_id", 42); /// cx.set_param("usuario_id", 42);
/// ///
/// // Recupera el parámetro y lo convierte a su tipo original. /// // Recupera el parámetro y lo convierte a su tipo original.
/// let id: i32 = ctx.get_param("usuario_id").unwrap(); /// let id: i32 = cx.get_param("usuario_id").unwrap();
/// assert_eq!(id, 42); /// assert_eq!(id, 42);
/// ///
/// // Recupera el tema seleccionado. /// // Recupera el tema seleccionado.
/// let active_theme = ctx.theme(); /// let active_theme = cx.theme();
/// assert_eq!(active_theme.short_name(), "aliner"); /// assert_eq!(active_theme.short_name(), "aliner");
/// ///
/// // Genera un identificador único para un componente de tipo `Menu`. /// // Genera un identificador único para un componente de tipo `Menu`.
/// struct Menu; /// struct Menu;
/// let unique_id = ctx.required_id::<Menu>(None); /// let unique_id = cx.required_id::<Menu>(None);
/// assert_eq!(unique_id, "menu-1"); // Si es el primero generado. /// assert_eq!(unique_id, "menu-1"); // Si es el primero generado.
/// } /// }
/// ``` /// ```
@ -83,6 +118,7 @@ pub struct Context {
request : HttpRequest, // Solicitud HTTP de origen. request : HttpRequest, // Solicitud HTTP de origen.
langid : &'static LanguageIdentifier, // Identificador del idioma. langid : &'static LanguageIdentifier, // Identificador del idioma.
theme : ThemeRef, // Referencia al tema para renderizar. theme : ThemeRef, // Referencia al tema para renderizar.
layout : &'static str, // Composición del documento para renderizar.
favicon : Option<Favicon>, // Favicon, si se ha definido. favicon : Option<Favicon>, // Favicon, si se ha definido.
stylesheets: Assets<StyleSheet>, // Hojas de estilo CSS. stylesheets: Assets<StyleSheet>, // Hojas de estilo CSS.
javascripts: Assets<JavaScript>, // Scripts JavaScript. javascripts: Assets<JavaScript>, // Scripts JavaScript.
@ -100,6 +136,7 @@ impl Context {
request, request,
langid : &DEFAULT_LANGID, langid : &DEFAULT_LANGID,
theme : *DEFAULT_THEME, theme : *DEFAULT_THEME,
layout : "default",
favicon : None, favicon : None,
stylesheets: Assets::<StyleSheet>::new(), stylesheets: Assets::<StyleSheet>::new(),
javascripts: Assets::<JavaScript>::new(), javascripts: Assets::<JavaScript>::new(),
@ -108,59 +145,45 @@ impl Context {
} }
} }
/// Modifica el identificador de idioma del documento. /// Modifica información o recursos del contexto usando [`ContextOp`].
pub fn set_langid(&mut self, langid: &'static LanguageIdentifier) -> &mut Self { pub fn alter_assets(&mut self, op: ContextOp) -> &mut Self {
self.langid = langid; match op {
self ContextOp::LangId(langid) => {
} self.langid = langid;
}
/// Establece el tema que se usará para renderizar el documento. ContextOp::Theme(theme_name) => {
/// self.theme = theme_by_short_name(theme_name).unwrap_or(*DEFAULT_THEME);
/// Localiza el tema por su [`short_name`](crate::core::AnyInfo::short_name), y si no aplica }
/// ninguno entonces usará el tema por defecto. ContextOp::Layout(layout) => {
pub fn set_theme(&mut self, short_name: impl AsRef<str>) -> &mut Self { self.layout = layout;
self.theme = theme_by_short_name(short_name).unwrap_or(*DEFAULT_THEME); }
self // Favicon.
} ContextOp::SetFavicon(favicon) => {
self.favicon = favicon;
/// Define el *favicon* del documento. Sobrescribe cualquier valor anterior. }
pub fn set_favicon(&mut self, favicon: Option<Favicon>) -> &mut Self { ContextOp::SetFaviconIfNone(icon) => {
self.favicon = favicon; if self.favicon.is_none() {
self self.favicon = Some(icon);
} }
}
/// Define el *favicon* solo si no se ha establecido previamente. // Stylesheets.
pub fn set_favicon_if_none(&mut self, favicon: Favicon) -> &mut Self { ContextOp::AddStyleSheet(css) => {
if self.favicon.is_none() { self.stylesheets.add(css);
self.favicon = Some(favicon); }
ContextOp::RemoveStyleSheet(path) => {
self.stylesheets.remove(path);
}
// JavaScripts.
ContextOp::AddJavaScript(js) => {
self.javascripts.add(js);
}
ContextOp::RemoveJavaScript(path) => {
self.javascripts.remove(path);
}
} }
self self
} }
/// Añade una hoja de estilos CSS al documento.
pub fn add_stylesheet(&mut self, css: StyleSheet) -> &mut Self {
self.stylesheets.add(css);
self
}
/// Elimina una hoja de estilos por su ruta o identificador.
pub fn remove_stylesheet(&mut self, name: impl AsRef<str>) -> &mut Self {
self.stylesheets.remove(name);
self
}
/// Añade un *script* JavaScript al documento.
pub fn add_javascript(&mut self, js: JavaScript) -> &mut Self {
self.javascripts.add(js);
self
}
/// Elimina un *script* por su ruta o identificador.
pub fn remove_javascript(&mut self, name: impl AsRef<str>) -> &mut Self {
self.javascripts.remove(name);
self
}
/// Añade o modifica un parámetro del contexto almacenando el valor como [`String`]. /// Añade o modifica un parámetro del contexto almacenando el valor como [`String`].
pub fn set_param<T: ToString>(&mut self, key: impl AsRef<str>, value: T) -> &mut Self { pub fn set_param<T: ToString>(&mut self, key: impl AsRef<str>, value: T) -> &mut Self {
self.params self.params
@ -185,6 +208,12 @@ impl Context {
self.theme self.theme
} }
/// Devuelve el tipo de composición usado para renderizar el documento. El valor predeterminado
/// es `"default"`.
pub fn layout(&self) -> &str {
self.layout
}
/// Recupera un parámetro del contexto convertido al tipo especificado. /// 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 /// Devuelve un error si el parámetro no existe ([`ErrorParam::NotFound`]) o la conversión falla