statsig_rust/id_lists_adapter/
id_lists_adapter_trait.rs

1use crate::{StatsigErr, StatsigRuntime};
2use async_trait::async_trait;
3use std::sync::Arc;
4use std::time::Duration;
5use std::{collections::HashMap, fmt};
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Serialize, Deserialize, Debug, Clone)]
10#[serde(rename_all = "camelCase")]
11pub struct IdListMetadata {
12    pub name: String,
13    pub url: String,
14
15    #[serde(rename = "fileID")]
16    pub file_id: Option<String>,
17
18    pub size: u64,
19    pub creation_time: i64,
20}
21
22pub struct IdListUpdate {
23    pub raw_changeset: Option<String>,
24    pub new_metadata: IdListMetadata,
25}
26
27#[async_trait]
28pub trait IdListsAdapter: Send + Sync {
29    /// Called during Statsig::initialize. Mostly to attach the listener.
30    /// Scheduling background threads should be done in the IdListsAdapter::schedule_background_sync function.
31    ///
32    /// # Arguments
33    ///
34    /// * `statsig_runtime` - The Statsig runtime instance.
35    /// * `listener` - The listener to push updates to.
36    async fn start(
37        self: Arc<Self>,
38        statsig_runtime: &Arc<StatsigRuntime>,
39        listener: Arc<dyn IdListsUpdateListener + Send + Sync>,
40    ) -> Result<(), StatsigErr>;
41
42    /// Called during Statsig::shutdown or Statsig::shutdown_with_timeout.
43    /// This function gives some grace period to the adapter to finish its work.
44    ///
45    /// # Arguments
46    ///
47    /// * `timeout` - The timeout for the shutdown.
48    async fn shutdown(&self, timeout: Duration) -> Result<(), StatsigErr>;
49
50    /// Called during Statsig::initialize.
51    /// This function is used to schedule the background sync thread and is called just after IdListsAdapter::start
52    ///
53    /// # Arguments
54    ///
55    /// * `statsig_runtime` - The Statsig runtime instance.
56    async fn schedule_background_sync(
57        self: Arc<Self>,
58        statsig_runtime: &Arc<StatsigRuntime>,
59    ) -> Result<(), StatsigErr>;
60
61    /// Returns the type name of the adapter. Used for logging and error messages.
62    fn get_type_name(&self) -> String;
63}
64
65pub trait IdListsUpdateListener: Send + Sync {
66    fn get_current_id_list_metadata(&self) -> HashMap<String, IdListMetadata>;
67
68    fn did_receive_id_list_updates(&self, updates: HashMap<String, IdListUpdate>);
69}
70
71impl fmt::Debug for dyn IdListsAdapter {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        write!(f, "{}", self.get_type_name())
74    }
75}