statsig_rust/event_logging/
exposable_string.rs

1use std::sync::Arc;
2
3use serde::{Deserialize, Deserializer, Serialize, Serializer};
4
5use crate::hashing::ahash_str;
6
7lazy_static::lazy_static! {
8    pub static ref EMPTY_STRING: ExposableString = ExposableString::new(String::new());
9    pub static ref DEFAULT_RULE: ExposableString = ExposableString::new("default".to_string());
10    pub static ref DISABLED_RULE: ExposableString = ExposableString::new("disabled".to_string());
11}
12
13#[derive(Debug, Clone)]
14pub struct ExposableString {
15    pub hash_value: u64,
16
17    // DO NOT MAKE "pub" OR FIRED
18    value: Arc<String>,
19}
20
21impl ExposableString {
22    pub fn new(value: String) -> Self {
23        let hash_value = ahash_str(&value);
24        Self {
25            value: Arc::new(value),
26            hash_value,
27        }
28    }
29
30    pub fn as_str(&self) -> &str {
31        self.value.as_str()
32    }
33
34    pub fn clone_inner(&self) -> Arc<String> {
35        self.value.clone()
36    }
37
38    pub fn from_arc(value: Arc<String>) -> Self {
39        let hash_value = ahash_str(&value);
40        Self { value, hash_value }
41    }
42
43    /// Clones the value out of the Arc. This is not performant.
44    /// Please only use this if we are giving a value to a caller outside of this library.
45    /// Consider using the `as_str` or `clone_inner` methods instead.
46    pub fn unperformant_to_string(&self) -> String {
47        self.value.to_string()
48    }
49}
50
51impl Default for ExposableString {
52    fn default() -> Self {
53        EMPTY_STRING.clone()
54    }
55}
56
57impl<'de> Deserialize<'de> for ExposableString {
58    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
59    where
60        D: Deserializer<'de>,
61    {
62        let value = String::deserialize(deserializer)?;
63        Ok(Self::new(value))
64    }
65}
66
67impl Serialize for ExposableString {
68    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
69    where
70        S: Serializer,
71    {
72        serializer.serialize_str(&self.value)
73    }
74}
75
76impl PartialEq for ExposableString {
77    fn eq(&self, other: &Self) -> bool {
78        self.hash_value == other.hash_value
79    }
80}
81
82impl Eq for ExposableString {}