Skip to main content

absmartly_sdk/
models.rs

1use serde::{Deserialize, Deserializer, Serialize};
2use std::collections::HashMap;
3
4fn deserialize_null_string<'de, D>(deserializer: D) -> Result<String, D::Error>
5where
6    D: Deserializer<'de>,
7{
8    let opt = Option::<String>::deserialize(deserializer)?;
9    Ok(opt.unwrap_or_default())
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize, Default)]
13#[serde(rename_all = "camelCase")]
14pub struct ContextData {
15    #[serde(default)]
16    pub experiments: Vec<ExperimentData>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase")]
21pub struct ExperimentData {
22    pub id: i64,
23    pub name: String,
24    #[serde(default)]
25    pub unit_type: Option<String>,
26    #[serde(default)]
27    pub iteration: i64,
28    #[serde(default)]
29    pub full_on_variant: i64,
30    #[serde(default)]
31    pub traffic_split: Vec<f64>,
32    #[serde(default)]
33    pub traffic_seed_hi: u32,
34    #[serde(default)]
35    pub traffic_seed_lo: u32,
36    #[serde(default, deserialize_with = "deserialize_null_string")]
37    pub audience: String,
38    #[serde(default)]
39    pub audience_strict: bool,
40    #[serde(default)]
41    pub split: Vec<f64>,
42    #[serde(default)]
43    pub seed_hi: u32,
44    #[serde(default)]
45    pub seed_lo: u32,
46    #[serde(default)]
47    pub variants: Vec<Variant>,
48    #[serde(default)]
49    pub variables: HashMap<String, serde_json::Value>,
50    #[serde(default)]
51    pub custom_field_values: Option<Vec<CustomFieldValue>>,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize, Default)]
55pub struct Variant {
56    #[serde(default)]
57    pub config: Option<String>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct CustomFieldValue {
62    pub name: String,
63    pub value: String,
64    #[serde(rename = "type")]
65    pub field_type: String,
66}
67
68#[derive(Debug, Clone, Default)]
69pub struct Assignment {
70    pub id: i64,
71    pub iteration: i64,
72    pub full_on_variant: i64,
73    pub unit_type: Option<String>,
74    pub variant: i32,
75    pub overridden: bool,
76    pub assigned: bool,
77    pub exposed: bool,
78    pub eligible: bool,
79    pub full_on: bool,
80    pub custom: bool,
81    pub audience_mismatch: bool,
82    pub traffic_split: Option<Vec<f64>>,
83    pub variables: Option<HashMap<String, serde_json::Value>>,
84    pub attrs_seq: u64,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
88#[serde(rename_all = "camelCase")]
89pub struct Exposure {
90    pub id: i64,
91    pub name: String,
92    pub exposed_at: i64,
93    pub unit: Option<String>,
94    pub variant: i32,
95    pub assigned: bool,
96    pub eligible: bool,
97    pub overridden: bool,
98    pub full_on: bool,
99    pub custom: bool,
100    pub audience_mismatch: bool,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104#[serde(rename_all = "camelCase")]
105pub struct Attribute {
106    pub name: String,
107    pub value: serde_json::Value,
108    pub set_at: i64,
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
112#[serde(rename_all = "camelCase")]
113pub struct Goal {
114    pub name: String,
115    pub properties: Option<HashMap<String, serde_json::Value>>,
116    pub achieved_at: i64,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize)]
120#[serde(rename_all = "camelCase")]
121pub struct Unit {
122    #[serde(rename = "type")]
123    pub unit_type: String,
124    pub uid: Option<String>,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
128#[serde(rename_all = "camelCase")]
129pub struct PublishParams {
130    pub published_at: i64,
131    pub units: Vec<Unit>,
132    pub hashed: bool,
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub exposures: Option<Vec<Exposure>>,
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub goals: Option<Vec<Goal>>,
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub attributes: Option<Vec<Attribute>>,
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142pub enum ContextState {
143    Loading,
144    Ready,
145    Failed,
146    Finalizing,
147    Finalized,
148}
149
150impl Default for ContextState {
151    fn default() -> Self {
152        Self::Loading
153    }
154}
155
156#[derive(Debug, Clone)]
157pub struct ContextParams {
158    pub units: HashMap<String, String>,
159}
160
161#[derive(Debug, Clone, Default)]
162pub struct ContextOptions {
163    pub publish_delay: i64,
164    pub refresh_period: i64,
165}