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    /// * `tenant_id` - The tenant ID for isolation
22    /// * `entity_id` - The unique ID of the entity to fetch
23    ///
24    /// # Returns
25    /// The entity serialized as JSON, or an error if not found
26    async fn fetch_as_json(&self, tenant_id: &Uuid, entity_id: &Uuid) -> Result<serde_json::Value>;
27}
28
29/// Trait for a microservice module
30pub trait Module: Send + Sync {
31    /// Unique module name
32    fn name(&self) -> &str;
33
34    /// Module version
35    fn version(&self) -> &str {
36        "1.0.0"
37    }
38
39    /// List of entity types managed by this module
40    fn entity_types(&self) -> Vec<&str>;
41
42    /// Load links configuration
43    fn links_config(&self) -> Result<LinksConfig>;
44
45    /// Register entities with the entity registry
46    ///
47    /// This method should register all entity descriptors for the module.
48    /// Each entity descriptor provides the CRUD routes for that entity.
49    fn register_entities(&self, registry: &mut EntityRegistry);
50
51    /// Get an entity fetcher for a specific entity type
52    ///
53    /// This allows the framework to dynamically load entities when enriching links.
54    ///
55    /// # Arguments
56    /// * `entity_type` - The type of entity (e.g., "order", "invoice")
57    ///
58    /// # Returns
59    /// An `EntityFetcher` implementation, or `None` if the entity type is not managed by this module
60    fn get_entity_fetcher(&self, entity_type: &str) -> Option<Arc<dyn EntityFetcher>>;
61}