reactive_graph_std_string/behaviour/entity/string_number_operation/
function.rs

1use std::sync::Arc;
2use std::sync::LazyLock;
3
4use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFactoryCreator;
5use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFunctions;
6use reactive_graph_behaviour_model_impl::entity::EntityBehaviourFunctionsStorage;
7use voca_rs::count;
8
9use crate::behaviour::entity::string_number_operation::StringNumberOperationFactory;
10use reactive_graph_std_string_model::NAMESPACE_STRING;
11
12pub type StringNumberFunction = fn(String) -> usize;
13
14pub const FN_STRING_LENGTH: StringNumberFunction = |lhs: String| lhs.len();
15pub const FN_CHAR_COUNT: StringNumberFunction = |lhs: String| count::count(lhs.as_str());
16pub const FN_CHAR_COUNT_GRAPHEMES: StringNumberFunction = |lhs: String| count::count_graphemes(lhs.as_str());
17
18const FACTORY_CREATOR: EntityBehaviourFactoryCreator<StringNumberFunction> = |ty, f| Arc::new(StringNumberOperationFactory::new(ty.clone(), f));
19
20pub static STRING_NUMBER_OPERATIONS: EntityBehaviourFunctionsStorage<StringNumberFunction> = LazyLock::new(|| {
21    EntityBehaviourFunctions::<StringNumberFunction>::with_namespace(NAMESPACE_STRING, FACTORY_CREATOR)
22        .behaviour("string_length", FN_STRING_LENGTH)
23        .behaviour("char_count", FN_CHAR_COUNT)
24        .behaviour("char_count_graphemes", FN_CHAR_COUNT_GRAPHEMES)
25        .get()
26});