reactive_graph_std_connector/behaviour/relation/connector/
function.rs

1use std::clone::Clone;
2use std::sync::Arc;
3use std::sync::LazyLock;
4
5use log::debug;
6use log::trace;
7use reactive_graph_behaviour_model_impl::relation::RelationBehaviourFactoryCreator;
8use reactive_graph_behaviour_model_impl::relation::function::RelationBehaviourFunctions;
9use reactive_graph_behaviour_model_impl::relation::function::RelationBehaviourFunctionsStorage;
10use serde_json::Value;
11use serde_json::json;
12
13use reactive_graph_std_connector_model::*;
14
15use crate::behaviour::relation::connector::ConnectorFactory;
16
17pub type ConnectorFunction = fn(&Value) -> Value;
18
19/// This connector logs the value before propagation (log level debug)
20pub const FN_DEBUG_CONNECTOR: ConnectorFunction = |v| {
21    debug!("connector propagates value {}", v);
22    v.clone()
23};
24
25/// This is the default connector type, which simply does nothing than propagate the value
26pub const FN_DEFAULT_CONNECTOR: ConnectorFunction = |v| v.clone();
27
28/// This connector parses a string value and propagates a float value
29pub const FN_PARSE_FLOAT_CONNECTOR: ConnectorFunction = |v| {
30    if v.is_f64() {
31        return v.clone();
32    }
33    // json!(v.as_str().map(str::parse::<f64>).unwrap_or(0.0f64))
34    let str_value = v.as_str();
35    if str_value.is_none() {
36        return json!(0.0);
37    }
38    str_value
39        .unwrap()
40        .parse::<f64>()
41        .map(|int_value| json!(int_value))
42        .unwrap_or_else(|_| json!(0.0))
43};
44
45/// This connector parses a string value and propagates a int value
46pub const FN_PARSE_INT_CONNECTOR: ConnectorFunction = |v| {
47    if v.is_i64() {
48        return v.clone();
49    }
50    // json!(v.as_str().map(str::parse::<i64>).unwrap_or(0i64))
51    let str_value = v.as_str();
52    if str_value.is_none() {
53        return json!(0);
54    }
55    str_value.unwrap().parse::<i64>().map(|int_value| json!(int_value)).unwrap_or_else(|_| json!(0))
56};
57
58/// This connector converts the value of any type to string before propagation
59pub const FN_TO_STRING_CONNECTOR: ConnectorFunction = |v| json!(v.to_string());
60
61/// This connector logs the value before propagation (log level trace)
62pub const FN_TRACE_CONNECTOR: ConnectorFunction = |v| {
63    trace!("connector propagates value {}", v);
64    v.clone()
65};
66
67const FACTORY_CREATOR: RelationBehaviourFactoryCreator<ConnectorFunction> = |ty, f| Arc::new(ConnectorFactory::new(ty.clone(), f));
68
69pub static CONNECTOR_BEHAVIOURS: RelationBehaviourFunctionsStorage<ConnectorFunction> = LazyLock::new(|| {
70    RelationBehaviourFunctions::<ConnectorFunction>::with_namespace(NAMESPACE_CONNECTOR, FACTORY_CREATOR)
71        .behaviour("debug_connector", FN_DEBUG_CONNECTOR)
72        .behaviour("default_connector", FN_DEFAULT_CONNECTOR)
73        .behaviour("parse_float_connector", FN_PARSE_FLOAT_CONNECTOR)
74        .behaviour("parse_int_connector", FN_PARSE_INT_CONNECTOR)
75        .behaviour("to_string_connector", FN_TO_STRING_CONNECTOR)
76        .behaviour("trace_connector", FN_TRACE_CONNECTOR)
77        .get()
78});