|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use chrono::NaiveDateTime; |
| 4 | +use semver::Version; |
| 5 | +use serde::Serialize; |
| 6 | + |
| 7 | +use crate::Error; |
| 8 | + |
| 9 | +#[derive(Serialize, Debug, PartialEq, Eq)] |
| 10 | +pub struct Event { |
| 11 | + event: String, |
| 12 | + properties: Properties, |
| 13 | + timestamp: Option<NaiveDateTime>, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Serialize, Debug, PartialEq, Eq)] |
| 17 | +pub struct Properties { |
| 18 | + distinct_id: String, |
| 19 | + props: HashMap<String, serde_json::Value>, |
| 20 | +} |
| 21 | + |
| 22 | +impl Properties { |
| 23 | + fn new<S: Into<String>>(distinct_id: S) -> Self { |
| 24 | + Self { |
| 25 | + distinct_id: distinct_id.into(), |
| 26 | + props: Default::default(), |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Event { |
| 32 | + pub fn new<S: Into<String>>(event: S, distinct_id: S) -> Self { |
| 33 | + Self { |
| 34 | + event: event.into(), |
| 35 | + properties: Properties::new(distinct_id), |
| 36 | + timestamp: None, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// Errors if `prop` fails to serialize |
| 41 | + pub fn insert_prop<K: Into<String>, P: Serialize>( |
| 42 | + &mut self, |
| 43 | + key: K, |
| 44 | + prop: P, |
| 45 | + ) -> Result<(), Error> { |
| 46 | + let as_json = |
| 47 | + serde_json::to_value(prop).map_err(|e| Error::Serialization(e.to_string()))?; |
| 48 | + let _ = self.properties.props.insert(key.into(), as_json); |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// This exists so that the client doesn't have to specify the API key over and over |
| 54 | +#[derive(Serialize)] |
| 55 | +pub struct InnerEvent { |
| 56 | + api_key: String, |
| 57 | + event: String, |
| 58 | + properties: Properties, |
| 59 | + timestamp: Option<NaiveDateTime>, |
| 60 | +} |
| 61 | + |
| 62 | +impl InnerEvent { |
| 63 | + pub fn new(event: Event, api_key: String) -> Self { |
| 64 | + let mut properties = event.properties; |
| 65 | + |
| 66 | + // Add $lib_name and $lib_version to the properties |
| 67 | + properties.props.insert( |
| 68 | + "$lib_name".into(), |
| 69 | + serde_json::Value::String("posthog-rs".into()), |
| 70 | + ); |
| 71 | + |
| 72 | + let version_str = env!("CARGO_PKG_VERSION"); |
| 73 | + properties.props.insert( |
| 74 | + "$lib_version".into(), |
| 75 | + serde_json::Value::String(version_str.into()), |
| 76 | + ); |
| 77 | + |
| 78 | + if let Ok(version) = version_str.parse::<Version>() { |
| 79 | + properties.props.insert( |
| 80 | + "$lib_version__major".into(), |
| 81 | + serde_json::Value::Number(version.major.into()), |
| 82 | + ); |
| 83 | + properties.props.insert( |
| 84 | + "$lib_version__minor".into(), |
| 85 | + serde_json::Value::Number(version.minor.into()), |
| 86 | + ); |
| 87 | + properties.props.insert( |
| 88 | + "$lib_version__patch".into(), |
| 89 | + serde_json::Value::Number(version.patch.into()), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + Self { |
| 94 | + api_key, |
| 95 | + event: event.event, |
| 96 | + properties, |
| 97 | + timestamp: event.timestamp, |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +pub mod tests { |
| 104 | + use crate::{event::InnerEvent, Event}; |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn inner_event_adds_lib_properties_correctly() { |
| 108 | + // Arrange |
| 109 | + let mut event = Event::new("unit test event", "1234"); |
| 110 | + event.insert_prop("key1", "value1").unwrap(); |
| 111 | + let api_key = "test_api_key".to_string(); |
| 112 | + |
| 113 | + // Act |
| 114 | + let inner_event = InnerEvent::new(event, api_key); |
| 115 | + |
| 116 | + // Assert |
| 117 | + let props = &inner_event.properties.props; |
| 118 | + assert_eq!( |
| 119 | + props.get("$lib_name"), |
| 120 | + Some(&serde_json::Value::String("posthog-rs".to_string())) |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
0 commit comments