devcycle_bucketing_rs/
lib.rs

1use crate::errors::DevCycleError;
2use crate::user::{BucketedUserConfig, User};
3use std::collections::HashMap;
4
5// Module declarations - now organized with mod.rs files in each folder
6pub(crate) mod bucketing;
7pub(crate) mod config;
8pub(crate) mod events;
9pub(crate) mod segmentation;
10pub mod user;
11pub(crate) mod util;
12
13// FFI bindings for C library support (exclude when building for WASM)
14#[cfg(not(feature = "wasm"))]
15pub mod ffi;
16
17// WASM bindings for WebAssembly support
18#[cfg(feature = "wasm")]
19pub mod wasm;
20
21// Internal re-exports for convenience within the crate
22pub(crate) use config::configmanager;
23pub(crate) use config::feature;
24// Re-export only what's needed for the public API
25pub use config::platform_data::*;
26pub use events::EventQueueOptions;
27pub use events::event::{DefaultReason, EvalDetails, EvaluationReason};
28pub(crate) use segmentation::filters;
29pub(crate) use segmentation::target;
30pub use user::BucketedUserConfig as BucketedConfig;
31pub use user::PopulatedUser;
32pub use user::User as DevCycleUser;
33pub(crate) use util::constants;
34pub(crate) use util::errors;
35pub use util::errors::DevCycleError as Error;
36pub(crate) use util::murmurhash;
37
38use crate::bucketing::bucketing::VariableForUserResult;
39use crate::config::ConfigBody;
40use crate::config::client_custom_data::get_client_custom_data;
41
42pub async fn init_sdk_key(
43    sdk_key: &str,
44    config_body: ConfigBody,
45    event_queue_options: EventQueueOptions,
46    client_custom_data: HashMap<String, serde_json::Value>,
47    platform_data: PlatformData,
48) -> Result<(), DevCycleError> {
49    set_platform_data(sdk_key, platform_data).await;
50    set_client_custom_data(sdk_key, client_custom_data).await?;
51    set_config(sdk_key, config_body).await?;
52    init_event_queue(sdk_key, event_queue_options).await?;
53
54    Ok(())
55}
56
57pub async fn set_config(sdk_key: &str, config_body: ConfigBody) -> Result<(), DevCycleError> {
58    Ok(configmanager::set_config(sdk_key, config_body))
59}
60
61#[cfg(feature = "protobuf")]
62pub async fn set_config_from_protobuf(
63    sdk_key: &str,
64    proto_config: protobuf::proto::ConfigBodyProto,
65) -> Result<(), DevCycleError> {
66    let config_body = protobuf::convert_proto_to_config_body(proto_config)?;
67    Ok(configmanager::set_config(sdk_key, config_body))
68}
69
70pub async fn generate_bucketed_config(
71    sdk_key: &str,
72    user: PopulatedUser,
73    client_custom_data: HashMap<String, serde_json::Value>,
74) -> Result<BucketedUserConfig, DevCycleError> {
75    bucketing::generate_bucketed_config(sdk_key.to_string(), user, client_custom_data).await
76}
77
78pub async fn generate_bucketed_config_from_user(
79    sdk_key: &str,
80    user: User,
81) -> Result<BucketedUserConfig, DevCycleError> {
82    let populated_user = user.get_populated_user(sdk_key);
83    bucketing::generate_bucketed_config(
84        sdk_key.to_string(),
85        populated_user,
86        get_client_custom_data(sdk_key.to_string()),
87    )
88    .await
89}
90
91pub async fn variable_for_user(
92    sdk_key: &str,
93    user: PopulatedUser,
94    variable_key: &str,
95    variable_type: &str,
96) -> Result<VariableForUserResult, DevCycleError> {
97    bucketing::variable_for_user(
98        sdk_key,
99        user,
100        variable_key,
101        variable_type,
102        get_client_custom_data(sdk_key.to_string()),
103    )
104    .await
105}
106
107pub async fn init_event_queue(
108    sdk_key: &str,
109    event_queue_options: EventQueueOptions,
110) -> Result<(), DevCycleError> {
111    let eq = events::event_queue::EventQueue::new(sdk_key.to_string(), event_queue_options);
112    match eq {
113        Ok(queue) => {
114            events::event_queue_manager::set_event_queue(sdk_key, queue);
115            Ok(())
116        }
117        Err(e) => Err(e),
118    }
119}
120
121pub async fn set_client_custom_data(
122    sdk_key: &str,
123    client_custom_data: HashMap<String, serde_json::Value>,
124) -> Result<(), DevCycleError> {
125    // The insert operation always succeeds, it returns the old value if one existed
126    config::client_custom_data::set_client_custom_data(sdk_key.to_string(), client_custom_data);
127    Ok(())
128}
129
130pub async fn set_platform_data(sdk_key: &str, platform_data: PlatformData) {
131    config::platform_data::set_platform_data(sdk_key.to_string(), platform_data);
132}