use serde::Serialize;
use std::net::IpAddr;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Context {
#[serde(skip_serializing_if = "Option::is_none")]
pub hostname: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub screen: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub language: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub referrer: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tag: Option<String>,
#[serde(rename = "id", skip_serializing_if = "Option::is_none")]
pub distinct_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ip: Option<IpAddr>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_agent: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub browser: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub os: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub device: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<i64>,
}
impl Context {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn hostname(mut self, hostname: impl Into<String>) -> Self {
self.hostname = Some(hostname.into());
self
}
#[must_use]
pub fn screen(mut self, width: u32, height: u32) -> Self {
self.screen = Some(format!("{width}x{height}"));
self
}
#[must_use]
pub fn screen_value(mut self, screen: impl Into<String>) -> Self {
self.screen = Some(screen.into());
self
}
#[must_use]
pub fn language(mut self, language: impl Into<String>) -> Self {
self.language = Some(language.into());
self
}
#[must_use]
pub fn url(mut self, url: impl Into<String>) -> Self {
self.url = Some(url.into());
self
}
#[must_use]
pub fn referrer(mut self, referrer: impl Into<String>) -> Self {
self.referrer = Some(referrer.into());
self
}
#[must_use]
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
#[must_use]
pub fn tag(mut self, tag: impl Into<String>) -> Self {
self.tag = Some(tag.into());
self
}
#[must_use]
pub fn distinct_id(mut self, distinct_id: impl Into<String>) -> Self {
self.distinct_id = Some(distinct_id.into());
self
}
#[must_use]
pub fn ip(mut self, ip: IpAddr) -> Self {
self.ip = Some(ip);
self
}
#[must_use]
pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.user_agent = Some(user_agent.into());
self
}
#[must_use]
pub fn browser(mut self, browser: impl Into<String>) -> Self {
self.browser = Some(browser.into());
self
}
#[must_use]
pub fn os(mut self, os: impl Into<String>) -> Self {
self.os = Some(os.into());
self
}
#[must_use]
pub fn device(mut self, device: impl Into<String>) -> Self {
self.device = Some(device.into());
self
}
#[must_use]
pub fn timestamp(mut self, unix_seconds: i64) -> Self {
self.timestamp = Some(unix_seconds);
self
}
#[must_use]
pub fn at(mut self, time: SystemTime) -> Self {
self.timestamp = Some(unix_seconds(time));
self
}
}
fn unix_seconds(time: SystemTime) -> i64 {
match time.duration_since(UNIX_EPOCH) {
Ok(duration) => i64::try_from(duration.as_secs()).unwrap_or(i64::MAX),
Err(error) => {
let seconds = i64::try_from(error.duration().as_secs()).unwrap_or(i64::MAX);
-seconds
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn converts_system_time_on_both_sides_of_epoch() {
assert_eq!(
Context::new()
.at(UNIX_EPOCH + Duration::from_secs(42))
.timestamp,
Some(42)
);
assert_eq!(
Context::new()
.at(UNIX_EPOCH - Duration::from_secs(42))
.timestamp,
Some(-42)
);
}
}