this/core/module.rs
1//! Module system for This-RS
2//!
3//! Defines traits for microservice modules
4
5use crate::config::LinksConfig;
6use crate::server::entity_registry::EntityRegistry;
7use anyhow::Result;
8use async_trait::async_trait;
9use std::sync::Arc;
10use uuid::Uuid;
11
12/// Trait for fetching entities dynamically
13///
14/// This allows the link system to enrich links with full entity data
15/// without knowing the concrete entity types at compile time.
16#[async_trait]
17pub trait EntityFetcher: Send + Sync {
18 /// Fetch an entity by ID and return it as JSON
19 ///
20 /// # Arguments
21 /// * `entity_id` - The unique ID of the entity to fetch
22 ///
23 /// # Returns
24 /// The entity serialized as JSON, or an error if not found
25 async fn fetch_as_json(&self, entity_id: &Uuid) -> Result<serde_json::Value>;
26}
27
28/// Trait for creating entities dynamically
29///
30/// This allows the link system to create new entities with automatic linking
31/// without knowing the concrete entity types at compile time.
32#[async_trait]
33pub trait EntityCreator: Send + Sync {
34 /// Create a new entity from JSON data
35 ///
36 /// # Arguments
37 /// * `entity_data` - The entity data as JSON
38 ///
39 /// # Returns
40 /// The created entity serialized as JSON (with generated ID, timestamps, etc.)
41 async fn create_from_json(&self, entity_data: serde_json::Value) -> Result<serde_json::Value>;
42}
43
44/// Trait for a microservice module
45pub trait Module: Send + Sync {
46 /// Unique module name
47 fn name(&self) -> &str;
48
49 /// Module version
50 fn version(&self) -> &str {
51 "1.0.0"
52 }
53
54 /// List of entity types managed by this module
55 fn entity_types(&self) -> Vec<&str>;
56
57 /// Load links configuration
58 fn links_config(&self) -> Result<LinksConfig>;
59
60 /// Register entities with the entity registry
61 ///
62 /// This method should register all entity descriptors for the module.
63 /// Each entity descriptor provides the CRUD routes for that entity.
64 fn register_entities(&self, registry: &mut EntityRegistry);
65
66 /// Get an entity fetcher for a specific entity type
67 ///
68 /// This allows the framework to dynamically load entities when enriching links.
69 ///
70 /// # Arguments
71 /// * `entity_type` - The type of entity (e.g., "order", "invoice")
72 ///
73 /// # Returns
74 /// An `EntityFetcher` implementation, or `None` if the entity type is not managed by this module
75 fn get_entity_fetcher(&self, entity_type: &str) -> Option<Arc<dyn EntityFetcher>>;
76
77 /// Get an entity creator for a specific entity type
78 ///
79 /// This allows the framework to create new entities dynamically when creating linked entities.
80 ///
81 /// # Arguments
82 /// * `entity_type` - The type of entity (e.g., "order", "invoice")
83 ///
84 /// # Returns
85 /// An `EntityCreator` implementation, or `None` if the entity type is not managed by this module
86 fn get_entity_creator(&self, entity_type: &str) -> Option<Arc<dyn EntityCreator>>;
87}