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
//! Modular handler crafting for Salvo web framework.
//!
//! This crate provides the `#[craft]` attribute macro that enables a more
//! ergonomic way to define handlers as methods on structs, allowing for
//! better code organization and state sharing.
//!
//! # Overview
//!
//! Instead of writing standalone handler functions, you can organize related
//! handlers as methods on a struct, with shared state accessible via `self`:
//!
//! ```ignore
//! use salvo::prelude::*;
//! use salvo_craft::craft;
//!
//! #[derive(Clone)]
//! pub struct UserService {
//! db: DatabasePool,
//! }
//!
//! #[craft]
//! impl UserService {
//! fn new(db: DatabasePool) -> Self {
//! Self { db }
//! }
//!
//! #[craft(handler)]
//! async fn get_user(&self, id: PathParam<i64>) -> Result<Json<User>, StatusError> {
//! let user = self.db.get_user(*id).await?;
//! Ok(Json(user))
//! }
//!
//! #[craft(handler)]
//! async fn list_users(&self) -> Json<Vec<User>> {
//! Json(self.db.list_users().await)
//! }
//! }
//! ```
//!
//! # Usage
//!
//! ## Basic Handler
//!
//! Use `#[craft(handler)]` to mark a method as a handler:
//!
//! ```ignore
//! #[craft]
//! impl MyService {
//! #[craft(handler)]
//! fn hello(&self) -> &'static str {
//! "Hello, World!"
//! }
//! }
//! ```
//!
//! ## With OpenAPI Support
//!
//! Use `#[craft(endpoint(...))]` for handlers that should be included in
//! OpenAPI documentation:
//!
//! ```ignore
//! #[craft]
//! impl MyService {
//! #[craft(endpoint(tags("users"), status_codes(200, 404)))]
//! async fn get_user(&self, id: PathParam<i64>) -> Result<Json<User>, StatusError> {
//! // ...
//! }
//! }
//! ```
//!
//! # Method Receivers
//!
//! The `#[craft]` macro supports different method receivers:
//!
//! | Receiver | Requirement | Use Case |
//! |----------|-------------|----------|
//! | `&self` | Type must implement `Clone` | Most common, shared state |
//! | `Arc<Self>` | None | Explicit reference counting |
//! | None (static) | None | Stateless handlers |
//!
//! ## Examples
//!
//! ```ignore
//! #[derive(Clone)]
//! pub struct Service { /* ... */ }
//!
//! #[craft]
//! impl Service {
//! // Uses &self - Service must be Clone
//! #[craft(handler)]
//! fn with_ref(&self) -> String { /* ... */ }
//!
//! // Uses Arc<Self> - explicit shared ownership
//! #[craft(handler)]
//! fn with_arc(self: Arc<Self>) -> String { /* ... */ }
//!
//! // Static method - no self
//! #[craft(handler)]
//! fn static_handler() -> String { /* ... */ }
//! }
//! ```
//!
//! # Router Integration
//!
//! Craft handlers are used with routers just like regular handlers:
//!
//! ```ignore
//! let service = UserService::new(db_pool);
//!
//! let router = Router::new()
//! .push(Router::with_path("users/<id>").get(service.get_user()))
//! .push(Router::with_path("users").get(service.list_users()));
//! ```
pub use *;