Skip to main content

trait_kit/core/
module.rs

1// Copyright © 2026 Kirky.X. All rights reserved.
2
3//! Module trait — the standard interface for all modules.
4
5use std::error::Error;
6
7use super::builder::ModuleBuilder;
8
9/// The standard interface that all modules must implement.
10///
11/// A module declares:
12/// - `NAME`: A diagnostic name for the module.
13/// - `Config`: The configuration type required for initialization.
14/// - `Requirements`: The dependencies required for initialization.
15/// - `Capability`: The capability facade exposed to consumers.
16/// - `Error`: The initialization error type.
17/// - `Builder`: The standard builder type that implements `ModuleBuilder<Self>`.
18///
19/// # Associated Types
20///
21/// - `Config`: Use `NoConfig` if the module requires no configuration.
22/// - `Requirements`: Use `NoRequirements` if the module has no dependencies.
23/// - `Capability`: Typically `Arc<dyn SomeTrait + Send + Sync>`.
24/// - `Error`: Must satisfy `std::error::Error + Send + Sync + 'static`.
25/// - `Builder`: Must implement `ModuleBuilder<Self>`.
26///
27/// # Example
28///
29/// ```
30/// use trait_kit::prelude::*;
31/// use std::sync::Arc;
32///
33/// struct MyModule;
34/// impl Module for MyModule {
35///     const NAME: &'static str = "my_module";
36///     type Config = NoConfig;
37///     type Requirements = NoRequirements;
38///     type Capability = Arc<i32>;
39///     type Error = std::convert::Infallible;
40///     type Builder = MyBuilder;
41/// }
42///
43/// struct MyBuilder;
44/// impl ModuleBuilder<MyModule> for MyBuilder {
45///     fn build(self) -> Result<Arc<i32>, std::convert::Infallible> {
46///         Ok(Arc::new(42))
47///     }
48/// }
49/// ```
50pub trait Module: Sized {
51    /// The diagnostic name of this module.
52    const NAME: &'static str;
53
54    /// The configuration type required for initialization.
55    /// Use `NoConfig` if no configuration is needed.
56    type Config;
57
58    /// The dependencies required for initialization.
59    /// Use `NoRequirements` if no dependencies are needed.
60    type Requirements;
61
62    /// The capability facade exposed to consumers.
63    /// Typically `Arc<dyn SomeTrait + Send + Sync>`.
64    type Capability;
65
66    /// The initialization error type.
67    /// Must satisfy `std::error::Error + Send + Sync + 'static`.
68    type Error: Error + Send + Sync + 'static;
69
70    /// The standard builder for this module.
71    /// Must implement `ModuleBuilder<Self>` (enforced at usage points).
72    type Builder: ModuleBuilder<Self>;
73}