statsig_rust/
data_store_interface.rs

1use std::fmt::Display;
2
3use async_trait::async_trait;
4
5use crate::StatsigErr;
6
7pub enum RequestPath {
8    RulesetsV2,
9    RulesetsV1,
10    IDListsV1,
11    IDList,
12}
13
14impl Display for RequestPath {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        let value = match self {
17            RequestPath::IDListsV1 => "/v1/get_id_lists",
18            RequestPath::IDList => "id_list",
19            RequestPath::RulesetsV2 => "/v2/download_config_specs",
20            RequestPath::RulesetsV1 => "/v1/download_config_specs",
21        };
22        write!(f, "{value}")
23    }
24}
25
26pub enum CompressFormat {
27    PlainText,
28    Gzip,
29}
30
31impl Display for CompressFormat {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        let value = match self {
34            CompressFormat::PlainText => "plain_text",
35            CompressFormat::Gzip => "gzip",
36        };
37        write!(f, "{value}")
38    }
39}
40
41pub struct DataStoreResponse {
42    pub result: Option<String>,
43    pub time: Option<u64>,
44}
45
46#[async_trait]
47pub trait DataStoreTrait: Send + Sync {
48    async fn initialize(&self) -> Result<(), StatsigErr>;
49    async fn shutdown(&self) -> Result<(), StatsigErr>;
50    async fn get(&self, key: &str) -> Result<DataStoreResponse, StatsigErr>;
51    async fn set(&self, key: &str, value: &str, time: Option<u64>) -> Result<(), StatsigErr>;
52    async fn support_polling_updates_for(&self, path: RequestPath) -> bool;
53}
54
55#[must_use]
56pub fn get_data_adapter_dcs_key(hashed_key: &str) -> String {
57    get_data_adapter_key(
58        RequestPath::RulesetsV2,
59        CompressFormat::PlainText,
60        hashed_key,
61    )
62}
63
64#[must_use]
65pub fn get_data_adapter_key(
66    path: RequestPath,
67    compress: CompressFormat,
68    hashed_key: &str,
69) -> String {
70    format!("statsig|{path}|{compress}|{hashed_key}")
71}