1use std::{collections::HashMap, marker::PhantomData};
6
7#[derive(uniffi::Enum, Default)]
15pub enum Lifetime {
16 #[default]
17 Ping,
18 Application,
19 User,
20}
21
22#[derive(uniffi::Enum)]
23pub enum MetricLabel {
24 Static(String),
25 Label(String),
26 KeyOnly(String, String),
27 CategoryOnly(String, String),
28 KeyAndCategory(String, String),
29}
30
31#[derive(uniffi::Record, Default)]
32pub struct CommonMetricData {
33 pub category: String,
34 pub name: String,
35 pub send_in_pings: Vec<String>,
36 pub lifetime: Lifetime,
37 pub disabled: bool,
38 pub label: Option<MetricLabel>,
39 pub in_session: bool,
40}
41
42#[derive(uniffi::Record)]
43pub struct Rate {
44 numerator: i32,
45 denominator: i32,
46}
47
48pub type JsonValue = String;
49
50#[derive(uniffi::Record, Debug)]
51pub struct RecordedEvent {
52 timestamp: u64,
53 category: String,
54 name: String,
55 extra: Option<::std::collections::HashMap<String, String>>,
56 session_metadata: Option<SessionMetadata>,
57}
58
59#[derive(uniffi::Record, Debug)]
60pub struct SessionMetadata {
61 pub session_id: String,
62 pub session_seq: u64,
63 pub event_seq: u64,
64 pub session_sample_rate: f64,
65 pub session_start_time: Option<String>,
66}
67
68#[derive(uniffi::Record)]
69pub struct Datetime {
70 year: i32,
71 month: u32,
72 day: u32,
73 hour: u32,
74 minute: u32,
75 second: u32,
76 nanosecond: u32,
77 offset_seconds: i32,
78}
79
80#[derive(uniffi::Record)]
81pub struct DistributionData {
82 values: ::std::collections::HashMap<i64, i64>,
83 sum: i64,
84 count: i64,
85}
86
87#[derive(uniffi::Record)]
88#[cfg_attr(not(feature = "active"), derive(Default))]
89pub struct TimerId {
90 id: u64,
91}
92
93#[derive(uniffi::Enum)]
94pub enum ErrorType {
95 InvalidValue,
96 InvalidLabel,
97 InvalidState,
98 InvalidOverflow,
99}
100
101#[derive(uniffi::Enum)]
102#[repr(i32)]
103pub enum TimeUnit {
104 Nanosecond,
106 Microsecond,
108 Millisecond,
110 Second,
112 Minute,
114 Hour,
116 Day,
118}
119
120#[derive(uniffi::Enum)]
121#[repr(i32)] pub enum MemoryUnit {
123 Byte,
125 Kilobyte,
127 Megabyte,
129 Gigabyte,
131}
132
133#[derive(uniffi::Enum)]
134pub enum HistogramType {
135 Linear,
137 Exponential,
139}
140
141pub type CowString = std::borrow::Cow<'static, str>;
142
143pub trait ExtraKeys {
144 const ALLOWED_KEYS: &'static [&'static str];
146
147 fn into_ffi_extra(self) -> HashMap<String, String>;
149}
150
151pub enum NoExtraKeys {}
152
153impl ExtraKeys for NoExtraKeys {
154 const ALLOWED_KEYS: &'static [&'static str] = &[];
155
156 fn into_ffi_extra(self) -> HashMap<String, String> {
157 unimplemented!("non-existing extra keys can't be turned into a list")
158 }
159}
160
161pub struct EventMetric<K> {
167 pub(crate) inner: crate::metrics::EventMetric,
168 extra_keys: PhantomData<K>,
169}
170
171impl<K: ExtraKeys> EventMetric<K> {
172 pub fn new(meta: CommonMetricData) -> Self {
174 let allowed_extra_keys = K::ALLOWED_KEYS.iter().map(|s| s.to_string()).collect();
175 let inner = crate::metrics::EventMetric::new(meta, allowed_extra_keys);
176 Self {
177 inner,
178 extra_keys: PhantomData,
179 }
180 }
181
182 pub fn record<M: Into<Option<K>>>(&self, extra: M) {
188 let extra = extra
189 .into()
190 .map(|e| e.into_ffi_extra())
191 .unwrap_or_else(HashMap::new);
192 self.inner.record(extra);
193 }
194
195 pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 {
207 self.inner.test_get_num_recorded_errors(error)
208 }
209}
210
211#[cfg(not(feature = "active"))]
212pub struct PingType;
213
214#[cfg(feature = "active")]
215pub struct PingType {
216 inner: crate::metrics::PingType,
217}
218
219#[cfg(feature = "active")]
220impl PingType {
221 #[allow(clippy::too_many_arguments)]
237 pub fn new<A: Into<String>>(
238 name: A,
239 include_client_id: bool,
240 send_if_empty: bool,
241 precise_timestamps: bool,
242 include_info_sections: bool,
243 enabled: bool,
244 schedules_pings: Vec<String>,
245 reason_codes: Vec<String>,
246 follows_collection_enabled: bool,
247 uploader_capabilities: Vec<String>,
248 ) -> Self {
249 let inner = crate::metrics::PingType::new(
250 name.into(),
251 include_client_id,
252 send_if_empty,
253 precise_timestamps,
254 include_info_sections,
255 enabled,
256 schedules_pings,
257 reason_codes,
258 follows_collection_enabled,
259 uploader_capabilities,
260 );
261
262 Self { inner }
263 }
264
265 pub fn submit(&self, reason: Option<&str>) {
266 self.inner.submit(reason.map(|s| s.to_string()))
267 }
268
269 pub fn set_enabled(&self, enabled: bool) {
270 self.inner.set_enabled(enabled)
271 }
272}
273
274#[cfg(not(feature = "active"))]
275impl PingType {
276 #[allow(clippy::too_many_arguments)]
292 pub fn new<A: Into<String>>(
293 _name: A,
294 _include_client_id: bool,
295 _send_if_empty: bool,
296 _precise_timestamps: bool,
297 _include_info_sections: bool,
298 _enabled: bool,
299 _schedules_pings: Vec<String>,
300 _reason_codes: Vec<String>,
301 _follows_collection_enabled: bool,
302 _uploader_capabilities: Vec<String>,
303 ) -> Self {
304 Self
305 }
306
307 pub fn submit(&self, _reason: Option<&str>) {}
308
309 pub fn set_enabled(&self, _enabled: bool) {}
310}