raisfast 0.2.23

The last backend you'll ever need. Rust-powered headless CMS with built-in blog, ecommerce, wallet, payment and 4 plugin engines.
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Protocol layer — Declarative protocol definitions
//!
//! Protocol = Aspect composition + declarative effects (ProtocolDeclaration).
//!
//! ## Design
//!
//! ```text
//! Protocol implementation
//!   ├─ aspects()        → Aspect list (AOP data injection)
//!   ├─ declaration()    → ProtocolDeclaration (pure data, compile-time safe)
//!   │     ├─ query_filters     — automatically appended WHERE conditions
//!   │     ├─ delete_strategy   — Soft / Hard
//!   │     ├─ snapshot_before_update — snapshot old record before update
//!   │     └─ revision_routes   — provide version history API
//!   └─ on_after_delete() → async hook (the only non-pure-data method)
//! ```
//!
//! ## Extending Protocols
//!
//! **Scenario A (common): new protocol only uses existing capabilities** → just add 1 file
//!
//! **Scenario B (rare): introduces a brand-new system integration point** → extend the
//! ProtocolDeclaration struct, and the compiler will error on all sites that need adaptation.

pub mod expirable;
pub mod lockable;
pub mod metaable;
pub mod nestable;
pub mod ownable;
pub mod soft_deletable;
pub mod sortable;
pub mod statusable;
pub mod tenantable;
pub mod timestampable;
pub mod versionable;

use crate::types::snowflake_id::SnowflakeId;
use std::collections::HashMap;
use std::sync::Arc;

use crate::aspects::{Aspect, ColumnDef};

// ─── DeleteStrategy ───

/// Delete strategy
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum DeleteStrategy {
    /// Physical DELETE (default)
    #[default]
    Hard,
    /// UPDATE SET column = now WHERE ...
    Soft { column: String },
}

// ─── SortDir ───

/// Sort direction
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SortDir {
    #[default]
    Asc,
    Desc,
}

// ─── ProtocolDeclaration ───

/// Status mode
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum StatusMode {
    #[default]
    String,
    Numeric,
}

/// All declarative effects a protocol has on the system
///
/// Pure data struct. New capabilities are added as fields.
/// Consumers read fields directly; the compiler guarantees type safety.
/// When extending this struct, the compiler will flag all sites needing adaptation.
#[derive(Debug, Clone, Default)]
pub struct ProtocolDeclaration {
    /// Query filters automatically appended as WHERE conditions: (column, SQL_condition)
    pub query_filters: Vec<(String, String)>,
    /// Delete strategy
    pub delete_strategy: DeleteStrategy,
    /// Whether to snapshot the current record before update (for version history)
    pub snapshot_before_update: bool,
    /// Whether to provide version history API routes (/revisions)
    pub revision_routes: bool,
    /// Optimistic lock column name (UPDATE WHERE column = ? + SET column = column + 1)
    pub lock_column: Option<String>,
    /// Default sort for list queries (column, direction)
    pub default_sort: Option<(String, SortDir)>,
    /// statusable: allowed status values
    pub status_values: Option<Vec<String>>,
    /// statusable: numeric mapping (label → number)
    pub status_map: Option<Vec<(String, i64)>>,
    /// statusable: default status value (label in string mode, label in numeric mode)
    pub status_default: Option<String>,
    /// statusable: storage mode
    pub status_mode: StatusMode,
}

impl ProtocolDeclaration {
    /// Merge another protocol declaration (Soft takes priority over Hard, bool uses OR, lock/sort: last wins)
    pub fn merge(&mut self, other: &ProtocolDeclaration) {
        self.query_filters
            .extend(other.query_filters.iter().cloned());
        if matches!(other.delete_strategy, DeleteStrategy::Soft { .. }) {
            self.delete_strategy = other.delete_strategy.clone();
        }
        if other.snapshot_before_update {
            self.snapshot_before_update = true;
        }
        if other.revision_routes {
            self.revision_routes = true;
        }
        if other.lock_column.is_some() {
            if self.lock_column.is_some() {
                tracing::warn!(
                    "conflict: lock_column already set, overwriting with {:?}",
                    other.lock_column
                );
            }
            self.lock_column = other.lock_column.clone();
        }
        if other.default_sort.is_some() {
            if self.default_sort.is_some() {
                tracing::warn!(
                    "conflict: default_sort already set, overwriting with {:?}",
                    other.default_sort
                );
            }
            self.default_sort = other.default_sort.clone();
        }
        if other.status_values.is_some() {
            self.status_values = other.status_values.clone();
        }
        if other.status_map.is_some() {
            self.status_map = other.status_map.clone();
        }
        if other.status_default.is_some() {
            self.status_default = other.status_default.clone();
        }
        if matches!(other.status_mode, StatusMode::Numeric) {
            self.status_mode = StatusMode::Numeric;
        }
    }

    /// Aggregate declarations from multiple protocols
    pub fn aggregated(names: &[String], registry: &ProtocolRegistry) -> Self {
        let mut agg = Self::default();
        for name in names {
            if let Some(protocol) = registry.get(name) {
                agg.merge(&protocol.declaration());
            }
        }
        agg.query_filters.sort_by(|a, b| a.0.cmp(&b.0));
        agg
    }

    pub fn is_soft_delete(&self) -> bool {
        matches!(self.delete_strategy, DeleteStrategy::Soft { .. })
    }

    pub fn is_lockable(&self) -> bool {
        self.lock_column.is_some()
    }

    pub fn is_sortable(&self) -> bool {
        self.default_sort.is_some()
    }
}

// ─── Protocol Trait ───

pub trait Protocol: Send + Sync + 'static {
    fn name(&self) -> &str;

    fn description(&self) -> &str {
        ""
    }

    fn aspects(&self) -> Vec<Arc<dyn Aspect>>;

    fn columns(&self) -> Vec<ColumnDef> {
        self.aspects().iter().flat_map(|a| a.columns()).collect()
    }

    fn behaviors(&self) -> Vec<&'static str> {
        vec![]
    }

    fn built_in(&self) -> bool {
        false
    }

    /// Protocol declarative effects (pure data)
    fn declaration(&self) -> ProtocolDeclaration {
        ProtocolDeclaration::default()
    }

    /// Apply user configuration to the declaration (e.g., sortable's field/direction)
    ///
    /// Default no-op. Protocols that need configuration override this method.
    fn apply_config(
        &self,
        _config: &HashMap<String, String>,
        _decl: &mut ProtocolDeclaration,
        _all_columns: &[&str],
    ) {
    }

    /// Register additional API routes required by the protocol
    ///
    /// Default no-op. Protocols that need routes override this method.
    fn register_routes(
        &self,
        _router: axum::Router<crate::AppState>,
        _plural: &str,
        _admin_prefix: &str,
    ) -> axum::Router<crate::AppState> {
        _router
    }

    /// Async callback after record deletion (e.g., cleanup related tables)
    ///
    /// This is the only hook that cannot be purely data-declared, because it involves
    /// async IO operations. Most protocols use the default no-op implementation.
    fn on_after_delete(
        &self,
        _pool: &crate::db::pool::Pool,
        _content_type_singular: &str,
        _record_id: SnowflakeId,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), anyhow::Error>> + Send + '_>>
    {
        Box::pin(async { Ok(()) })
    }
}

// ─── ProtocolEntry (inventory) ───

pub struct ProtocolEntry {
    pub factory: fn() -> Arc<dyn Protocol>,
}

inventory::collect!(ProtocolEntry);

/// Macro for self-registration within protocol files
#[macro_export]
macro_rules! register_protocol {
    ($protocol_type:ty, $instance:expr) => {
        ::inventory::submit! {
            $crate::protocols::ProtocolEntry {
                factory: || std::sync::Arc::new($instance),
            }
        }
    };
}

// ─── ProtocolRegistry ───

pub struct ProtocolRegistry {
    protocols: HashMap<String, Arc<dyn Protocol>>,
}

impl ProtocolRegistry {
    pub fn new() -> Self {
        Self {
            protocols: HashMap::new(),
        }
    }

    pub fn register(&mut self, protocol: impl Protocol) {
        let name = protocol.name().to_string();
        self.protocols.insert(name, Arc::new(protocol));
    }

    pub fn register_from_inventory(&mut self) {
        for entry in inventory::iter::<ProtocolEntry> {
            let name = (entry.factory)().name().to_string();
            self.protocols.insert(name, (entry.factory)());
        }
    }

    pub fn get(&self, name: &str) -> Option<&Arc<dyn Protocol>> {
        self.protocols.get(name)
    }

    pub fn names(&self) -> Vec<&str> {
        self.protocols.keys().map(|s| s.as_str()).collect()
    }

    pub fn columns_for(&self, names: &[String]) -> Vec<ColumnDef> {
        let mut cols = Vec::new();
        let mut seen: HashMap<String, (String, ColumnDef)> = HashMap::new();
        for name in names {
            if let Some(protocol) = self.protocols.get(name.as_str()) {
                for col in protocol.columns() {
                    if let Some((prev_proto, prev_col)) = seen.get(&col.name) {
                        if prev_col.sql_type != col.sql_type || prev_col.default != col.default {
                            tracing::warn!(
                                "column '{}' declared by '{}' ({:?}) and '{}' ({:?}): first wins",
                                col.name,
                                prev_proto,
                                prev_col.sql_type,
                                name,
                                col.sql_type,
                            );
                        }
                        continue;
                    }
                    seen.insert(col.name.clone(), (name.clone(), col.clone()));
                    cols.push(col);
                }
            }
        }
        cols
    }

    pub fn aspects_for(&self, names: &[String]) -> Vec<Arc<dyn Aspect>> {
        let mut aspects = Vec::new();
        let mut seen = std::collections::HashSet::new();
        for name in names {
            if let Some(protocol) = self.protocols.get(name.as_str()) {
                for aspect in protocol.aspects() {
                    if seen.insert(aspect.name().to_string()) {
                        aspects.push(aspect);
                    }
                }
            }
        }
        aspects
    }

    /// Aggregate declarations from multiple protocols
    pub fn declaration_for(&self, names: &[String]) -> ProtocolDeclaration {
        ProtocolDeclaration::aggregated(names, self)
    }

    /// Apply user configuration to the aggregated declaration
    pub fn apply_config_for(
        &self,
        impl_refs: &[crate::content_type::schema::ProtocolRef],
        decl: &mut ProtocolDeclaration,
        all_columns: &[&str],
    ) {
        for pref in impl_refs {
            if let Some(protocol) = self.protocols.get(pref.name()) {
                protocol.apply_config(pref.config(), decl, all_columns);
            }
        }
    }

    /// Register additional routes for all protocols
    pub fn register_routes_for(
        &self,
        names: &[String],
        router: axum::Router<crate::AppState>,
        plural: &str,
        admin_prefix: &str,
    ) -> axum::Router<crate::AppState> {
        let mut router = router;
        for name in names {
            if let Some(protocol) = self.protocols.get(name.as_str()) {
                router = protocol.register_routes(router, plural, admin_prefix);
            }
        }
        router
    }

    /// Post-delete callback
    pub async fn dispatch_after_delete(
        &self,
        names: &[String],
        pool: &crate::db::pool::Pool,
        content_type_singular: &str,
        record_id: SnowflakeId,
    ) -> Result<(), anyhow::Error> {
        for name in names {
            if let Some(protocol) = self.protocols.get(name.as_str()) {
                protocol
                    .on_after_delete(pool, content_type_singular, record_id)
                    .await?;
            }
        }
        Ok(())
    }

    pub fn register_aspects_into(&self, engine: &crate::aspects::engine::AspectEngine) {
        let mut seen = std::collections::HashSet::new();
        for protocol in self.protocols.values() {
            for aspect in protocol.aspects() {
                if seen.insert(aspect.name().to_string()) {
                    engine.register_from_arc(aspect);
                }
            }
        }
    }
}

impl Default for ProtocolRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for ProtocolRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let names: Vec<&str> = self.names();
        f.debug_struct("ProtocolRegistry")
            .field("protocols", &names)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn register_and_get() {
        let mut reg = ProtocolRegistry::new();
        reg.register(ownable::OwnableProtocol);
        assert!(reg.get("ownable").is_some());
        assert!(reg.get("nonexistent").is_none());
    }

    #[test]
    fn names_returns_all() {
        let mut reg = ProtocolRegistry::new();
        reg.register(ownable::OwnableProtocol);
        reg.register(timestampable::TimestampableProtocol);
        let mut names = reg.names();
        names.sort();
        assert_eq!(names, vec!["ownable", "timestampable"]);
    }

    #[test]
    fn columns_for_deduplicates() {
        let mut reg = ProtocolRegistry::new();
        reg.register(ownable::OwnableProtocol);
        reg.register(timestampable::TimestampableProtocol);
        let cols = reg.columns_for(&["ownable".into(), "timestampable".into()]);
        let col_names: Vec<&str> = cols.iter().map(|c| c.name.as_str()).collect();
        assert!(col_names.contains(&"created_by"));
        assert!(col_names.contains(&"updated_by"));
        assert!(col_names.contains(&"created_at"));
        assert!(col_names.contains(&"updated_at"));
    }

    #[test]
    fn aspects_for_deduplicates() {
        let mut reg = ProtocolRegistry::new();
        reg.register(ownable::OwnableProtocol);
        reg.register(timestampable::TimestampableProtocol);
        let aspects = reg.aspects_for(&["ownable".into(), "timestampable".into()]);
        assert_eq!(aspects.len(), 2);
    }

    #[test]
    fn declaration_aggregation() {
        let mut reg = ProtocolRegistry::new();
        reg.register(soft_deletable::SoftDeletableProtocol);
        reg.register(versionable::VersionableProtocol);

        let sd = reg.declaration_for(&["soft_deletable".into()]);
        assert!(sd.is_soft_delete());
        assert_eq!(sd.query_filters.len(), 1);
        assert_eq!(sd.query_filters[0].0, "deleted_at");
        assert_eq!(sd.query_filters[0].1, "IS NULL");

        let ver = reg.declaration_for(&["versionable".into()]);
        assert!(!ver.is_soft_delete());
        assert!(ver.snapshot_before_update);
        assert!(ver.revision_routes);

        let both = reg.declaration_for(&["soft_deletable".into(), "versionable".into()]);
        assert!(both.is_soft_delete());
        assert!(both.snapshot_before_update);
        assert!(both.revision_routes);
        assert_eq!(both.query_filters.len(), 1);
    }

    /// Guard test: ensures merge() covers all ProtocolDeclaration fields.
    /// When adding new fields, this test will fail if merge() is not updated.
    #[test]
    fn merge_covers_all_declaration_fields() {
        let full = ProtocolDeclaration {
            query_filters: vec![("col_a".into(), "IS NULL".into())],
            delete_strategy: DeleteStrategy::Soft {
                column: "archived_at".into(),
            },
            snapshot_before_update: true,
            revision_routes: true,
            lock_column: Some("lock_version".into()),
            default_sort: Some(("priority".into(), SortDir::Desc)),
            status_values: Some(vec!["draft".into(), "published".into()]),
            status_map: Some(vec![("draft".into(), 1), ("published".into(), 10)]),
            status_default: Some("draft".into()),
            status_mode: StatusMode::Numeric,
        };

        let mut empty = ProtocolDeclaration::default();
        empty.merge(&full);

        assert_eq!(empty.query_filters.len(), 1);
        assert_eq!(empty.query_filters[0].0, "col_a");
        assert!(matches!(empty.delete_strategy, DeleteStrategy::Soft { .. }));
        assert!(empty.snapshot_before_update);
        assert!(empty.revision_routes);
        assert_eq!(empty.lock_column.as_deref(), Some("lock_version"));
        assert_eq!(
            empty.default_sort.as_ref().map(|(c, d)| (c.as_str(), *d)),
            Some(("priority", SortDir::Desc))
        );
        assert_eq!(
            empty.status_values,
            Some(vec!["draft".into(), "published".into()])
        );
        assert_eq!(
            empty.status_map,
            Some(vec![("draft".into(), 1), ("published".into(), 10)])
        );
        assert_eq!(empty.status_default, Some("draft".into()));
        assert_eq!(empty.status_mode, StatusMode::Numeric);
    }
}