reactive_graph_command_impl/
command_manager_impl.rs1use async_trait::async_trait;
2use serde_json::json;
3use std::ops::Deref;
4use std::sync::Arc;
5
6use reactive_graph_command_api::CommandManager;
7use reactive_graph_command_api::CommandRegistrationError;
8use reactive_graph_command_model::entity::Command;
9use reactive_graph_command_model::error::NoSuchCommand;
10use reactive_graph_command_model::reactive_graph::command::command::COMMAND;
11use reactive_graph_command_model::reactive_graph::command::command::CommandProperties;
12use reactive_graph_command_model::reactive_graph::command::num_commands::NUM_COMMANDS;
13use reactive_graph_graph::EntityType;
14use reactive_graph_graph::PropertyInstanceGetter;
15use reactive_graph_lifecycle::Lifecycle;
16use reactive_graph_reactive_model_impl::ReactiveEntity;
17use reactive_graph_reactive_service_api::ReactiveEntityManager;
18use reactive_graph_type_system_api::EntityTypeManager;
19use springtime_di::Component;
20use springtime_di::component_alias;
21
22#[derive(Component)]
23pub struct CommandManagerImpl {
24 entity_type_manager: Arc<dyn EntityTypeManager + Send + Sync>,
25 reactive_entity_manager: Arc<dyn ReactiveEntityManager + Send + Sync>,
26}
27
28#[async_trait]
29#[component_alias]
30impl CommandManager for CommandManagerImpl {
31 fn get_command(&self, name: &str) -> Result<Command, NoSuchCommand> {
32 let name = name.into();
33 match self.reactive_entity_manager.get_by_component(&COMMAND).iter().find(|e| {
34 e.as_string(CommandProperties::COMMAND.as_ref())
35 .is_some_and(|command_name| command_name == name)
36 }) {
37 Some(e) => Command::try_from(e.clone()).map_err(NoSuchCommand::NotACommand),
38 None => Err(NoSuchCommand::CommandNotFound(name)),
39 }
40 }
41
42 fn get_commands(&self) -> Vec<Command> {
43 self.reactive_entity_manager
44 .get_by_component(&COMMAND)
45 .iter()
46 .filter_map(|e| Command::try_from(e.clone()).ok())
47 .collect()
48 }
49
50 fn register_command(&self, command: Command) -> Result<(), CommandRegistrationError> {
51 let ty = command.ty();
52 if !self.entity_type_manager.has(&ty) {
53 return Err(CommandRegistrationError::EntityTypeNotFound(ty));
54 }
55 let _ = self
56 .reactive_entity_manager
57 .register_reactive_instance(command.get_instance())
58 .map_err(CommandRegistrationError::ReactiveEntityRegistrationError);
59 Ok(())
60 }
61
62 fn register_singleton_command(&self, command: Command, entity_type: EntityType) -> Result<(), CommandRegistrationError> {
63 let _ = self
64 .entity_type_manager
65 .register(entity_type)
66 .map_err(CommandRegistrationError::EntityTypeRegistrationError)?;
67 let _ = self
68 .reactive_entity_manager
69 .register_reactive_instance(command.get_instance())
70 .map_err(CommandRegistrationError::ReactiveEntityRegistrationError);
71 Ok(())
72 }
73}
74
75#[async_trait]
76impl Lifecycle for CommandManagerImpl {
77 async fn init(&self) {
78 let reactive_entity_manager = self.reactive_entity_manager.clone();
79
80 let executor = Box::new(move |_: &ReactiveEntity| json!(reactive_entity_manager.get_by_component(&COMMAND).len()));
81 let command = Command::builder()
82 .ty(NUM_COMMANDS.deref())
83 .help("Number of commands")
84 .description("Number of commands")
85 .executor(executor)
86 .build();
87 let entity_type: EntityType = command.get_entity_type();
88 let _ = self.register_singleton_command(command, entity_type);
89
90 }
100}