reactive_graph_std_random/behaviour/entity/
random_string.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 reactive_graph_runtime_model::ActionProperties::TRIGGER;
7use serde_json::Value;
8use serde_json::json;
9use uuid::Uuid;
10
11use reactive_graph_std_random_model::RandomStringProperties::LENGTH;
12use reactive_graph_std_result_model::ResultStringProperties::RESULT;
13
14pub const CHARSET: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
15
16entity_behaviour!(RandomString, RandomStringFactory, RandomStringFsm, RandomStringBehaviourTransitions, RandomStringValidator);
17
18behaviour_validator!(RandomStringValidator, Uuid, ReactiveEntity, TRIGGER.as_ref(), RESULT.as_ref());
19
20impl BehaviourInit<Uuid, ReactiveEntity> for RandomStringBehaviourTransitions {
21 fn init(&self) -> Result<(), BehaviourInitializationFailed> {
22 if self.reactive_instance.as_bool(TRIGGER).unwrap_or(false) {
23 if let Some(length) = self.reactive_instance.as_u64(LENGTH) {
24 self.reactive_instance.set(RESULT, random(length));
25 }
26 }
27 Ok(())
28 }
29}
30
31impl BehaviourConnect<Uuid, ReactiveEntity> for RandomStringBehaviourTransitions {
32 fn connect(&self) -> Result<(), BehaviourConnectFailed> {
33 let reactive_instance = self.reactive_instance.clone();
34 self.property_observers.observe_with_handle(TRIGGER.as_ref(), move |trigger: &Value| {
35 if !trigger.as_bool().unwrap_or(false) {
36 return;
37 }
38 if let Some(length) = reactive_instance.as_u64(LENGTH) {
39 reactive_instance.set(RESULT, random(length));
40 }
41 });
42 Ok(())
43 }
44}
45
46impl BehaviourShutdown<Uuid, ReactiveEntity> for RandomStringBehaviourTransitions {}
47impl BehaviourTransitions<Uuid, ReactiveEntity> for RandomStringBehaviourTransitions {}
48
49fn random(length: u64) -> Value {
50 let length = length as usize;
51 json!(random_string::generate(length, CHARSET))
52}