reovim_kernel/api/module/
state.rs1use std::fmt;
4
5use {
6 super::{Module, ModuleId},
7 crate::api::version::Version,
8};
9
10#[derive(Debug, Clone, PartialEq, Eq, Default)]
19pub enum ModuleState {
20 #[default]
22 Loaded,
23 Initializing,
25 Running,
27 Unloading,
29 Failed(String),
31}
32
33impl ModuleState {
34 #[must_use]
36 pub const fn can_transition_to(&self, next: &Self) -> bool {
37 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#[derive(Debug, Clone)]
67pub struct ModuleInfo {
68 pub id: ModuleId,
70 pub name: &'static str,
72 pub version: Version,
74 pub api_version: Version,
76 pub description: &'static str,
78 pub authors: &'static [&'static str],
80 pub license: &'static str,
82}
83
84impl ModuleInfo {
85 #[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}