use offline_protocol_telemetry_wire::OsKind;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TelemetryOs {
Ios,
Android,
Linux,
Macos,
Windows,
Other,
}
impl TelemetryOs {
pub fn current() -> Self {
if cfg!(target_os = "ios") {
Self::Ios
} else if cfg!(target_os = "android") {
Self::Android
} else if cfg!(target_os = "linux") {
Self::Linux
} else if cfg!(target_os = "macos") {
Self::Macos
} else if cfg!(target_os = "windows") {
Self::Windows
} else {
Self::Other
}
}
pub(crate) fn wire(self) -> OsKind {
match self {
Self::Ios => OsKind::Ios,
Self::Android => OsKind::Android,
Self::Linux => OsKind::Linux,
Self::Macos => OsKind::Macos,
Self::Windows => OsKind::Windows,
Self::Other => OsKind::Other,
}
}
pub(crate) fn user_agent_token(self) -> &'static str {
match self {
Self::Ios => "ios",
Self::Android => "android",
Self::Linux => "linux",
Self::Macos => "macos",
Self::Windows => "windows",
Self::Other => "other",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppState {
Active,
Background,
Inactive,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TelemetryHost {
pub os: TelemetryOs,
pub os_major: u16,
pub app_state: AppState,
}
impl TelemetryHost {
pub fn new(os: TelemetryOs, os_major: u16) -> Self {
Self {
os,
os_major,
app_state: AppState::Active,
}
}
pub fn with_app_state(mut self, app_state: AppState) -> Self {
self.app_state = app_state;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_os_has_a_wire_form_and_a_user_agent_token() {
for os in [
TelemetryOs::Ios,
TelemetryOs::Android,
TelemetryOs::Linux,
TelemetryOs::Macos,
TelemetryOs::Windows,
TelemetryOs::Other,
] {
let wire = serde_json::to_value(os.wire()).expect("serializes");
assert_eq!(
wire,
serde_json::Value::String(os.user_agent_token().into())
);
}
}
#[test]
fn current_platform_is_one_of_the_named_hosts() {
assert_ne!(TelemetryOs::current(), TelemetryOs::Other);
}
}