use std::{collections::HashMap, marker::PhantomData};
#[derive(uniffi::Enum, Default)]
pub enum Lifetime {
#[default]
Ping,
Application,
User,
}
#[derive(uniffi::Enum)]
pub enum MetricLabel {
Static(String),
Label(String),
KeyOnly(String, String),
CategoryOnly(String, String),
KeyAndCategory(String, String),
}
#[derive(uniffi::Record, Default)]
pub struct CommonMetricData {
pub category: String,
pub name: String,
pub send_in_pings: Vec<String>,
pub lifetime: Lifetime,
pub disabled: bool,
pub label: Option<MetricLabel>,
pub in_session: bool,
}
#[derive(uniffi::Record)]
pub struct Rate {
numerator: i32,
denominator: i32,
}
pub type JsonValue = String;
#[derive(uniffi::Record, Debug)]
pub struct RecordedEvent {
timestamp: u64,
category: String,
name: String,
extra: Option<::std::collections::HashMap<String, String>>,
session_metadata: Option<SessionMetadata>,
}
#[derive(uniffi::Record, Debug)]
pub struct SessionMetadata {
pub session_id: String,
pub session_seq: u64,
pub event_seq: u64,
pub session_sample_rate: f64,
pub session_start_time: Option<String>,
}
#[derive(uniffi::Record)]
pub struct Datetime {
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
nanosecond: u32,
offset_seconds: i32,
}
#[derive(uniffi::Record)]
pub struct DistributionData {
values: ::std::collections::HashMap<i64, i64>,
sum: i64,
count: i64,
}
#[derive(uniffi::Record)]
#[cfg_attr(not(feature = "active"), derive(Default))]
pub struct TimerId {
id: u64,
}
#[derive(uniffi::Enum)]
pub enum ErrorType {
InvalidValue,
InvalidLabel,
InvalidState,
InvalidOverflow,
}
#[derive(uniffi::Enum)]
#[repr(i32)]
pub enum TimeUnit {
Nanosecond,
Microsecond,
Millisecond,
Second,
Minute,
Hour,
Day,
}
#[derive(uniffi::Enum)]
#[repr(i32)] pub enum MemoryUnit {
Byte,
Kilobyte,
Megabyte,
Gigabyte,
}
#[derive(uniffi::Enum)]
pub enum HistogramType {
Linear,
Exponential,
}
pub type CowString = std::borrow::Cow<'static, str>;
pub trait ExtraKeys {
const ALLOWED_KEYS: &'static [&'static str];
fn into_ffi_extra(self) -> HashMap<String, String>;
}
pub enum NoExtraKeys {}
impl ExtraKeys for NoExtraKeys {
const ALLOWED_KEYS: &'static [&'static str] = &[];
fn into_ffi_extra(self) -> HashMap<String, String> {
unimplemented!("non-existing extra keys can't be turned into a list")
}
}
pub struct EventMetric<K> {
pub(crate) inner: crate::metrics::EventMetric,
extra_keys: PhantomData<K>,
}
impl<K: ExtraKeys> EventMetric<K> {
pub fn new(meta: CommonMetricData) -> Self {
let allowed_extra_keys = K::ALLOWED_KEYS.iter().map(|s| s.to_string()).collect();
let inner = crate::metrics::EventMetric::new(meta, allowed_extra_keys);
Self {
inner,
extra_keys: PhantomData,
}
}
pub fn record<M: Into<Option<K>>>(&self, extra: M) {
let extra = extra
.into()
.map(|e| e.into_ffi_extra())
.unwrap_or_else(HashMap::new);
self.inner.record(extra);
}
pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 {
self.inner.test_get_num_recorded_errors(error)
}
}
#[cfg(not(feature = "active"))]
pub struct PingType;
#[cfg(feature = "active")]
pub struct PingType {
inner: crate::metrics::PingType,
}
#[cfg(feature = "active")]
impl PingType {
#[allow(clippy::too_many_arguments)]
pub fn new<A: Into<String>>(
name: A,
include_client_id: bool,
send_if_empty: bool,
precise_timestamps: bool,
include_info_sections: bool,
enabled: bool,
schedules_pings: Vec<String>,
reason_codes: Vec<String>,
follows_collection_enabled: bool,
uploader_capabilities: Vec<String>,
) -> Self {
let inner = crate::metrics::PingType::new(
name.into(),
include_client_id,
send_if_empty,
precise_timestamps,
include_info_sections,
enabled,
schedules_pings,
reason_codes,
follows_collection_enabled,
uploader_capabilities,
);
Self { inner }
}
pub fn submit(&self, reason: Option<&str>) {
self.inner.submit(reason.map(|s| s.to_string()))
}
pub fn set_enabled(&self, enabled: bool) {
self.inner.set_enabled(enabled)
}
}
#[cfg(not(feature = "active"))]
impl PingType {
#[allow(clippy::too_many_arguments)]
pub fn new<A: Into<String>>(
_name: A,
_include_client_id: bool,
_send_if_empty: bool,
_precise_timestamps: bool,
_include_info_sections: bool,
_enabled: bool,
_schedules_pings: Vec<String>,
_reason_codes: Vec<String>,
_follows_collection_enabled: bool,
_uploader_capabilities: Vec<String>,
) -> Self {
Self
}
pub fn submit(&self, _reason: Option<&str>) {}
pub fn set_enabled(&self, _enabled: bool) {}
}