🎨 Mejora definición encapsulando uso de recursos

This commit is contained in:
Manuel Cillero 2025-07-13 11:10:06 +02:00
parent c30c4cdf66
commit 5d2f293942
3 changed files with 34 additions and 12 deletions

View file

@ -109,10 +109,10 @@
//! } //! }
//! ``` //! ```
//! //!
//! También se puede acceder al conjunto de recursos declarando un `HashMap` estático global: //! También se puede asignar el conjunto de recursos a una variable global; p.ej. `GUIDES`:
//! //!
//! ```rust,ignore //! ```rust,ignore
//! include_files!(HM_GUIDES => guides); //! include_files!(GUIDES => guides);
//! ``` //! ```
use grass::{from_path, Options, OutputStyle}; use grass::{from_path, Options, OutputStyle};

View file

@ -30,12 +30,33 @@
#![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, feature(doc_cfg))]
use std::collections::HashMap;
use std::ops::Deref;
// RE-EXPORTED ************************************************************************************* // RE-EXPORTED *************************************************************************************
pub use pagetop_macros::{builder_fn, html, main, test, AutoDefault}; pub use pagetop_macros::{builder_fn, html, main, test, AutoDefault};
/// Representa un conjunto de recursos asociados a `$STATIC` en [`include_files!`]. /// Conjunto de recursos asociados a `$STATIC` en [`include_files!`](crate::include_files).
pub type StaticResources = std::collections::HashMap<&'static str, static_files::Resource>; pub struct StaticResources {
bundle: HashMap<&'static str, static_files::Resource>,
}
impl StaticResources {
/// Crea un contenedor para un conjunto de recursos generado por `build.rs` (consultar
/// [`pagetop_build`](https://docs.rs/pagetop-build)).
pub fn new(bundle: HashMap<&'static str, static_files::Resource>) -> Self {
Self { bundle }
}
}
impl Deref for StaticResources {
type Target = HashMap<&'static str, static_files::Resource>;
fn deref(&self) -> &Self::Target {
&self.bundle
}
}
// API ********************************************************************************************* // API *********************************************************************************************

View file

@ -15,18 +15,18 @@ pub use actix_web::test;
/// ///
/// # Formas de uso /// # Formas de uso
/// ///
/// * `include_files!(media)` - Incluye el conjunto de recursos llamado `media`. Normalmente se /// * `include_files!(media)` - Para incluir un conjunto de recursos llamado `media`. Normalmente se
/// usará esta forma. /// usará esta forma.
/// ///
/// * `include_files!(BLOG_HM => blog)` - Asigna a la variable estática `BLOG_HM` un conjunto de /// * `include_files!(BLOG => media)` - También se puede asignar el conjunto de recursos a una
/// recursos llamado `blog`. /// variable global; p.ej. `BLOG`.
/// ///
/// # Argumentos /// # Argumentos
/// ///
/// * `$bundle` Nombre del conjunto de recursos generado por `build.rs` (consultar /// * `$bundle` Nombre del conjunto de recursos generado por `build.rs` (consultar
/// [`pagetop_build`](https://docs.rs/pagetop-build)). /// [`pagetop_build`](https://docs.rs/pagetop-build)).
/// * `$STATIC` Identificador para la variable estática de tipo /// * `$STATIC` Asigna el conjunto de recursos a una variable global de tipo
/// [`StaticResources`](`crate::StaticResources`). /// [`StaticResources`](crate::StaticResources).
/// ///
/// # Ejemplos /// # Ejemplos
/// ///
@ -51,8 +51,9 @@ macro_rules! include_files {
mod [<static_files_ $bundle>] { mod [<static_files_ $bundle>] {
include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs")); include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs"));
} }
pub static $STATIC: std::sync::LazyLock<StaticResources> = std::sync::LazyLock::new( pub static $STATIC: std::sync::LazyLock<$crate::StaticResources> =
[<static_files_ $bundle>]::$bundle std::sync::LazyLock::new(
$crate::StaticResources::new([<static_files_ $bundle>]::$bundle)
); );
} }
}; };