reinhardt-macros 0.1.2

Procedural macros for Reinhardt framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Admin macro implementation
//!
//! This module provides the `#[admin(model, ...)]` attribute macro for
//! automatically implementing the `ModelAdmin` trait.

use proc_macro2::TokenStream;
use quote::quote;
use syn::{
	Ident, ItemStruct, LitBool, LitInt, LitStr, Result, Token, Type, bracketed, parenthesized,
	parse::{Parse, ParseStream},
	punctuated::Punctuated,
};

/// Custom keywords for admin macro
mod kw {
	syn::custom_keyword!(model);
	syn::custom_keyword!(asc);
	syn::custom_keyword!(desc);
	syn::custom_keyword!(allow_all);
}

/// Order direction for sorting
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Order {
	Asc,
	Desc,
}

impl Parse for Order {
	fn parse(input: ParseStream) -> Result<Self> {
		let lookahead = input.lookahead1();
		if lookahead.peek(kw::asc) {
			input.parse::<kw::asc>()?;
			Ok(Order::Asc)
		} else if lookahead.peek(kw::desc) {
			input.parse::<kw::desc>()?;
			Ok(Order::Desc)
		} else {
			Err(lookahead.error())
		}
	}
}

/// Ordering specification: (field_name, order)
#[derive(Debug, Clone)]
pub(crate) struct OrderingSpec {
	pub field: Ident,
	pub order: Order,
}

impl Parse for OrderingSpec {
	fn parse(input: ParseStream) -> Result<Self> {
		let content;
		parenthesized!(content in input);
		let field: Ident = content.parse()?;
		content.parse::<Token![,]>()?;
		let order: Order = content.parse()?;
		Ok(OrderingSpec { field, order })
	}
}

/// Parsed configuration from `#[admin(model, ...)]`
#[derive(Debug)]
pub(crate) struct AdminModelConfig {
	/// The model type (for = ModelType)
	pub model_type: Type,
	/// The model name (name = "ModelName")
	pub name: String,
	/// Fields to display in list view
	pub list_display: Option<Vec<Ident>>,
	/// Fields that can be used for filtering
	pub list_filter: Option<Vec<Ident>>,
	/// Fields that can be searched
	pub search_fields: Option<Vec<Ident>>,
	/// Fields to display in forms
	pub fields: Option<Vec<Ident>>,
	/// Read-only fields
	pub readonly_fields: Option<Vec<Ident>>,
	/// Ordering specification
	pub ordering: Option<Vec<OrderingSpec>>,
	/// Number of items per page
	pub list_per_page: Option<usize>,
	/// Individual permission flags
	pub allow_view: Option<bool>,
	pub allow_add: Option<bool>,
	pub allow_change: Option<bool>,
	pub allow_delete: Option<bool>,
	/// Permission preset (e.g., "allow_all")
	pub permissions: Option<String>,
}

impl Parse for AdminModelConfig {
	fn parse(input: ParseStream) -> Result<Self> {
		let span = input.span();

		// Parse 'model' keyword first
		if !input.peek(kw::model) {
			return Err(syn::Error::new(
				span,
				"expected `model` keyword in #[admin(...)]\n\n  = help: use `#[admin(model, for = ModelType, name = \"ModelName\", ...)]`",
			));
		}
		input.parse::<kw::model>()?;

		// Comma after 'model'
		if input.peek(Token![,]) {
			input.parse::<Token![,]>()?;
		}

		let mut model_type: Option<Type> = None;
		let mut name: Option<String> = None;
		let mut list_display: Option<Vec<Ident>> = None;
		let mut list_filter: Option<Vec<Ident>> = None;
		let mut search_fields: Option<Vec<Ident>> = None;
		let mut fields: Option<Vec<Ident>> = None;
		let mut readonly_fields: Option<Vec<Ident>> = None;
		let mut ordering: Option<Vec<OrderingSpec>> = None;
		let mut list_per_page: Option<usize> = None;
		let mut allow_view: Option<bool> = None;
		let mut allow_add: Option<bool> = None;
		let mut allow_change: Option<bool> = None;
		let mut allow_delete: Option<bool> = None;
		let mut permissions: Option<String> = None;

		while !input.is_empty() {
			// Handle 'for' keyword specially since it's a reserved keyword
			if input.peek(Token![for]) {
				input.parse::<Token![for]>()?;
				input.parse::<Token![=]>()?;
				model_type = Some(input.parse()?);

				// Optional trailing comma
				if input.peek(Token![,]) {
					input.parse::<Token![,]>()?;
				}
				continue;
			}

			let key: Ident = input.parse()?;
			input.parse::<Token![=]>()?;

			match key.to_string().as_str() {
				"name" => {
					let lit: LitStr = input.parse()?;
					name = Some(lit.value());
				}
				"list_display" => {
					list_display = Some(parse_ident_array(input)?);
				}
				"list_filter" => {
					list_filter = Some(parse_ident_array(input)?);
				}
				"search_fields" => {
					search_fields = Some(parse_ident_array(input)?);
				}
				"fields" => {
					fields = Some(parse_ident_array(input)?);
				}
				"readonly_fields" => {
					readonly_fields = Some(parse_ident_array(input)?);
				}
				"ordering" => {
					ordering = Some(parse_ordering_array(input)?);
				}
				"list_per_page" => {
					let lit: LitInt = input.parse()?;
					list_per_page = Some(lit.base10_parse()?);
				}
				"allow_view" => {
					let lit: LitBool = input.parse()?;
					allow_view = Some(lit.value());
				}
				"allow_add" => {
					let lit: LitBool = input.parse()?;
					allow_add = Some(lit.value());
				}
				"allow_change" => {
					let lit: LitBool = input.parse()?;
					allow_change = Some(lit.value());
				}
				"allow_delete" => {
					let lit: LitBool = input.parse()?;
					allow_delete = Some(lit.value());
				}
				"permissions" => {
					let ident: Ident = input.parse()?;
					match ident.to_string().as_str() {
						"allow_all" => {
							permissions = Some("allow_all".to_string());
						}
						other => {
							return Err(syn::Error::new(
								ident.span(),
								format!(
									"unknown permission preset `{}`\n\n  = help: valid presets are: allow_all",
									other
								),
							));
						}
					}
				}
				unknown => {
					return Err(syn::Error::new(
						key.span(),
						format!(
							"unknown attribute `{}` for model admin\n\n  = help: valid attributes are: for, name, list_display, list_filter, search_fields, fields, readonly_fields, ordering, list_per_page, allow_view, allow_add, allow_change, allow_delete, permissions",
							unknown
						),
					));
				}
			}

			// Optional trailing comma
			if input.peek(Token![,]) {
				input.parse::<Token![,]>()?;
			}
		}

		// Validate required fields
		let model_type = model_type.ok_or_else(|| {
			syn::Error::new(
				span,
				"`for` attribute is required for model admin\n\n  = help: add `for = ModelType` to specify the model type",
			)
		})?;

		let name = name.ok_or_else(|| {
			syn::Error::new(
				span,
				"`name` attribute is required for model admin\n\n  = help: add `name = \"ModelName\"` to specify the model name",
			)
		})?;

		Ok(AdminModelConfig {
			model_type,
			name,
			list_display,
			list_filter,
			search_fields,
			fields,
			readonly_fields,
			ordering,
			list_per_page,
			allow_view,
			allow_add,
			allow_change,
			allow_delete,
			permissions,
		})
	}
}

/// Parse an array of identifiers: [id, name, email]
fn parse_ident_array(input: ParseStream) -> Result<Vec<Ident>> {
	let content;
	bracketed!(content in input);

	let mut idents = Vec::new();
	while !content.is_empty() {
		idents.push(content.parse()?);
		if content.peek(Token![,]) {
			content.parse::<Token![,]>()?;
		} else {
			break;
		}
	}
	Ok(idents)
}

/// Parse an array of ordering specs: [(field, asc), (field, desc)]
fn parse_ordering_array(input: ParseStream) -> Result<Vec<OrderingSpec>> {
	let content;
	bracketed!(content in input);

	let specs: Punctuated<OrderingSpec, Token![,]> = content.call(Punctuated::parse_terminated)?;
	Ok(specs.into_iter().collect())
}

/// Generate the ModelAdmin trait implementation
pub(crate) fn admin_impl(args: TokenStream, input: ItemStruct) -> Result<TokenStream> {
	let admin_api = crate::crate_paths::get_reinhardt_admin_adapters_crate();
	let async_trait = crate::crate_paths::get_async_trait_crate();
	let orm_crate = crate::crate_paths::get_reinhardt_orm_crate();

	let config: AdminModelConfig = syn::parse2(args)?;
	let struct_name = &input.ident;
	let struct_vis = &input.vis;
	let struct_attrs = &input.attrs;

	let model_type = &config.model_type;
	let name = &config.name;

	// Collect all field identifiers for validation
	let mut all_fields: Vec<&Ident> = Vec::new();
	if let Some(ref fields) = config.list_display {
		all_fields.extend(fields.iter());
	}
	if let Some(ref fields) = config.list_filter {
		all_fields.extend(fields.iter());
	}
	if let Some(ref fields) = config.search_fields {
		all_fields.extend(fields.iter());
	}
	if let Some(ref fields) = config.fields {
		all_fields.extend(fields.iter());
	}
	if let Some(ref fields) = config.readonly_fields {
		all_fields.extend(fields.iter());
	}
	if let Some(ref ordering) = config.ordering {
		all_fields.extend(ordering.iter().map(|o| &o.field));
	}

	// Generate field validation code
	let field_checks: Vec<TokenStream> = all_fields
		.iter()
		.map(|field| {
			let method_name = Ident::new(&format!("field_{}", field), field.span());
			quote! {
				let _ = #model_type::#method_name;
			}
		})
		.collect();

	// Generate table_name method from Model trait (Issue #2929)
	let table_name_impl = quote! {
		fn table_name(&self) -> &str {
			<#model_type as #orm_crate::Model>::table_name()
		}
	};

	// Generate list_display method
	let list_display_impl = if let Some(ref fields) = config.list_display {
		let field_strs: Vec<String> = fields.iter().map(|f| f.to_string()).collect();
		quote! {
			fn list_display(&self) -> Vec<&str> {
				vec![#(#field_strs),*]
			}
		}
	} else {
		quote! {}
	};

	// Generate list_filter method
	let list_filter_impl = if let Some(ref fields) = config.list_filter {
		let field_strs: Vec<String> = fields.iter().map(|f| f.to_string()).collect();
		quote! {
			fn list_filter(&self) -> Vec<&str> {
				vec![#(#field_strs),*]
			}
		}
	} else {
		quote! {}
	};

	// Generate search_fields method
	let search_fields_impl = if let Some(ref fields) = config.search_fields {
		let field_strs: Vec<String> = fields.iter().map(|f| f.to_string()).collect();
		quote! {
			fn search_fields(&self) -> Vec<&str> {
				vec![#(#field_strs),*]
			}
		}
	} else {
		quote! {}
	};

	// Generate fields method
	let fields_impl = if let Some(ref fields) = config.fields {
		let field_strs: Vec<String> = fields.iter().map(|f| f.to_string()).collect();
		quote! {
			fn fields(&self) -> Option<Vec<&str>> {
				Some(vec![#(#field_strs),*])
			}
		}
	} else {
		quote! {}
	};

	// Generate readonly_fields method
	let readonly_fields_impl = if let Some(ref fields) = config.readonly_fields {
		let field_strs: Vec<String> = fields.iter().map(|f| f.to_string()).collect();
		quote! {
			fn readonly_fields(&self) -> Vec<&str> {
				vec![#(#field_strs),*]
			}
		}
	} else {
		quote! {}
	};

	// Generate ordering method
	let ordering_impl = if let Some(ref ordering) = config.ordering {
		let ordering_strs: Vec<String> = ordering
			.iter()
			.map(|o| {
				let prefix = if o.order == Order::Desc { "-" } else { "" };
				format!("{}{}", prefix, o.field)
			})
			.collect();
		quote! {
			fn ordering(&self) -> Vec<&str> {
				vec![#(#ordering_strs),*]
			}
		}
	} else {
		quote! {}
	};

	// Generate list_per_page method
	let list_per_page_impl = if let Some(count) = config.list_per_page {
		quote! {
			fn list_per_page(&self) -> Option<usize> {
				Some(#count)
			}
		}
	} else {
		quote! {}
	};

	// Generate permission methods (Issue #2931)
	let (perm_view, perm_add, perm_change, perm_delete) =
		if config.permissions.as_deref() == Some("allow_all") {
			(true, true, true, true)
		} else {
			(
				config.allow_view.unwrap_or(false),
				config.allow_add.unwrap_or(false),
				config.allow_change.unwrap_or(false),
				config.allow_delete.unwrap_or(false),
			)
		};

	let permission_impls = quote! {
		async fn has_view_permission(&self, _user: &dyn #admin_api::AdminUser) -> bool {
			#perm_view
		}

		async fn has_add_permission(&self, _user: &dyn #admin_api::AdminUser) -> bool {
			#perm_add
		}

		async fn has_change_permission(&self, _user: &dyn #admin_api::AdminUser) -> bool {
			#perm_change
		}

		async fn has_delete_permission(&self, _user: &dyn #admin_api::AdminUser) -> bool {
			#perm_delete
		}
	};

	Ok(quote! {
		#(#struct_attrs)*
		#struct_vis struct #struct_name;

		// Compile-time field validation
		const _: () = {
			#(#field_checks)*
		};

		#[#async_trait::async_trait]
		impl #admin_api::ModelAdmin for #struct_name {
			fn model_name(&self) -> &str {
				#name
			}

			#table_name_impl
			#list_display_impl
			#list_filter_impl
			#search_fields_impl
			#fields_impl
			#readonly_fields_impl
			#ordering_impl
			#list_per_page_impl
			#permission_impls
		}
	})
}