reactive_graph_command_model/
builder.rs1use std::ops::Deref;
2
3use serde_json::Value;
4use serde_json::json;
5use typed_builder::TypedBuilder;
6use uuid::Uuid;
7
8use crate::entity::Command;
9use crate::entity::CommandArgs;
10use crate::reactive_graph::command::command::COMMAND;
11use crate::reactive_graph::command::command::CommandProperties;
12use reactive_graph_graph::ComponentTypeIds;
13use reactive_graph_graph::EntityTypeId;
14use reactive_graph_graph::NamespacedTypeGetter;
15use reactive_graph_graph::PropertyInstanceSetter;
16use reactive_graph_graph::PropertyInstances;
17use reactive_graph_model_core::reactive_graph::core::action::ACTION;
18use reactive_graph_model_core::reactive_graph::core::action::ActionProperties;
19use reactive_graph_model_core::reactive_graph::core::labeled::LABELED;
20use reactive_graph_model_core::reactive_graph::core::labeled::LabeledProperties;
21use reactive_graph_reactive_model_impl::ReactiveEntity;
22use reactive_graph_reactive_model_impl::ReactiveProperties;
23
24pub type CommandExecutor = dyn FnMut(&ReactiveEntity) -> Value + 'static + Send;
25
26#[derive(TypedBuilder)]
27#[builder(
28 build_method(vis="pub", into=Command),
29 builder_method(vis="pub"),
30 builder_type(vis="pub", name=CommandDefinitionBuilder),
31)]
32pub struct CommandDefinition {
33 #[builder(setter(into))]
34 ty: EntityTypeId,
35 #[builder(default=Uuid::new_v4(), setter(into))]
36 id: Uuid,
37 #[builder(default, setter(strip_option, into))]
38 namespace: Option<String>,
39 #[builder(default, setter(strip_option, into))]
40 name: Option<String>,
41 #[builder(default, setter(into))]
42 description: String,
43 #[builder(default, setter(into))]
44 help: String,
45 #[builder(default, setter(into))]
46 arguments: CommandArgs,
47 executor: Box<CommandExecutor>,
48}
49
50impl From<CommandDefinition> for Command {
51 fn from(definition: CommandDefinition) -> Self {
52 let handle_id = Uuid::new_v4().as_u128();
53
54 let namespace = definition.namespace.unwrap_or_else(|| definition.ty.path().to_string());
55 let name = definition.name.unwrap_or_else(|| definition.ty.type_name().to_string());
56
57 let label = format!("/io/reactive-graph/commands/{namespace}/{name}");
58
59 let components = ComponentTypeIds::new()
60 .component(LABELED.deref())
61 .component(ACTION.deref())
62 .component(COMMAND.deref());
63
64 let properties = PropertyInstances::new()
65 .property(LabeledProperties::LABEL.as_ref(), json!(label))
66 .property(ActionProperties::TRIGGER.as_ref(), json!(false))
67 .property(CommandProperties::NAMESPACE.as_ref(), json!(namespace))
68 .property(CommandProperties::COMMAND.as_ref(), json!(name))
69 .property(CommandProperties::ARGS.as_ref(), definition.arguments.to_value())
70 .property(CommandProperties::HELP.as_ref(), json!(definition.help))
71 .property(CommandProperties::CMD_RESULT.as_ref(), json!(0));
72
73 for arg in definition.arguments.to_vec() {
74 if !properties.contains_key(&arg.name) {
75 properties.insert(arg.name.clone(), json!(0));
76 }
77 }
78
79 let reactive_entity = ReactiveEntity::builder()
80 .ty(definition.ty)
81 .id(definition.id)
82 .description(definition.description)
83 .components(components)
84 .properties(ReactiveProperties::new_with_id_from_properties(definition.id, properties))
85 .build();
86 let reactive_entity_inner = reactive_entity.clone();
87 let mut executor = definition.executor;
88 if let Some(property_instance) = reactive_entity.properties.get(ActionProperties::TRIGGER.as_ref()) {
89 property_instance.stream.read().unwrap().observe_with_handle(
90 move |trigger| {
91 if trigger.as_bool().unwrap_or_default() {
92 reactive_entity_inner.set(CommandProperties::CMD_RESULT.as_ref(), executor(&reactive_entity_inner));
93 }
94 },
95 handle_id,
96 );
97 };
98 Command::new_unchecked(reactive_entity)
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use crate::Command;
105 use crate::CommandArgs;
106 use reactive_graph_graph::EntityTypeId;
107 use reactive_graph_reactive_model_impl::ReactiveEntity;
108 use serde_json::json;
109 use std::str::FromStr;
110
111 #[test]
112 fn command_builder_test() {
113 let args = CommandArgs::new();
114 let executor = Box::new(move |_: &ReactiveEntity| json!("abc"));
116 let command = Command::builder()
117 .ty(EntityTypeId::from_str("reactive_graph::command::NumCommands").unwrap())
118 .description("The number of commands")
119 .help("Number of commands")
120 .arguments(args)
121 .executor(executor)
122 .build();
123
124 assert_eq!(command.ty(), EntityTypeId::from_str("reactive_graph::command::NumCommands").unwrap());
125 }
126}