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
use TokenStream;
/// Derive macro for creating a Cog with automatic dependency injection.
///
/// Generates `impl Cog`, a `CogFactory`, and inventory registration.
///
/// # Field Attributes
///
/// - `#[inject]` - Dependency injection. Field type must be `Arc<T>` where `T: Cog`.
/// - `#[config]` - Load from configuration. Type must implement `CogConfig + Default`.
/// - `#[default(fn)]` - Initialize via sync function: `fn() -> T`.
/// - `#[default_async(fn)]` - Initialize via async function: `async fn(&Arc<Hub>) -> Result<T, Error>`.
/// - No attribute - Uses `Default::default()`.
///
/// # Example
///
/// ```ignore
/// #[cog]
/// struct UserService {
/// #[inject]
/// db: Arc<Database>,
/// #[config]
/// settings: UserServiceConfig,
/// request_count: u64,
/// }
/// ```
/// Defines a GET route handler with automatic dependency injection.
///
/// # Parameter Injection
///
/// Parameters typed as `Arc<T>` where `T: Cog` are automatically rewritten to use
/// `Inject<T>`, which extracts the service from the Hub at request time. Other
/// axum extractors (`Json`, `Path`, `Query`, etc.) pass through unchanged.
///
/// For example, `repo: Arc<UserRepo>` expands to `Inject(repo): Inject<UserRepo>`.
///
/// # Example
///
/// ```ignore
/// #[get("/users")]
/// async fn list_users(repo: Arc<UserRepo>) -> impl IntoResponse {
/// Json(repo.find_all().await)
/// }
/// ```
/// Defines a POST route handler with automatic dependency injection.
///
/// `Arc<T>` parameters are rewritten to `Inject<T>` (see [`get`] for details).
///
/// # Example
///
/// ```ignore
/// #[post("/users")]
/// async fn create_user(
/// repo: Arc<UserRepo>,
/// body: Json<CreateUser>,
/// ) -> impl IntoResponse {
/// let user = repo.create(body.0).await;
/// (StatusCode::CREATED, Json(user))
/// }
/// ```
/// Defines a PUT route handler with automatic dependency injection.
///
/// `Arc<T>` parameters are rewritten to `Inject<T>` (see [`get`] for details).
///
/// # Example
///
/// ```ignore
/// #[put("/users/{id}")]
/// async fn update_user(
/// path: Path<String>,
/// repo: Arc<UserRepo>,
/// body: Json<UpdateUser>,
/// ) -> impl IntoResponse {
/// Json(repo.update(&path, body.0).await)
/// }
/// ```
/// Defines a DELETE route handler with automatic dependency injection.
///
/// `Arc<T>` parameters are rewritten to `Inject<T>` (see [`get`] for details).
///
/// # Example
///
/// ```ignore
/// #[delete("/users/{id}")]
/// async fn delete_user(
/// path: Path<String>,
/// repo: Arc<UserRepo>,
/// ) -> impl IntoResponse {
/// repo.delete(&path).await;
/// StatusCode::NO_CONTENT
/// }
/// ```
/// Defines a PATCH route handler with automatic dependency injection.
///
/// `Arc<T>` parameters are rewritten to `Inject<T>` (see [`get`] for details).
///
/// # Example
///
/// ```ignore
/// #[patch("/users/{id}")]
/// async fn patch_user(
/// path: Path<String>,
/// repo: Arc<UserRepo>,
/// body: Json<PatchUser>,
/// ) -> impl IntoResponse {
/// Json(repo.patch(&path, body.0).await)
/// }
/// ```
/// Implements `CogConfig` trait for a struct with the given config key.
///
/// The struct must also derive `Default` and `serde::Deserialize`.
///
/// # Example
///
/// ```ignore
/// #[cog_config("database")]
/// #[derive(Default, Deserialize)]
/// pub struct DbConfig {
/// url: String,
/// max_connections: u32,
/// }
/// ```
/// Derive macro for implementing `PgEntity` and `PgRepository` traits.
///
/// Generates CRUD repository operations (create, update, find, delete) on `PgClient`.
///
/// # Struct Attributes
///
/// - `#[table("name")]` - Required. Database table name.
///
/// # Field Attributes
///
/// - `#[primary_key]` - Primary key field(s). Multiple fields create composite keys.
/// - `#[skip]` - Exclude from all DB operations. Field must implement `Default`.
/// - `#[skip_upsert]` - Exclude from UPDATE portion of upsert operations.
/// - `#[pg_type(Type)]` - Cast field to a different type when binding.
///
/// # Example
///
/// ```ignore
/// #[derive(PgEntity)]
/// #[table("users")]
/// pub struct User {
/// #[primary_key]
/// pub id: String,
/// pub name: String,
/// #[skip]
/// pub computed_field: String,
/// }
/// ```
/// Derive macro for generating REST CRUD endpoints with pagination.
///
/// Use alongside `PgEntity` to generate DTOs and route handlers
/// (GET, POST, PUT, PATCH, DELETE) registered via inventory.
///
/// # Struct Attributes
///
/// - `#[table("name")]` - Required. Database table name (from PgEntity).
/// - `#[crud(path = "/users")]` - REST path (default: pluralized snake_case).
/// - `#[crud(read_only)]` - Only generate read endpoints.
/// - `#[crud(skip_create)]` / `#[crud(skip_delete)]` - Skip specific endpoints.
///
/// # Field Attributes
///
/// - `#[primary_key]` - Primary key (from PgEntity).
/// - `#[auto_generated]` - DB-generated field (excluded from create/update DTOs).
/// - `#[readonly]` - Response-only field (e.g., `created_at`).
/// - `#[writeonly]` - Input-only field (e.g., `password_hash`).
///
/// # Example
///
/// ```ignore
/// #[derive(PgEntity, Crud)]
/// #[table("users")]
/// #[crud(path = "/users")]
/// pub struct User {
/// #[primary_key]
/// #[auto_generated]
/// pub id: Uuid,
/// pub name: String,
/// #[readonly]
/// pub created_at: DateTime<Utc>,
/// }
/// ```
/// Generates a Gearbox application entry point.
///
/// Replaces `main` with the Gearbox startup sequence (tokio runtime + framework init).
///
/// # Example
///
/// ```ignore
/// #[gearbox_app]
/// fn main() {}
/// ```
/// Generate custom query methods on `PgClient`.
///
/// Define SQL queries with type-safe parameters and return types.
/// Placeholder count is validated at compile time.
///
/// # Return Types
///
/// - `Option<T>` — `fetch_optional`
/// - `Vec<T>` — `fetch_all`
/// - `T` (struct) — `fetch_one`
/// - Scalars (`i64`, `String`, etc.) — `query_scalar`
/// - `bool` — `rows_affected > 0`
/// - `u64` — `rows_affected`
/// - (none) — `execute`
///
/// # Example
///
/// ```ignore
/// pg_queries! {
/// fn find_by_email(email: &str) -> Option<User> {
/// "SELECT * FROM users WHERE email = $1"
/// }
///
/// fn count_active() -> i64 {
/// "SELECT COUNT(*) FROM users WHERE active = true"
/// }
/// }
/// ```