1use crate::audit::AuditLog;
14use crate::bus::CommandBus;
15use crate::command::CommandRegistry;
16use crate::context::RuntimeContext;
17use crate::decision::{DecisionEngine, DecisionOutcome, DecisionRegistry};
18use crate::envelope::CommandEnvelope;
19use crate::error::RuntimeResult;
20use crate::event::EventRegistry;
21use crate::event_bus::EventBus;
22use crate::handler::CommandResult;
23use crate::identity::RuntimeIdentity;
24use crate::lifecycle::RuntimeLifecycle;
25use crate::state::StateRegistry;
26use appcore_contracts::ApplicationManifestV1;
27
28#[derive(Debug)]
30pub struct RuntimeInstance {
31 pub(crate) application_manifest: ApplicationManifestV1,
32 pub(crate) identity: RuntimeIdentity,
33 pub(crate) command_registry: CommandRegistry,
34 pub(crate) event_registry: EventRegistry,
35 pub(crate) state_registry: StateRegistry,
36 pub(crate) decision_registry: DecisionRegistry,
37 pub(crate) decision_engine: DecisionEngine,
38 pub(crate) command_bus: CommandBus,
39 pub(crate) event_bus: EventBus,
40 pub(crate) audit_log: AuditLog,
41 pub(crate) lifecycle: RuntimeLifecycle,
42}
43
44impl RuntimeInstance {
45 pub fn application_manifest(&self) -> &ApplicationManifestV1 {
47 &self.application_manifest
48 }
49
50 pub fn identity(&self) -> &RuntimeIdentity {
52 &self.identity
53 }
54
55 pub fn commands(&self) -> &CommandRegistry {
57 &self.command_registry
58 }
59
60 pub fn events(&self) -> &EventRegistry {
62 &self.event_registry
63 }
64
65 pub fn states(&self) -> &StateRegistry {
67 &self.state_registry
68 }
69
70 pub fn decisions(&self) -> &DecisionRegistry {
72 &self.decision_registry
73 }
74
75 pub fn command_bus(&self) -> &CommandBus {
77 &self.command_bus
78 }
79
80 pub fn lifecycle(&self) -> &RuntimeLifecycle {
82 &self.lifecycle
83 }
84
85 pub fn event_bus(&self) -> &EventBus {
87 &self.event_bus
88 }
89
90 pub fn audit_log(&self) -> &AuditLog {
92 &self.audit_log
93 }
94
95 pub fn dispatch_command(
97 &self,
98 command: &CommandEnvelope,
99 context: &dyn RuntimeContext,
100 ) -> RuntimeResult<CommandResult> {
101 match self.decision_engine.evaluate(command, context)? {
102 DecisionOutcome::Allow => self.command_bus.dispatch(command, context),
103 DecisionOutcome::Deny(message) => Ok(CommandResult::rejected(message)),
104 DecisionOutcome::Defer(message) => Ok(CommandResult::rejected(message)),
105 }
106 }
107
108 pub fn ensure_compatible(&self, other: &RuntimeIdentity) -> RuntimeResult<()> {
110 self.identity().ensure_compatible(other)
111 }
112}