Skip to main content

reovim_kernel/api/module/
state.rs

1//! Module state and info types.
2
3use std::fmt;
4
5use {
6    super::{Module, ModuleId},
7    crate::api::version::Version,
8};
9
10/// Module lifecycle state.
11///
12/// State machine:
13/// ```text
14/// Loaded -> Initializing -> Running -> Unloading -> (removed)
15///    |           |            |           |
16///    +---------->+--->--------+--->-------+---> Failed
17/// ```
18#[derive(Debug, Clone, PartialEq, Eq, Default)]
19pub enum ModuleState {
20    /// Module is loaded but not initialized.
21    #[default]
22    Loaded,
23    /// Module is currently initializing.
24    Initializing,
25    /// Module is running normally.
26    Running,
27    /// Module is being unloaded.
28    Unloading,
29    /// Module failed (with error message).
30    Failed(String),
31}
32
33impl ModuleState {
34    /// Check if transition to another state is valid.
35    #[must_use]
36    pub const fn can_transition_to(&self, next: &Self) -> bool {
37        // Any state can transition to Failed
38        if matches!(next, Self::Failed(_)) {
39            return true;
40        }
41
42        matches!(
43            (self, next),
44            (Self::Loaded, Self::Initializing)
45                | (Self::Initializing, Self::Running)
46                | (Self::Running, Self::Unloading)
47        )
48    }
49}
50
51impl fmt::Display for ModuleState {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            Self::Loaded => write!(f, "loaded"),
55            Self::Initializing => write!(f, "initializing"),
56            Self::Running => write!(f, "running"),
57            Self::Unloading => write!(f, "unloading"),
58            Self::Failed(msg) => write!(f, "failed: {msg}"),
59        }
60    }
61}
62
63/// Static module metadata.
64///
65/// Used for module discovery and display.
66#[derive(Debug, Clone)]
67pub struct ModuleInfo {
68    /// Unique identifier.
69    pub id: ModuleId,
70    /// Human-readable name.
71    pub name: &'static str,
72    /// Version.
73    pub version: Version,
74    /// Required API version.
75    pub api_version: Version,
76    /// Description.
77    pub description: &'static str,
78    /// Author(s).
79    pub authors: &'static [&'static str],
80    /// License.
81    pub license: &'static str,
82}
83
84impl ModuleInfo {
85    /// Create module info from a Module instance.
86    #[must_use]
87    pub fn from_module<M: Module>(module: &M) -> Self {
88        Self {
89            id: module.id(),
90            name: module.name(),
91            version: module.version(),
92            api_version: module.api_version(),
93            description: "",
94            authors: &[],
95            license: "",
96        }
97    }
98}