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
496
497
498
499
500
501
502
//! Core crate for the cinderblock framework — a declarative, resource-oriented
//! application framework for Rust.
//!
//! This crate provides the [`resource!`] macro, the [`Resource`] trait, CRUD
//! operation traits ([`Create`], [`Update`], [`Destroy`], [`ReadAction`]), the
//! runtime [`Context`], and a built-in [`InMemoryDataLayer`](data_layer::in_memory::InMemoryDataLayer)
//! for prototyping.
//!
//! # The `resource!` macro
//!
//! The [`resource!`] macro is the primary entry point for defining domain
//! models. It accepts a declarative DSL and generates:
//!
//! - A **struct** with the declared attributes (derives `Serialize`,
//! `Deserialize`, `Clone`, `Debug`).
//! - A [`Resource`] trait impl with primary key metadata and the configured
//! data layer.
//! - For each action, a **marker struct** and the corresponding CRUD trait
//! impl. Create and update actions also generate an **input struct**.
//! - Extension dispatch — each declared extension receives the full DSL
//! tokens so it can generate its own code (e.g. route handlers, SQL
//! queries).
//!
//! ## DSL reference
//!
//! ```rust,ignore
//! use cinderblock_core::resource;
//!
//! resource! {
//! // A dotted name identifying the resource. The last segment becomes the
//! // struct name; all segments are available at runtime via `Resource::NAME`.
//! name = Helpdesk.Support.Ticket;
//!
//! // Optional: override the data layer. Defaults to `InMemoryDataLayer`.
//! // data_layer = cinderblock_sqlx::sqlite::SqliteDataLayer;
//!
//! attributes {
//! // Each attribute is `name Type` followed by either `;` or an options block.
//! ticket_id Uuid {
//! primary_key true; // Marks this as the primary key (default: false).
//! writable false; // Excludes from create/update input structs (default: true).
//! generated true; // Indicates the PK is auto-generated (default: false).
//! default || Uuid::new_v4(); // Closure producing a default value.
//! }
//!
//! // Simple form — writable, not a primary key, no default.
//! subject String;
//! status TicketStatus;
//! }
//!
//! actions {
//! // ── Read actions ──
//! //
//! // A read action returns `Vec<Resource>`. It can optionally declare
//! // arguments (typed query parameters) and filters.
//!
//! // Minimal read — no filters, no arguments. Arguments type is `()`.
//! read all;
//!
//! // Read with a compile-time literal filter.
//! read open_tickets {
//! filter { status == TicketStatus::Open };
//! };
//!
//! // Read with a runtime argument bound to a filter.
//! // Generates a `ByStatusArguments` struct with a `status` field.
//! read by_status {
//! argument { status: TicketStatus };
//! filter { status == arg(status) };
//! };
//!
//! // Optional arguments use `Option<T>`. When `None`, the filter is
//! // skipped entirely at runtime.
//! read search {
//! argument { status: Option<TicketStatus> };
//! filter { status == arg(status) };
//! };
//!
//! // ── Create actions ──
//! //
//! // A create action generates an input struct from the resource's
//! // writable attributes and a `Create<A>` impl that builds a new
//! // resource instance.
//!
//! // Accepts all writable attributes. Generates `OpenInput { subject, status }`.
//! create open;
//!
//! // Restrict which fields the input struct includes.
//! // Generates `AssignInput { subject }`.
//! create assign {
//! accept [subject];
//! };
//!
//! // ── Update actions ──
//! //
//! // An update action fetches the resource by primary key, applies
//! // changes, and persists the result. It generates an input struct
//! // and an `Update<A>` impl.
//!
//! // Accepts all writable attributes.
//! update edit;
//!
//! // Accept no fields from the caller, but apply a programmatic
//! // mutation via `change_ref`. Multiple `change_ref` blocks are
//! // applied in order.
//! update close {
//! accept [];
//! change_ref |ticket| {
//! ticket.status = TicketStatus::Closed;
//! };
//! };
//!
//! // ── Destroy actions ──
//! //
//! // A destroy action deletes the resource by primary key.
//! destroy remove;
//! }
//!
//! // Optional: declare extensions. Each extension module receives the
//! // full resource DSL and its own configuration block, then generates
//! // additional code (e.g. route handlers, SQL queries).
//! extensions {
//! cinderblock_json_api {
//! route = { method = GET; path = "/"; action = all; };
//! route = { method = POST; path = "/"; action = open; };
//! };
//!
//! cinderblock_sqlx {
//! table = "tickets";
//! };
//! }
//! }
//! ```
//!
//! ## Generated items
//!
//! For a resource named `Helpdesk.Support.Ticket` with actions `open`
//! (create), `close` (update), `open_tickets` (read), and `remove` (destroy),
//! the macro generates:
//!
//! | Generated item | Kind | Description |
//! |---|---|---|
//! | `Ticket` | struct | The resource struct with all declared attributes |
//! | `Open` | struct (marker) | Create action marker |
//! | `OpenInput` | struct | Input fields for the `open` create action |
//! | `Close` | struct (marker) | Update action marker |
//! | `CloseInput` | struct | Input fields for the `close` update action |
//! | `OpenTickets` | struct (marker) | Read action marker |
//! | `Remove` | struct (marker) | Destroy action marker |
//!
//! Action names are converted to `PascalCase` for the marker and input struct
//! names (e.g. `open_tickets` becomes `OpenTickets`, and its input struct
//! would be `OpenTicketsInput`).
//!
//! ## Using the generated types
//!
//! ```rust,ignore
//! use cinderblock_core::Context;
//!
//! let ctx = Context::new();
//!
//! // Create
//! let ticket = cinderblock_core::create::<Ticket, Open>(
//! OpenInput { subject: "Printer is broken".into(), status: TicketStatus::Open },
//! &ctx,
//! ).await?;
//!
//! // Read (with arguments)
//! let open = cinderblock_core::read::<Ticket, ByStatus>(
//! &ctx,
//! &ByStatusArguments { status: TicketStatus::Open },
//! ).await?;
//!
//! // Read (no arguments — pass `&()`)
//! let all_open = cinderblock_core::read::<Ticket, OpenTickets>(&ctx, &()).await?;
//!
//! // Update
//! let closed = cinderblock_core::update::<Ticket, Close>(
//! &ticket.ticket_id,
//! CloseInput {},
//! &ctx,
//! ).await?;
//!
//! // Destroy
//! let removed = cinderblock_core::destroy::<Ticket, Remove>(
//! &ticket.ticket_id,
//! &ctx,
//! ).await?;
//! ```
use ;
pub use resource;
pub use serde;
pub use thiserror;
use crateDataLayer;
// ---------------------------------------------------------------------------
// # Error Types
// ---------------------------------------------------------------------------
/// Structured error carrying the resource name and an action-specific error
/// variant. The type parameter `E` differs per CRUD action so that callers
/// can match on only the variants relevant to the operation that failed.
/// Error variants for create operations.
/// Error variants for single-resource read operations (by primary key).
/// Error variants for read-action (list) operations.
/// Error variants for update operations.
/// Error variants for destroy operations.
/// Default number of items per page for paged read actions.
///
/// Individual actions can override this via `default_per_page` in the DSL.
pub const DEFAULT_PER_PAGE: u32 = 100;
// ---------------------------------------------------------------------------
// # Pagination Types
// ---------------------------------------------------------------------------
/// Result type for paged read actions, containing the data page and metadata
/// needed to navigate the full result set.
/// Metadata describing the current page position within the full result set.
/// Trait implemented on the **Arguments type** of paged read actions.
///
/// The generated `Paged` impl resolves `Option<u32>` fields into concrete
/// page/per_page values using defaults and clamping from the DSL config.
/// Marker trait for a resource.
/// Marker trait indicating that a struct is a read action.
///
/// Non-paged actions set `Response = Vec<Output>`. Paged actions set
/// `Response = PaginatedResult<Output>`. This lets the framework return
/// the correct shape without runtime branching.
/// Trait indicating that a [`DataLayer`] can perform [`ReadAction`] `A`.
/// Trait placed on a [`Resource`] specifying how to create the resource using action `A`.
/// Trait placed on a [`Resource`] specifying how to update a resource using action `A`.
/// Marker trait for destroy actions.
/// Create resource `R` using action `A`.
pub async
/// Update resource `R` using action `A`. First
/// fetches an instance of `R` using the primary key.
pub async
/// Read resource `R` using action `A`.
pub async
/// Destroy resource `R` using action `A`.
pub async