♻️ Relocate macro codes based on function
This commit is contained in:
parent
f2031307d7
commit
c9f71685b4
16 changed files with 163 additions and 137 deletions
|
|
@ -16,7 +16,7 @@ impl ModuleTrait for Aliner {
|
|||
}
|
||||
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
serve_static_files!(scfg, "/aliner", aliner);
|
||||
static_files_service!(scfg, "/aliner", aliner);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ impl ModuleTrait for Bootsier {
|
|||
}
|
||||
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
serve_static_files!(scfg, "/bootsier", bootsier);
|
||||
static_files_service!(scfg, "/bootsier", bootsier);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ impl ModuleTrait for Bulmix {
|
|||
}
|
||||
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
serve_static_files!(scfg, "/bulmix", bulmix);
|
||||
static_files_service!(scfg, "/bulmix", bulmix);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ impl ModuleTrait for HomeDemo {
|
|||
}
|
||||
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
serve_static_files!(scfg, "/homedemo", homedemo);
|
||||
static_files_service!(scfg, "/homedemo", homedemo);
|
||||
scfg.route("/", service::web::get().to(demo));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ impl ModuleTrait for JQuery {
|
|||
}
|
||||
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
serve_static_files!(scfg, "/jquery", jquery);
|
||||
static_files_service!(scfg, "/jquery", jquery);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,6 @@ impl ModuleTrait for MegaMenu {
|
|||
}
|
||||
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
serve_static_files!(scfg, "/megamenu", megamenu);
|
||||
static_files_service!(scfg, "/megamenu", megamenu);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ impl ModuleTrait for Minimal {
|
|||
}
|
||||
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
serve_static_files!(scfg, "/minimal", minimal);
|
||||
static_files_service!(scfg, "/minimal", minimal);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -119,11 +119,9 @@ mod path;
|
|||
mod source;
|
||||
mod value;
|
||||
|
||||
use crate::default_settings;
|
||||
use crate::LazyStatic;
|
||||
|
||||
use crate::config::data::ConfigData;
|
||||
use crate::config::file::File;
|
||||
use crate::LazyStatic;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
|
|
@ -159,6 +157,31 @@ pub static CONFIG: LazyStatic<ConfigData> = LazyStatic::new(|| {
|
|||
settings
|
||||
});
|
||||
|
||||
#[macro_export]
|
||||
/// Define un conjunto de ajustes de configuración usando tipos seguros y valores predefinidos.
|
||||
///
|
||||
/// Detiene la aplicación con un panic! si no pueden asignarse los ajustes de configuración.
|
||||
///
|
||||
/// Ver [`Cómo añadir ajustes de configuración`](config/index.html#cómo-añadir-ajustes-de-configuración).
|
||||
macro_rules! default_settings {
|
||||
( $($key:literal => $value:literal),* $(,)? ) => {
|
||||
#[doc = concat!(
|
||||
"Assigned or predefined values for configuration settings associated to the ",
|
||||
"[`Settings`] type."
|
||||
)]
|
||||
pub static SETTINGS: $crate::LazyStatic<Settings> = $crate::LazyStatic::new(|| {
|
||||
let mut settings = $crate::config::CONFIG.clone();
|
||||
$(
|
||||
settings.set_default($key, $value).unwrap();
|
||||
)*
|
||||
match settings.try_into() {
|
||||
Ok(s) => s,
|
||||
Err(e) => panic!("Error parsing settings: {}", e),
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
/// Configuration settings for the [`[app]`](App), [`[database]`](Database), [`[dev]`](Dev),
|
||||
/// [`[log]`](Log), and [`[server]`](Server) sections (see [`SETTINGS`]).
|
||||
|
|
|
|||
|
|
@ -8,3 +8,10 @@ use list::ActionsList;
|
|||
mod all;
|
||||
pub(crate) use all::add_action;
|
||||
pub use all::run_actions;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! action {
|
||||
( $action:ty => $f:ident $(, $weight:expr)? ) => {{
|
||||
Box::new(<$action>::new().with_action($f)$(.with_weight($weight))?)
|
||||
}};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,13 +4,6 @@ use std::sync::{Arc, RwLock};
|
|||
|
||||
pub type Action = Box<dyn ActionTrait>;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! action {
|
||||
( $action:ty => $f:ident $(, $weight:expr)? ) => {{
|
||||
Box::new(<$action>::new().with_action($f)$(.with_weight($weight))?)
|
||||
}};
|
||||
}
|
||||
|
||||
pub struct ActionsList(Arc<RwLock<Vec<Action>>>);
|
||||
|
||||
impl ActionsList {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use crate::core::theme::{ThemeRef, ThemeTrait};
|
|||
use crate::html::{Favicon, StyleSheet};
|
||||
use crate::response::page::Page;
|
||||
use crate::service;
|
||||
use crate::{new_handle, serve_static_files, static_files, Handle};
|
||||
use crate::{new_handle, static_files, static_files_service, Handle};
|
||||
|
||||
new_handle!(THEME_DEFAULT);
|
||||
|
||||
|
|
@ -26,7 +26,7 @@ impl ModuleTrait for DefaultTheme {
|
|||
}
|
||||
|
||||
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
|
||||
serve_static_files!(scfg, "/theme", theme);
|
||||
static_files_service!(scfg, "/theme", theme);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -129,6 +129,9 @@ static_locales!(LOCALES_PAGETOP);
|
|||
// PUBLIC API.
|
||||
// *************************************************************************************************
|
||||
|
||||
// Functions and macro helpers.
|
||||
pub mod util;
|
||||
|
||||
// Gestión de la configuración.
|
||||
pub mod config;
|
||||
// Registro de trazas y eventos de la aplicación.
|
||||
|
|
@ -145,18 +148,15 @@ pub mod datetime;
|
|||
#[cfg(feature = "database")]
|
||||
pub mod db;
|
||||
|
||||
// APIs esenciales para crear acciones, componentes, módulos y temas.
|
||||
pub mod core;
|
||||
|
||||
// API para operar con los servicios web.
|
||||
pub mod service;
|
||||
|
||||
// APIs esenciales para crear acciones, componentes, módulos y temas.
|
||||
pub mod core;
|
||||
|
||||
// Tipos de respuestas a peticiones web.
|
||||
pub mod response;
|
||||
|
||||
// Funciones y macros útiles.
|
||||
pub mod util;
|
||||
|
||||
// Prepara y ejecuta la aplicación.
|
||||
pub mod app;
|
||||
|
||||
|
|
|
|||
|
|
@ -132,3 +132,53 @@ pub fn langid_for(language: &str) -> &LanguageIdentifier {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
/// Define un conjunto de elementos de localización y textos locales de traducción.
|
||||
macro_rules! static_locales {
|
||||
( $LOCALES:ident $(, $core_locales:literal)? ) => {
|
||||
$crate::locale::fluent_templates::static_loader! {
|
||||
static $LOCALES = {
|
||||
locales: "src/locale",
|
||||
$( core_locales: $core_locales, )?
|
||||
fallback_language: "en-US",
|
||||
|
||||
// Elimina las marcas Unicode que delimitan los argumentos.
|
||||
customise: |bundle| bundle.set_use_isolating(false),
|
||||
};
|
||||
}
|
||||
};
|
||||
( $LOCALES:ident in $dir_locales:literal $(, $core_locales:literal)? ) => {
|
||||
$crate::locale::fluent_templates::static_loader! {
|
||||
static $LOCALES = {
|
||||
locales: $dir_locales,
|
||||
$( core_locales: $core_locales, )?
|
||||
fallback_language: "en-US",
|
||||
|
||||
// Elimina las marcas Unicode que delimitan los argumentos.
|
||||
customise: |bundle| bundle.set_use_isolating(false),
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! t {
|
||||
( $langid:expr, $locales:expr, $key:expr ) => {
|
||||
$locales.lookup($langid, $key).unwrap_or($key.to_string())
|
||||
};
|
||||
( $langid:expr, $locales:expr, $key:expr, $args:expr ) => {
|
||||
$locales
|
||||
.lookup_with_args(
|
||||
$langid,
|
||||
$key,
|
||||
&$args
|
||||
.iter()
|
||||
.fold(std::collections::HashMap::new(), |mut a, (k, v)| {
|
||||
a.insert(k.to_string(), v.to_owned().into());
|
||||
a
|
||||
}),
|
||||
)
|
||||
.unwrap_or($key.to_string())
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,22 @@ pub use crate::{concat_string, fn_builder, main, paste, test};
|
|||
// Global.
|
||||
pub use crate::{Handle, HashMapResources, LazyStatic, ResultExt, Weight};
|
||||
|
||||
// Funciones y macros útiles.
|
||||
// Functions and macro helpers.
|
||||
pub use crate::util;
|
||||
pub use crate::{action, actions_for_component};
|
||||
pub use crate::{default_settings, kv, new_handle};
|
||||
pub use crate::{serve_static_files, static_files, static_locales};
|
||||
pub use crate::{kv, new_handle};
|
||||
|
||||
// MACROS.
|
||||
|
||||
// crate::config
|
||||
pub use crate::default_settings;
|
||||
// crate::locale
|
||||
pub use crate::{static_locales, t};
|
||||
// crate::service
|
||||
pub use crate::{static_files, static_files_service};
|
||||
// crate::core::action
|
||||
pub use crate::action;
|
||||
// crate::core::component
|
||||
pub use crate::actions_for_component;
|
||||
|
||||
// API.
|
||||
|
||||
|
|
@ -27,6 +38,9 @@ pub use crate::datetime::*;
|
|||
#[cfg(feature = "database")]
|
||||
pub use crate::{db, db::*, migration_item, pub_migration};
|
||||
|
||||
pub use crate::service;
|
||||
pub use crate::service::HttpMessage;
|
||||
|
||||
pub use crate::core::action::*;
|
||||
pub use crate::core::component::html::*;
|
||||
pub use crate::core::component::l10n::*;
|
||||
|
|
@ -34,9 +48,6 @@ pub use crate::core::component::*;
|
|||
pub use crate::core::module::*;
|
||||
pub use crate::core::theme::*;
|
||||
|
||||
pub use crate::service;
|
||||
pub use crate::service::HttpMessage;
|
||||
|
||||
pub use crate::response::fatal_error::*;
|
||||
pub use crate::response::{page::*, redirect::*, ResponseError};
|
||||
|
||||
|
|
|
|||
|
|
@ -7,3 +7,47 @@ pub use actix_web::{
|
|||
|
||||
pub use actix_web_files::Files as ActixFiles;
|
||||
pub use actix_web_static_files::ResourceFiles;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! static_files {
|
||||
( $bundle:ident ) => {
|
||||
$crate::paste! {
|
||||
mod [<static_files_ $bundle>] {
|
||||
include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs"));
|
||||
}
|
||||
}
|
||||
};
|
||||
( $bundle:ident => $STATIC:ident ) => {
|
||||
$crate::paste! {
|
||||
mod [<static_files_ $bundle>] {
|
||||
include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs"));
|
||||
}
|
||||
static $STATIC: LazyStatic<HashMapResources> = LazyStatic::new([
|
||||
<static_files_ $bundle>]::$bundle
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! static_files_service {
|
||||
( $scfg:ident, $path:expr, $bundle:ident ) => {{
|
||||
$crate::paste! {
|
||||
let static_files = &$crate::config::SETTINGS.dev.static_files;
|
||||
if static_files.is_empty() {
|
||||
$scfg.service($crate::service::ResourceFiles::new(
|
||||
$path,
|
||||
[<static_files_ $bundle>]::$bundle(),
|
||||
));
|
||||
} else {
|
||||
$scfg.service(
|
||||
$crate::service::ActixFiles::new(
|
||||
$path,
|
||||
$crate::concat_string!(static_files, $path),
|
||||
)
|
||||
.show_files_listing(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
//! Funciones útiles.
|
||||
//! Functions and macro helpers.
|
||||
|
||||
use crate::Handle;
|
||||
|
||||
// *************************************************************************************************
|
||||
// FUNCIONES ÚTILES.
|
||||
// FUNCTIONS HELPERS.
|
||||
// *************************************************************************************************
|
||||
|
||||
// https://stackoverflow.com/a/71464396
|
||||
|
|
@ -57,7 +57,7 @@ pub fn single_type_name<T: ?Sized>() -> &'static str {
|
|||
}
|
||||
|
||||
// *************************************************************************************************
|
||||
// MACROS DECLARATIVAS.
|
||||
// MACRO HELPERS.
|
||||
// *************************************************************************************************
|
||||
|
||||
#[macro_export]
|
||||
|
|
@ -80,31 +80,6 @@ macro_rules! kv {
|
|||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
/// Define un conjunto de ajustes de configuración usando tipos seguros y valores predefinidos.
|
||||
///
|
||||
/// Detiene la aplicación con un panic! si no pueden asignarse los ajustes de configuración.
|
||||
///
|
||||
/// Ver [`Cómo añadir ajustes de configuración`](config/index.html#cómo-añadir-ajustes-de-configuración).
|
||||
macro_rules! default_settings {
|
||||
( $($key:literal => $value:literal),* $(,)? ) => {
|
||||
#[doc = concat!(
|
||||
"Assigned or predefined values for configuration settings associated to the ",
|
||||
"[`Settings`] type."
|
||||
)]
|
||||
pub static SETTINGS: $crate::LazyStatic<Settings> = $crate::LazyStatic::new(|| {
|
||||
let mut settings = $crate::config::CONFIG.clone();
|
||||
$(
|
||||
settings.set_default($key, $value).unwrap();
|
||||
)*
|
||||
match settings.try_into() {
|
||||
Ok(s) => s,
|
||||
Err(e) => panic!("Error parsing settings: {}", e),
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! new_handle {
|
||||
( $HANDLE:ident ) => {
|
||||
|
|
@ -118,80 +93,3 @@ macro_rules! new_handle {
|
|||
$crate::util::handle(module_path!(), file!(), line!(), column!());
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
/// Define un conjunto de elementos de localización y funciones locales de traducción.
|
||||
macro_rules! static_locales {
|
||||
( $LOCALES:ident $(, $core_locales:literal)? ) => {
|
||||
use $crate::locale::*;
|
||||
|
||||
fluent_templates::static_loader! {
|
||||
static $LOCALES = {
|
||||
locales: "src/locale",
|
||||
$( core_locales: $core_locales, )?
|
||||
fallback_language: "en-US",
|
||||
|
||||
// Elimina las marcas Unicode que delimitan los argumentos.
|
||||
customise: |bundle| bundle.set_use_isolating(false),
|
||||
};
|
||||
}
|
||||
};
|
||||
( $LOCALES:ident in $dir_locales:literal $(, $core_locales:literal)? ) => {
|
||||
use $crate::locale::*;
|
||||
|
||||
fluent_templates::static_loader! {
|
||||
static $LOCALES = {
|
||||
locales: $dir_locales,
|
||||
$( core_locales: $core_locales, )?
|
||||
fallback_language: "en-US",
|
||||
|
||||
// Elimina las marcas Unicode que delimitan los argumentos.
|
||||
customise: |bundle| bundle.set_use_isolating(false),
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! static_files {
|
||||
( $bundle:ident ) => {
|
||||
$crate::paste! {
|
||||
mod [<static_files_ $bundle>] {
|
||||
include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs"));
|
||||
}
|
||||
}
|
||||
};
|
||||
( $bundle:ident => $STATIC:ident ) => {
|
||||
$crate::paste! {
|
||||
mod [<static_files_ $bundle>] {
|
||||
include!(concat!(env!("OUT_DIR"), "/", stringify!($bundle), ".rs"));
|
||||
}
|
||||
static $STATIC: LazyStatic<HashMapResources> = LazyStatic::new([
|
||||
<static_files_ $bundle>]::$bundle
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! serve_static_files {
|
||||
( $scfg:ident, $path:expr, $bundle:ident ) => {{
|
||||
$crate::paste! {
|
||||
let static_files = &$crate::config::SETTINGS.dev.static_files;
|
||||
if static_files.is_empty() {
|
||||
$scfg.service($crate::service::ResourceFiles::new(
|
||||
$path,
|
||||
[<static_files_ $bundle>]::$bundle(),
|
||||
));
|
||||
} else {
|
||||
$scfg.service(
|
||||
$crate::service::ActixFiles::new(
|
||||
$path,
|
||||
$crate::concat_string!(static_files, $path),
|
||||
)
|
||||
.show_files_listing(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue