reactive_graph_std_string/behaviour/entity/string_number_operation/
mod.rs1use reactive_graph_behaviour_model_api::behaviour_validator;
2use reactive_graph_behaviour_model_api::prelude::*;
3use reactive_graph_behaviour_model_impl::entity_behaviour;
4use reactive_graph_graph::prelude::*;
5use reactive_graph_reactive_model_impl::ReactiveEntity;
6use serde_json::Value;
7use serde_json::json;
8use uuid::Uuid;
9
10pub use function::STRING_NUMBER_OPERATIONS;
11pub use function::StringNumberFunction;
12
13use reactive_graph_std_result_model::ResultNumberU64Properties::RESULT;
14use reactive_graph_std_string_model::StringNumberOperationProperties::LHS;
15
16pub mod function;
17
18entity_behaviour!(
19 StringNumberOperation,
20 StringNumberOperationFactory,
21 StringNumberOperationFsm,
22 StringNumberOperationBehaviourTransitions,
23 StringNumberOperationValidator,
24 f,
25 StringNumberFunction
26);
27
28behaviour_validator!(StringNumberOperationValidator, Uuid, ReactiveEntity, LHS.as_ref(), RESULT.as_ref());
29
30impl BehaviourInit<Uuid, ReactiveEntity> for StringNumberOperationBehaviourTransitions {
31 fn init(&self) -> Result<(), BehaviourInitializationFailed> {
32 let lhs = self
33 .reactive_instance
34 .get(LHS)
35 .and_then(|lhs| lhs.as_str().map(|lhs| lhs.to_string()))
36 .ok_or(BehaviourInitializationFailed {})?;
37 let f = self.f;
38 self.reactive_instance.set(RESULT, json!(f(lhs)));
39 Ok(())
40 }
41}
42
43impl BehaviourConnect<Uuid, ReactiveEntity> for StringNumberOperationBehaviourTransitions {
44 fn connect(&self) -> Result<(), BehaviourConnectFailed> {
45 let reactive_instance = self.property_observers.reactive_instance.clone();
46 let f = self.f;
47 self.property_observers.observe_with_handle(LHS.as_ref(), move |lhs: &Value| {
48 if let Some(lhs) = lhs.as_str().map(|lhs| lhs.to_string()) {
49 reactive_instance.set(RESULT, json!(f(lhs)));
50 }
51 });
52 Ok(())
53 }
54}
55
56impl BehaviourShutdown<Uuid, ReactiveEntity> for StringNumberOperationBehaviourTransitions {}
57impl BehaviourTransitions<Uuid, ReactiveEntity> for StringNumberOperationBehaviourTransitions {}