statsig_rust/
data_store_interface.rs

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