Skip to main content

appcore_core/
command.rs

1// =============================================================================
2//        #######
3//     ###       ###     F: command.rs
4//    ##   ## ##   ##    P: AppCore-Runtime
5//         ## ##
6//                       C: 2026/05/29 20:47:35 by dnettoRaw
7//    ##   ## ##   ##    U: 2026/06/04 11:51:29 by dnettoRaw
8//      ###########      S: 0.6.0
9// =============================================================================
10
11//! Command contract and ordered, duplicate-safe command registry.
12//! Command payload transport uses `CommandEnvelope`.
13//! Handler contract lives in `handler.rs`.
14
15use crate::error::RuntimeResult;
16use crate::ids::CommandName;
17use crate::registry::NameRegistry;
18
19/// Minimal runtime command contract.
20pub trait RuntimeCommand {
21    /// Returns the stable command name used for registration and dispatch.
22    fn name(&self) -> &CommandName;
23}
24
25/// Ordered registry of declared command names.
26#[derive(Debug, Default)]
27pub struct CommandRegistry {
28    names: NameRegistry<CommandName>,
29}
30
31impl CommandRegistry {
32    /// Creates an empty command registry.
33    pub fn new() -> Self {
34        Self::default()
35    }
36
37    /// Registers a command name, rejecting duplicates.
38    pub fn register(&mut self, name: CommandName) -> RuntimeResult<()> {
39        self.names.register(name, "command")
40    }
41
42    /// Reports whether a command name is registered.
43    pub fn contains(&self, name: &CommandName) -> bool {
44        self.names.contains(name)
45    }
46
47    /// Returns the number of registered command names.
48    pub fn len(&self) -> usize {
49        self.names.len()
50    }
51
52    /// Reports whether no command names are registered.
53    pub fn is_empty(&self) -> bool {
54        self.names.is_empty()
55    }
56
57    /// Returns command names in registration order.
58    pub fn list(&self) -> &[CommandName] {
59        self.names.list()
60    }
61}
62
63#[cfg(test)]
64#[path = "command_tests.rs"]
65mod tests;