reactive_graph_std_string/behaviour/entity/string_bool_operation/
mod.rs

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