2022-07-21 19:01:05 +02:00
|
|
|
use syn::{
|
|
|
|
DeriveInput, Ident
|
|
|
|
};
|
|
|
|
use quote::quote;
|
|
|
|
use crate::model_fields::{ ModelField };
|
|
|
|
use proc_macro2::{Span};
|
|
|
|
|
2022-08-20 21:44:24 +02:00
|
|
|
pub fn get_select_list_from_model(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
|
|
//let ast: DeriveInput = syn::parse(input).unwrap();
|
|
|
|
//let (_vis, _ty, _generics) = (&ast.vis, &ast.ident, &ast.generics);
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#[async_trait]
|
|
|
|
impl ActixAdminSelectListTrait for Entity {
|
2022-10-07 21:32:59 +02:00
|
|
|
async fn get_key_value(db: &DatabaseConnection) -> Result<Vec<(String, String)>, ActixAdminError> {
|
|
|
|
let entities = Entity::find().order_by_asc(Column::Id).all(db).await?;
|
2022-08-20 21:44:24 +02:00
|
|
|
let mut key_value = Vec::new();
|
|
|
|
|
2022-10-07 21:32:59 +02:00
|
|
|
for entity in entities {
|
2022-08-20 21:44:24 +02:00
|
|
|
key_value.push((entity.id.to_string(), entity.to_string()));
|
|
|
|
};
|
|
|
|
key_value.sort_by(|a, b| a.1.cmp(&b.1));
|
2022-10-07 21:32:59 +02:00
|
|
|
Ok(key_value)
|
2022-08-20 21:44:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
proc_macro::TokenStream::from(expanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_select_list_from_enum(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
2022-07-21 19:01:05 +02:00
|
|
|
let ast: DeriveInput = syn::parse(input).unwrap();
|
|
|
|
let (_vis, ty, _generics) = (&ast.vis, &ast.ident, &ast.generics);
|
|
|
|
|
|
|
|
let expanded = quote! {
|
2022-08-20 21:44:24 +02:00
|
|
|
#[async_trait]
|
2022-07-21 19:01:05 +02:00
|
|
|
impl ActixAdminSelectListTrait for #ty {
|
2022-10-07 21:32:59 +02:00
|
|
|
async fn get_key_value(db: &DatabaseConnection) -> Result<Vec<(String, String)>, ActixAdminError> {
|
2022-07-21 19:01:05 +02:00
|
|
|
let mut fields = Vec::new();
|
|
|
|
for field in #ty::iter() {
|
|
|
|
fields.push((field.to_string(), field.to_string()));
|
|
|
|
}
|
2022-10-07 21:32:59 +02:00
|
|
|
Ok(fields)
|
2022-07-21 19:01:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-08-20 21:44:24 +02:00
|
|
|
|
2022-07-21 19:01:05 +02:00
|
|
|
proc_macro::TokenStream::from(expanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_select_lists(fields: &Vec<ModelField>) -> Vec<proc_macro2::TokenStream> {
|
|
|
|
fields
|
|
|
|
.iter()
|
|
|
|
.filter(|model_field| model_field.select_list != "")
|
|
|
|
.map(|model_field| {
|
|
|
|
let ident_name = model_field.ident.to_string();
|
|
|
|
let select_list_ident = Ident::new(&(model_field.select_list), Span::call_site());
|
|
|
|
quote! {
|
2022-10-07 21:32:59 +02:00
|
|
|
#ident_name => #select_list_ident::get_key_value(db).await?
|
2022-07-21 19:01:05 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}
|