Skip to main content

statsig_rust/user/
statsig_user.rs

1use crate::evaluation::dynamic_value::DynamicValue;
2use crate::statsig_metadata;
3use crate::{dyn_value, evaluation::dynamic_string::DynamicString};
4use std::{collections::HashMap, sync::Arc};
5
6use super::{into_optional::IntoOptional, unit_id::UnitID, user_data::UserData};
7
8#[derive(Clone)]
9pub struct StatsigUser {
10    pub data: Arc<UserData>,
11
12    pub(crate) sdk_version: &'static str,
13}
14
15impl StatsigUser {
16    #[must_use]
17    pub fn with_user_id(user_id: impl Into<UnitID>) -> Self {
18        let unit_id: UnitID = user_id.into();
19        Self::new(UserData {
20            user_id: Some(unit_id.into()),
21            ..UserData::default()
22        })
23    }
24
25    #[must_use]
26    pub fn with_custom_ids<K, U>(custom_ids: HashMap<K, U>) -> Self
27    where
28        K: Into<String>,
29        U: Into<UnitID>,
30    {
31        let custom_ids: HashMap<String, DynamicValue> = custom_ids
32            .into_iter()
33            .map(|(k, v)| (k.into(), v.into().into()))
34            .collect();
35
36        Self::new(UserData {
37            custom_ids: Some(custom_ids),
38            ..UserData::default()
39        })
40    }
41
42    pub fn new(inner: UserData) -> Self {
43        Self {
44            data: Arc::new(inner),
45            sdk_version: statsig_metadata::SDK_VERSION,
46        }
47    }
48}
49
50// -------------------------------------------------------------------------------- [Getters/Setters]
51
52macro_rules! string_field_accessor {
53    ($self:ident, $getter_name:ident, $setter_name:ident, $field:ident) => {
54        pub fn $getter_name(&self) -> Option<&str> {
55            self.data
56                .$field
57                .as_ref()?
58                .string_value
59                .as_ref()
60                .map(|s| s.value.as_str())
61        }
62
63        pub fn $setter_name(&mut self, value: impl IntoOptional<String>) {
64            let value = value.into_optional();
65            let mut_data = Arc::make_mut(&mut self.data);
66            match value {
67                Some(value) => {
68                    mut_data.$field = Some(dyn_value!(value));
69                }
70                None => mut_data.$field = None,
71            }
72        }
73    };
74}
75
76macro_rules! map_field_accessor {
77    ($self:ident, $getter_name:ident, $setter_name:ident, $field:ident) => {
78        pub fn $getter_name(&self) -> Option<&HashMap<String, DynamicValue>> {
79            self.data.$field.as_ref()
80        }
81
82        pub fn $setter_name<K, V>(&mut self, value: impl IntoOptional<HashMap<K, V>>)
83        where
84            K: Into<String>,
85            V: Into<DynamicValue>,
86        {
87            let mut_data = Arc::make_mut(&mut self.data);
88            let value = match value.into_optional() {
89                Some(value) => value,
90                None => {
91                    mut_data.$field = None;
92                    return;
93                }
94            };
95
96            mut_data.$field = Some(
97                value
98                    .into_iter()
99                    .map(|(k, v)| (k.into(), v.into()))
100                    .collect(),
101            );
102        }
103    };
104}
105
106impl StatsigUser {
107    pub fn get_user_id(&self) -> Option<&str> {
108        self.data
109            .user_id
110            .as_ref()?
111            .string_value
112            .as_ref()
113            .map(|s| s.value.as_str())
114    }
115
116    pub fn set_user_id(&mut self, user_id: impl Into<UnitID>) {
117        let unit_id = user_id.into();
118        let mut_data = Arc::make_mut(&mut self.data);
119        mut_data.user_id = Some(unit_id.into());
120    }
121
122    pub fn get_custom_ids(&self) -> Option<HashMap<&str, &str>> {
123        let mapped = self
124            .data
125            .custom_ids
126            .as_ref()?
127            .iter()
128            .map(entry_to_key_value_refs)
129            .collect();
130
131        Some(mapped)
132    }
133
134    pub fn set_custom_ids<K, U>(&mut self, custom_ids: HashMap<K, U>)
135    where
136        K: Into<String>,
137        U: Into<UnitID>,
138    {
139        let custom_ids = custom_ids
140            .into_iter()
141            .map(|(k, v)| (k.into(), v.into().into()))
142            .collect();
143
144        let mut_data = Arc::make_mut(&mut self.data);
145        mut_data.custom_ids = Some(custom_ids);
146    }
147
148    pub fn get_unit_id(&self, id_type: &DynamicString) -> Option<&DynamicValue> {
149        if id_type.lowercased_value.eq("userid") {
150            return self.data.user_id.as_ref();
151        }
152
153        let custom_ids = self.data.custom_ids.as_ref()?;
154
155        if let Some(custom_id) = custom_ids.get(id_type.value.as_str()) {
156            return Some(custom_id);
157        }
158
159        custom_ids.get(id_type.lowercased_value.as_str())
160    }
161
162    string_field_accessor!(self, get_email, set_email, email);
163    string_field_accessor!(self, get_ip, set_ip, ip);
164    string_field_accessor!(self, get_user_agent, set_user_agent, user_agent);
165    string_field_accessor!(self, get_country, set_country, country);
166    string_field_accessor!(self, get_locale, set_locale, locale);
167    string_field_accessor!(self, get_app_version, set_app_version, app_version);
168
169    map_field_accessor!(self, get_custom, set_custom, custom);
170    map_field_accessor!(
171        self,
172        get_private_attributes,
173        set_private_attributes,
174        private_attributes
175    );
176}
177
178// -------------------------------------------------------------------------------- [Helpers]
179
180fn entry_to_key_value_refs<'a>(entry: (&'a String, &'a DynamicValue)) -> (&'a str, &'a str) {
181    let (key, value) = entry;
182
183    (
184        key.as_str(),
185        value
186            .string_value
187            .as_ref()
188            .map(|s| s.value.as_str())
189            .unwrap_or(""),
190    )
191}