laravel-rust-admin 0.1.0

Admin panel framework for Laravel for Rust: auto-generated CRUD tables, forms, actions, and dashboard widgets from your models.
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! What the panel's server and its browser app say to each other: every request and response body
//! of the panel's JSON API.
//!
//! Each type here is plain data with a twin in `frontend/src/api/types.ts`. The twins are kept in
//! step by hand for now. They are meant to be generated from these types by
//! `laravel-rust-typescript-bridge`, which is why nothing here borrows or holds a closure.

// The builders in `schema` and `widget` document what each of these fields means. The types here
// only carry the values to the browser.
#![allow(missing_docs)]

use std::collections::BTreeMap;

use laravel::validation::Validate;
use serde::{Deserialize, Serialize};

/// The color of the panel's primary buttons, checked controls, and brand mark. `Zinc`, the
/// default, is near-black on a light page and white on a dark one.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AccentColor {
    #[default]
    Zinc,
    Indigo,
    Blue,
    Emerald,
    Amber,
    Rose,
    Violet,
}

impl AccentColor {
    /// The accent's name, as the panel's stylesheet spells it.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Zinc => "zinc",
            Self::Indigo => "indigo",
            Self::Blue => "blue",
            Self::Emerald => "emerald",
            Self::Amber => "amber",
            Self::Rose => "rose",
            Self::Violet => "violet",
        }
    }
}

/// Badge color token used for status pills, sidebar badges, and deltas.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BadgeColor {
    #[default]
    Zinc,
    Emerald,
    Amber,
    Rose,
    Sky,
    Indigo,
    Violet,
}

/// Sort direction for table queries.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SortDirection {
    #[default]
    Asc,
    Desc,
}

/// Initial bootstrap payload injected into the HTML shell so the SPA knows its mount path
/// before its first API call.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellBootstrap {
    pub base_path: String,
    pub api_base: String,
    pub brand: String,
    pub subtitle: Option<String>,
    pub accent: AccentColor,
    pub logout_url: Option<String>,
}

/// Information about the currently signed-in admin user shown in the sidebar footer & dashboard greeting.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdminUserDto {
    pub name: String,
    pub email: Option<String>,
    pub avatar_url: Option<String>,
    pub role: Option<String>,
}

/// Sidebar badge displayed next to a resource item (e.g. unread count).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavigationBadgeDto {
    pub label: String,
    pub color: BadgeColor,
}

/// Effective permissions for the current user on a resource.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ResourcePermissionsDto {
    pub view_any: bool,
    pub create: bool,
    pub edit: bool,
    pub delete: bool,
}

/// Full panel schema returned by `GET {base}/api/panel`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PanelSchemaResponse {
    pub brand: String,
    pub subtitle: Option<String>,
    pub base_path: String,
    pub accent: AccentColor,
    pub logout_url: Option<String>,
    pub user: Option<AdminUserDto>,
    pub resources: Vec<ResourceSchemaDto>,
}

/// Complete schema describing one admin resource (navigation, table, form, actions, permissions).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceSchemaDto {
    pub slug: String,
    pub label: String,
    pub plural_label: String,
    pub icon: String,
    pub navigation_group: Option<String>,
    pub navigation_sort: i32,
    pub navigation_badge: Option<NavigationBadgeDto>,
    pub key_column: String,
    pub title_column: Option<String>,
    pub permissions: ResourcePermissionsDto,
    pub per_page_options: Vec<u64>,
    pub default_per_page: u64,
    pub searchable: bool,
    pub exportable: bool,
    pub default_sort: Option<DefaultSortDto>,
    pub columns: Vec<ColumnSchemaDto>,
    pub filters: Vec<FilterSchemaDto>,
    pub tabs: Vec<TableTabSchemaDto>,
    pub row_actions: Vec<ActionSchemaDto>,
    pub bulk_actions: Vec<ActionSchemaDto>,
    pub form_sections: Vec<FormSectionSchemaDto>,
}

/// Default sort configuration for a resource table.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DefaultSortDto {
    pub column: String,
    pub direction: SortDirection,
}

/// Column renderer kind.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ColumnKind {
    #[default]
    Text,
    Badge,
    Boolean,
    Number,
    Currency,
    Datetime,
    Date,
    RelativeTime,
    Avatar,
    Tags,
    Json,
}

/// Column horizontal alignment.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ColumnAlign {
    #[default]
    Left,
    Center,
    Right,
}

/// Schema for a single table column.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColumnSchemaDto {
    pub name: String,
    pub label: String,
    pub kind: ColumnKind,
    pub align: ColumnAlign,
    pub sortable: bool,
    pub searchable: bool,
    pub toggleable: bool,
    pub hidden_by_default: bool,
    pub copyable: bool,
    pub mono: bool,
    pub prefix: Option<String>,
    pub suffix: Option<String>,
    pub currency: Option<String>,
    pub description_column: Option<String>,
    pub limit: Option<usize>,
    pub badge_colors: BTreeMap<String, BadgeColor>,
    pub badge_dot: bool,
}

/// Option for select/radio inputs and filters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SelectOptionDto {
    pub value: String,
    pub label: String,
    pub description: Option<String>,
}

/// Filter control type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum FilterKindDto {
    Select { options: Vec<SelectOptionDto> },
    Boolean { true_label: String, false_label: String },
    DateRange,
    Text { placeholder: Option<String> },
}

/// Schema for one table filter chip.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilterSchemaDto {
    pub name: String,
    pub label: String,
    pub control: FilterKindDto,
}

/// A tab above a resource's table.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableTabSchemaDto {
    pub key: String,
    pub label: String,
    pub badge_color: Option<BadgeColor>,
}

/// Form input field kind.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FieldKind {
    #[default]
    Text,
    Email,
    Url,
    Password,
    Textarea,
    Number,
    Decimal,
    Select,
    Radio,
    Toggle,
    Checkbox,
    Date,
    /// An instant, such as a `Timestamp`. The browser edits it in the user's time zone.
    Datetime,
    /// A wall-clock date and time without a time zone, such as a `civil::DateTime`.
    CivilDatetime,
    Tags,
    Json,
    Readonly,
}

/// A section of a resource's form: a card with a heading and some of the form's fields.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormSectionSchemaDto {
    pub title: Option<String>,
    pub description: Option<String>,
    pub columns: u8,
    pub fields: Vec<FieldSchemaDto>,
}

/// Schema for a single form field.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldSchemaDto {
    pub name: String,
    pub label: String,
    pub kind: FieldKind,
    pub required: bool,
    pub nullable: bool,
    pub disabled: bool,
    pub placeholder: Option<String>,
    pub helper: Option<String>,
    pub prefix: Option<String>,
    pub suffix: Option<String>,
    pub rows: Option<u8>,
    pub min: Option<f64>,
    pub max: Option<f64>,
    pub max_length: Option<usize>,
    pub column_span: u8,
    pub default_value: Option<serde_json::Value>,
    pub options: Vec<SelectOptionDto>,
    pub create_only: bool,
    pub edit_only: bool,
}

/// Confirmation prompt configuration for an action.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionConfirmationDto {
    pub title: String,
    pub description: String,
    pub confirm_label: String,
}

/// Schema for a row action or bulk action.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionSchemaDto {
    pub name: String,
    pub label: String,
    pub icon: Option<String>,
    pub destructive: bool,
    pub confirmation: Option<ActionConfirmationDto>,
    pub fields: Vec<FieldSchemaDto>,
}

/// Paginated resource list response (`GET {base}/api/resources/{slug}`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceListResponse {
    pub data: Vec<RecordEnvelopeDto>,
    pub meta: PaginationMetaDto,
    pub tab_counts: BTreeMap<String, u64>,
}

/// Wrapper around a serialized model record with resolved key, title, and per-row permissions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordEnvelopeDto {
    pub key: String,
    pub title: Option<String>,
    pub data: serde_json::Value,
    pub can_view: bool,
    pub can_edit: bool,
    pub can_delete: bool,
    /// The names of the row actions that apply to the record and that the user may run on it.
    pub actions: Vec<String>,
}

/// Pagination metadata matching Laravel's paginator semantics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginationMetaDto {
    pub current_page: u64,
    pub last_page: u64,
    pub per_page: u64,
    pub total: u64,
    pub from: Option<u64>,
    pub to: Option<u64>,
}

/// Single record response (`GET/POST/PATCH {base}/api/resources/{slug}[/{id}]`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordResponse {
    pub record: RecordEnvelopeDto,
    pub message: Option<String>,
}

/// Payload sent when invoking a custom row action (`POST .../actions/{action}`).
#[derive(Debug, Clone, Default, Serialize, Deserialize, Validate)]
pub struct RunRowActionRequest {
    #[serde(default)]
    pub data: BTreeMap<String, serde_json::Value>,
}

/// Payload sent when invoking a bulk action (`POST .../bulk/{action}`).
#[derive(Debug, Clone, Default, Serialize, Deserialize, Validate)]
pub struct RunBulkActionRequest {
    pub ids: Vec<String>,
    #[serde(default)]
    pub data: BTreeMap<String, serde_json::Value>,
}

/// Result returned by a row action, bulk action, or delete endpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionResponse {
    pub message: String,
    pub affected: u64,
    pub redirect_to: Option<String>,
    pub refresh: bool,
}

/// Global search response (`GET {base}/api/search?q=...`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalSearchResponse {
    pub query: String,
    pub groups: Vec<GlobalSearchGroupDto>,
}

/// Search matches grouped by resource.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalSearchGroupDto {
    pub resource_slug: String,
    pub resource_label: String,
    pub icon: String,
    pub hits: Vec<GlobalSearchHitDto>,
}

/// Single hit returned by global search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalSearchHitDto {
    pub key: String,
    pub title: String,
    pub subtitle: Option<String>,
}

/// Dashboard response (`GET {base}/api/dashboard`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardResponse {
    pub widgets: Vec<DashboardWidgetDto>,
}

/// Which way a stat's value moved.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TrendDirection {
    Up,
    Down,
    Neutral,
}

/// Chart visual style.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChartStyle {
    Bar,
    Area,
    HorizontalBar,
}

/// One data point in a chart widget.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChartPointDto {
    pub label: String,
    pub value: f64,
    pub color: Option<BadgeColor>,
}

/// One column in a compact dashboard table widget.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableWidgetColumnDto {
    pub key: String,
    pub label: String,
    pub align: ColumnAlign,
    pub badge_colors: BTreeMap<String, BadgeColor>,
}

/// One row in a compact dashboard table widget.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableWidgetRowDto {
    pub key: String,
    pub href: Option<String>,
    pub cells: BTreeMap<String, serde_json::Value>,
}

/// Discriminated union of dashboard widgets.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum DashboardWidgetDto {
    Stat {
        id: String,
        label: String,
        value: String,
        description: Option<String>,
        trend: Option<String>,
        trend_direction: Option<TrendDirection>,
        icon: Option<String>,
        color: BadgeColor,
        sparkline: Vec<f64>,
        href: Option<String>,
        col_span: u8,
    },
    Chart {
        id: String,
        title: String,
        subtitle: Option<String>,
        style: ChartStyle,
        points: Vec<ChartPointDto>,
        value_prefix: Option<String>,
        value_suffix: Option<String>,
        col_span: u8,
    },
    Table {
        id: String,
        title: String,
        subtitle: Option<String>,
        action_label: Option<String>,
        action_href: Option<String>,
        columns: Vec<TableWidgetColumnDto>,
        rows: Vec<TableWidgetRowDto>,
        col_span: u8,
    },
}

/// Standard Laravel 422 validation error response shape.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationErrorResponse {
    pub message: String,
    pub errors: BTreeMap<String, Vec<String>>,
}