🎨 Mejora definición encapsulando uso de recursos
This commit is contained in:
parent
c30c4cdf66
commit
5d2f293942
3 changed files with 34 additions and 12 deletions
|
@ -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
|
||||
//! include_files!(HM_GUIDES => guides);
|
||||
//! include_files!(GUIDES => guides);
|
||||
//! ```
|
||||
|
||||
use grass::{from_path, Options, OutputStyle};
|
||||
|
|
25
src/lib.rs
25
src/lib.rs
|
@ -30,12 +30,33 @@
|
|||
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Deref;
|
||||
|
||||
// RE-EXPORTED *************************************************************************************
|
||||
|
||||
pub use pagetop_macros::{builder_fn, html, main, test, AutoDefault};
|
||||
|
||||
/// Representa un conjunto de recursos asociados a `$STATIC` en [`include_files!`].
|
||||
pub type StaticResources = std::collections::HashMap<&'static str, static_files::Resource>;
|
||||
/// Conjunto de recursos asociados a `$STATIC` en [`include_files!`](crate::include_files).
|
||||
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 *********************************************************************************************
|
||||
|
||||
|
|
|
@ -15,18 +15,18 @@ pub use actix_web::test;
|
|||
///
|
||||
/// # 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.
|
||||
///
|
||||
/// * `include_files!(BLOG_HM => blog)` - Asigna a la variable estática `BLOG_HM` un conjunto de
|
||||
/// recursos llamado `blog`.
|
||||
/// * `include_files!(BLOG => media)` - También se puede asignar el conjunto de recursos a una
|
||||
/// variable global; p.ej. `BLOG`.
|
||||
///
|
||||
/// # Argumentos
|
||||
///
|
||||
/// * `$bundle` – Nombre del conjunto de recursos generado por `build.rs` (consultar
|
||||
/// [`pagetop_build`](https://docs.rs/pagetop-build)).
|
||||
/// * `$STATIC` – Identificador para la variable estática de tipo
|
||||
/// [`StaticResources`](`crate::StaticResources`).
|
||||
/// * `$STATIC` – Asigna el conjunto de recursos a una variable global de tipo
|
||||
/// [`StaticResources`](crate::StaticResources).
|
||||
///
|
||||
/// # Ejemplos
|
||||
///
|
||||
|
@ -51,8 +51,9 @@ macro_rules! include_files {
|
|||
mod [<static_files_ $bundle>] {
|
||||
include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs"));
|
||||
}
|
||||
pub static $STATIC: std::sync::LazyLock<StaticResources> = std::sync::LazyLock::new(
|
||||
[<static_files_ $bundle>]::$bundle
|
||||
pub static $STATIC: std::sync::LazyLock<$crate::StaticResources> =
|
||||
std::sync::LazyLock::new(
|
||||
$crate::StaticResources::new([<static_files_ $bundle>]::$bundle)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue