diff --git a/actix_admin_macros/src/lib.rs b/actix_admin_macros/src/lib.rs index 0d7498d..8b5139b 100644 --- a/actix_admin_macros/src/lib.rs +++ b/actix_admin_macros/src/lib.rs @@ -37,6 +37,7 @@ pub fn derive_actix_admin(_input: proc_macro::TokenStream) -> proc_macro::TokenS use sea_orm::EntityTrait; use itertools::izip; use actix_session::{Session}; + use lazy_static::lazy_static; }; proc_macro::TokenStream::from(expanded) } @@ -155,6 +156,55 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T let fields_textarea = get_actix_admin_fields_textarea(&fields); let expanded = quote! { + lazy_static! { + pub static ref ACTIX_ADMIN_VIEWMODEL_FIELDS: Vec = { + let mut vec = Vec::new(); + + let field_names = stringify!( + #(#field_names),* + ).split(",") + .collect::>(); + + let html_input_types = stringify!( + #(#field_html_input_type),* + ).split(",") + .collect::>(); + + let field_select_lists = stringify!( + #(#field_select_list),* + ).split(",") + .collect::>(); + + let is_option_lists = [ + #(#is_option_list),* + ]; + + let fields_type_paths = [ + #(#fields_type_path),* + ]; + + let fields_textareas = [ + #(#fields_textarea),* + ]; + + for (field_name, html_input_type, select_list, is_option_list, fields_type_path, is_textarea) in izip!(&field_names, &html_input_types, &field_select_lists, is_option_lists, fields_type_paths, fields_textareas) { + + let select_list = select_list.replace('"', "").replace(' ', "").to_string(); + let field_name = field_name.replace('"', "").replace(' ', "").to_string(); + let html_input_type = html_input_type.replace('"', "").replace(' ', "").to_string(); + + vec.push(ActixAdminViewModelField { + field_name: field_name, + html_input_type: html_input_type, + select_list: select_list.clone(), + is_option: is_option_list, + field_type: ActixAdminViewModelFieldType::get_field_type(fields_type_path, select_list, is_textarea) + }); + } + vec + }; + } + impl From for ActixAdminModel { fn from(model: Model) -> Self { ActixAdminModel { @@ -211,51 +261,8 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T model.errors = errors; } - fn get_fields() -> Vec { - let mut vec = Vec::new(); - - let field_names = stringify!( - #(#field_names),* - ).split(",") - .collect::>(); - - let html_input_types = stringify!( - #(#field_html_input_type),* - ).split(",") - .collect::>(); - - let field_select_lists = stringify!( - #(#field_select_list),* - ).split(",") - .collect::>(); - - let is_option_lists = [ - #(#is_option_list),* - ]; - - let fields_type_paths = [ - #(#fields_type_path),* - ]; - - let fields_textareas = [ - #(#fields_textarea),* - ]; - - for (field_name, html_input_type, select_list, is_option_list, fields_type_path, is_textarea) in izip!(&field_names, &html_input_types, &field_select_lists, is_option_lists, fields_type_paths, fields_textareas) { - - let select_list = select_list.replace('"', "").replace(' ', "").to_string(); - let field_name = field_name.replace('"', "").replace(' ', "").to_string(); - let html_input_type = html_input_type.replace('"', "").replace(' ', "").to_string(); - - vec.push(ActixAdminViewModelField { - field_name: field_name, - html_input_type: html_input_type, - select_list: select_list.clone(), - is_option: is_option_list, - field_type: ActixAdminViewModelFieldType::get_field_type(fields_type_path, select_list, is_textarea) - }); - } - vec + fn get_fields() -> &'static[ActixAdminViewModelField] { + ACTIX_ADMIN_VIEWMODEL_FIELDS.as_slice() } } }; diff --git a/example/Cargo.toml b/example/Cargo.toml index f939d1a..acdf239 100644 --- a/example/Cargo.toml +++ b/example/Cargo.toml @@ -15,6 +15,6 @@ dotenv = "0.15" serde = "1.0.136" serde_derive = "1.0.136" sea-orm = { version = "^0.9.1", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = true } - +lazy_static = "1.4.0" actix-admin = { path = "../" } azure_auth = { path = "./azure_auth" } \ No newline at end of file diff --git a/src/model.rs b/src/model.rs index 1138357..274826e 100644 --- a/src/model.rs +++ b/src/model.rs @@ -15,7 +15,7 @@ pub trait ActixAdminModelTrait { posts_per_page: usize, search: &String ) -> Result<(usize, Vec), ActixAdminError>; - fn get_fields() -> Vec; + fn get_fields() -> &'static[ActixAdminViewModelField]; fn validate_model(model: &mut ActixAdminModel); } diff --git a/src/view_model.rs b/src/view_model.rs index c67da7c..4c17962 100644 --- a/src/view_model.rs +++ b/src/view_model.rs @@ -35,7 +35,7 @@ pub trait ActixAdminViewModelTrait { pub struct ActixAdminViewModel { pub entity_name: String, pub primary_key: String, - pub fields: Vec, + pub fields: &'static[ActixAdminViewModelField], pub show_search: bool, pub user_can_access: Option bool> } @@ -44,7 +44,7 @@ pub struct ActixAdminViewModel { pub struct ActixAdminViewModelSerializable { pub entity_name: String, pub primary_key: String, - pub fields: Vec, + pub fields: &'static [ActixAdminViewModelField], pub show_search: bool }