💥 Replace action! macro with actions!

This commit is contained in:
Manuel Cillero 2023-09-03 09:00:00 +02:00
parent 507015d5c9
commit c50cfa985f
8 changed files with 22 additions and 15 deletions

View file

@ -30,7 +30,7 @@ impl ModuleTrait for Admin {
} }
fn actions(&self) -> Vec<Action> { fn actions(&self) -> Vec<Action> {
vec![action!(ActionBeforePrepareBody => before_prepare_body)] actions![ActionBeforePrepareBody::with(before_prepare_body)]
} }
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) { fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {

View file

@ -29,7 +29,7 @@ impl ModuleTrait for JQuery {
} }
fn actions(&self) -> Vec<Action> { fn actions(&self) -> Vec<Action> {
vec![action!(ActionAfterPrepareBody => after_prepare_body)] actions![ActionAfterPrepareBody::with(after_prepare_body)]
} }
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) { fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {

View file

@ -30,7 +30,7 @@ impl ModuleTrait for Minimal {
} }
fn actions(&self) -> Vec<Action> { fn actions(&self) -> Vec<Action> {
vec![action!(ActionAfterPrepareBody => after_prepare_body, 99)] actions![ActionAfterPrepareBody::with(after_prepare_body).with_weight(99)]
} }
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) { fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {

View file

@ -27,7 +27,7 @@ impl ModuleTrait for Node {
} }
fn actions(&self) -> Vec<Action> { fn actions(&self) -> Vec<Action> {
vec![action!(ActionBeforePrepareBody => before_prepare_body, -1)] actions![ActionBeforePrepareBody::with(before_prepare_body).with_weight(-1)]
} }
fn migrations(&self) -> Vec<MigrationItem> { fn migrations(&self) -> Vec<MigrationItem> {

View file

@ -8,10 +8,3 @@ use list::ActionsList;
mod all; mod all;
pub(crate) use all::add_action; pub(crate) use all::add_action;
pub use all::run_actions; 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))?)
}};
}

View file

@ -31,3 +31,17 @@ impl ActionsList {
let _: Vec<_> = self.0.read().unwrap().iter().map(f).collect(); let _: Vec<_> = self.0.read().unwrap().iter().map(f).collect();
} }
} }
#[macro_export]
macro_rules! actions {
() => {
Vec::<Action>::new()
};
( $($action:expr),+ $(,)? ) => {{
let mut v = Vec::<Action>::new();
$(
v.push(Box::new($action));
)*
v
}};
}

View file

@ -1,7 +1,7 @@
use crate::core::action::Action; use crate::core::action::Action;
use crate::core::component::L10n; use crate::core::component::L10n;
use crate::core::theme::ThemeRef; use crate::core::theme::ThemeRef;
use crate::{service, util, Handle}; use crate::{actions, service, util, Handle};
#[cfg(feature = "database")] #[cfg(feature = "database")]
use crate::{db::MigrationItem, migrations}; use crate::{db::MigrationItem, migrations};
@ -37,7 +37,7 @@ pub trait ModuleTrait: ModuleBase + Send + Sync {
} }
fn actions(&self) -> Vec<Action> { fn actions(&self) -> Vec<Action> {
vec![] actions![]
} }
fn init(&self) {} fn init(&self) {}

View file

@ -18,8 +18,8 @@ pub use crate::default_settings;
pub use crate::static_locales; pub use crate::static_locales;
// crate::service // crate::service
pub use crate::{static_files, static_files_service}; pub use crate::{static_files, static_files_service};
// crate::core::action // crate::core::actions
pub use crate::action; pub use crate::actions;
// crate::core::component // crate::core::component
pub use crate::actions_for_component; pub use crate::actions_for_component;