laravel_rust_admin/lib.rs
1//! An admin panel for [Laravel for Rust](https://laravel.com/rust) applications.
2//!
3//! Describe your models as resources, add the panel's service provider, and your application
4//! serves a back office at `/admin`: a table for each model with search, filters, tabs, sorting,
5//! and CSV export, forms that create and edit records, your own actions on one record or on many,
6//! a dashboard, and a command palette that searches everything. The panel's frontend is built
7//! into the crate, so the application needs no Node toolchain.
8//!
9//! # A resource from a model
10//!
11//! The quickest resource is a derive on the model. The table and the form come from its fields,
12//! and the inputs you name say what the panel may write:
13//!
14//! ```ignore
15//! use laravel::model::model;
16//! use laravel::time::Timestamp;
17//! use laravel_rust_admin::AdminResource;
18//!
19//! #[model]
20//! #[derive(AdminResource)]
21//! #[admin(icon = "users", create = StoreCustomer, update = StoreCustomer)]
22//! pub struct Customer {
23//! pub id: i64,
24//! pub name: String,
25//! pub email: String,
26//! pub created_at: Timestamp,
27//! pub updated_at: Timestamp,
28//! }
29//!
30//! #[laravel::input(fills = Customer)]
31//! #[validate(authenticated)]
32//! pub struct StoreCustomer {
33//! #[validate(required, max_length(120))]
34//! pub name: String,
35//! #[validate(required, email)]
36//! pub email: String,
37//! }
38//! ```
39//!
40//! The panel creates and updates records only through those input structs. Their validation
41//! rules run on every save, their messages appear under the form's fields, and a field the
42//! struct doesn't declare can't be written, whatever the browser sends.
43//!
44//! For a table with tabs, a column from a relationship, actions, or authorization, implement
45//! [`AdminResource`] by hand instead.
46//!
47//! # The panel
48//!
49//! [`AdminPanel`] is a service provider. Give it the resources, say who may enter, and add it in
50//! `src/app.rs`:
51//!
52//! ```ignore
53//! use laravel::Application;
54//! use laravel::auth::Authenticate;
55//! use laravel::http::Request;
56//! use laravel_rust_admin::AdminPanel;
57//!
58//! async fn admins_only(request: &Request) -> laravel::Result<bool> {
59//! Ok(request.user::<User>().await?.is_some_and(|user| user.is_admin))
60//! }
61//!
62//! Application::configure()
63//! .with_providers(|providers| {
64//! providers.add(
65//! AdminPanel::new()
66//! .brand("Acme")
67//! .middleware(Authenticate)
68//! .authorize(admins_only)
69//! .resource::<Customer>()
70//! .resource::<OrderResource>(),
71//! );
72//! })
73//! ```
74//!
75//! A panel with neither [`middleware`](AdminPanel::middleware) nor an
76//! [`authorize`](AdminPanel::authorize) gate would be open to anyone who finds its URL. Such a
77//! panel only answers in the `local` and `testing` environments. Everywhere else it responds
78//! with a `403` until you add one of the two.
79//!
80//! # Cargo features
81//!
82//! The panel uses whichever database driver your application enables on `laravel-rust`, so an
83//! application needs none of this crate's features. `sqlite`, `postgres`, and `mysql` only exist
84//! to build the crate by itself, as its own tests do.
85
86#![warn(missing_docs)]
87
88pub mod config;
89pub mod http;
90mod model;
91pub mod panel;
92pub mod protocol;
93pub mod resource;
94pub mod schema;
95pub mod widget;
96
97pub use config::{AccentColor, AdminConfig};
98pub use laravel_rust_admin_macros::AdminResource;
99pub use panel::{AdminPanel, AdminUser, PanelState, RequestFn};
100pub use protocol::{BadgeColor, ChartStyle, ColumnAlign, ColumnKind, FieldKind, SortDirection, TrendDirection};
101pub use resource::{AdminResource, NavigationBadge, ReadOnly, ResourceInput};
102pub use schema::{
103 ActionConfirmation, ActionContext, ActionOutcome, BulkAction, Column, Field, Filter, FormBuilder, FormSection,
104 RowAction, SelectOption, TableBuilder, TableTab,
105};
106pub use widget::{ChartWidget, DashboardWidget, StatWidget, TableWidget};
107
108/// Convenient imports for defining admin resources, widgets, and panels.
109pub mod prelude {
110 pub use crate::{
111 AccentColor, ActionConfirmation, ActionContext, ActionOutcome, AdminPanel, AdminResource, AdminUser,
112 BadgeColor, BulkAction, ChartStyle, ChartWidget, Column, ColumnAlign, DashboardWidget, Field, Filter,
113 FormBuilder, FormSection, NavigationBadge, ReadOnly, RowAction, SelectOption, SortDirection, StatWidget,
114 TableBuilder, TableTab, TableWidget, TrendDirection,
115 };
116}