add hashmap macro

This commit is contained in:
manuel 2022-05-22 22:30:07 +02:00
parent a22195d5d8
commit 6f8a802deb
2 changed files with 25 additions and 8 deletions

View File

@ -19,7 +19,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
use std::convert::From;
use async_trait::async_trait;
use actix_web::{web, HttpResponse, HttpRequest, Error};
use actix_admin::{ ActixAdminField, ActixAdminModelTrait, ActixAdminViewModelTrait, ActixAdminViewModel, ActixAdminModel, AppDataTrait };
use actix_admin::{ ActixAdminField, ActixAdminModelTrait, ActixAdminViewModelTrait, ActixAdminViewModel, ActixAdminModel, AppDataTrait , hashmap};
use sea_orm::ActiveValue::Set;
use sea_orm::{ConnectOptions, DatabaseConnection};
use sea_orm::{entity::*, query::*};
@ -35,6 +35,19 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
}
}
impl From<Model> for ActixAdminModel {
fn from(model: Model) -> Self {
ActixAdminModel {
// TODO: create dynamically
values: hashmap![
"title" => model.title,
"text" => model.text,
"id" => model.id.to_string()
]
}
}
}
#[async_trait(?Send)]
impl ActixAdminViewModelTrait for Entity {
async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> {
@ -68,14 +81,9 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
// TODO: must be dynamic
let mut model_entities = Vec::new();
for entity in entities {
let mut model_values = HashMap::new();
model_values.insert("title", entity.title);
model_values.insert("text", entity.text);
model_values.insert("id", entity.id.to_string());
model_entities.push(
ActixAdminModel {
values: model_values,
});
ActixAdminModel::from(entity)
);
}
model_entities

View File

@ -11,6 +11,15 @@ pub use actix_admin_macros::DeriveActixAdminModel;
const DEFAULT_ENTITIES_PER_PAGE: usize = 5;
#[macro_export]
macro_rules! hashmap {
($( $key: expr => $val: expr ),*) => {{
let mut map = ::std::collections::HashMap::new();
$( map.insert($key, $val); )*
map
}}
}
// globals
lazy_static! {
static ref TERA: Tera =