1use serde::Serialize;
2use std::net::IpAddr;
3use std::time::{SystemTime, UNIX_EPOCH};
4
5#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Context {
13 #[serde(skip_serializing_if = "Option::is_none")]
15 pub hostname: Option<String>,
16 #[serde(skip_serializing_if = "Option::is_none")]
18 pub screen: Option<String>,
19 #[serde(skip_serializing_if = "Option::is_none")]
21 pub language: Option<String>,
22 #[serde(skip_serializing_if = "Option::is_none")]
24 pub url: Option<String>,
25 #[serde(skip_serializing_if = "Option::is_none")]
27 pub referrer: Option<String>,
28 #[serde(skip_serializing_if = "Option::is_none")]
30 pub title: Option<String>,
31 #[serde(skip_serializing_if = "Option::is_none")]
33 pub tag: Option<String>,
34 #[serde(rename = "id", skip_serializing_if = "Option::is_none")]
36 pub distinct_id: Option<String>,
37 #[serde(skip_serializing_if = "Option::is_none")]
39 pub ip: Option<IpAddr>,
40 #[serde(skip_serializing_if = "Option::is_none")]
42 pub user_agent: Option<String>,
43 #[serde(skip_serializing_if = "Option::is_none")]
45 pub browser: Option<String>,
46 #[serde(skip_serializing_if = "Option::is_none")]
48 pub os: Option<String>,
49 #[serde(skip_serializing_if = "Option::is_none")]
51 pub device: Option<String>,
52 #[serde(skip_serializing_if = "Option::is_none")]
54 pub timestamp: Option<i64>,
55}
56
57impl Context {
58 #[must_use]
60 pub fn new() -> Self {
61 Self::default()
62 }
63
64 #[must_use]
66 pub fn hostname(mut self, hostname: impl Into<String>) -> Self {
67 self.hostname = Some(hostname.into());
68 self
69 }
70
71 #[must_use]
73 pub fn screen(mut self, width: u32, height: u32) -> Self {
74 self.screen = Some(format!("{width}x{height}"));
75 self
76 }
77
78 #[must_use]
80 pub fn screen_value(mut self, screen: impl Into<String>) -> Self {
81 self.screen = Some(screen.into());
82 self
83 }
84
85 #[must_use]
87 pub fn language(mut self, language: impl Into<String>) -> Self {
88 self.language = Some(language.into());
89 self
90 }
91
92 #[must_use]
94 pub fn url(mut self, url: impl Into<String>) -> Self {
95 self.url = Some(url.into());
96 self
97 }
98
99 #[must_use]
101 pub fn referrer(mut self, referrer: impl Into<String>) -> Self {
102 self.referrer = Some(referrer.into());
103 self
104 }
105
106 #[must_use]
108 pub fn title(mut self, title: impl Into<String>) -> Self {
109 self.title = Some(title.into());
110 self
111 }
112
113 #[must_use]
115 pub fn tag(mut self, tag: impl Into<String>) -> Self {
116 self.tag = Some(tag.into());
117 self
118 }
119
120 #[must_use]
122 pub fn distinct_id(mut self, distinct_id: impl Into<String>) -> Self {
123 self.distinct_id = Some(distinct_id.into());
124 self
125 }
126
127 #[must_use]
129 pub fn ip(mut self, ip: IpAddr) -> Self {
130 self.ip = Some(ip);
131 self
132 }
133
134 #[must_use]
136 pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
137 self.user_agent = Some(user_agent.into());
138 self
139 }
140
141 #[must_use]
143 pub fn browser(mut self, browser: impl Into<String>) -> Self {
144 self.browser = Some(browser.into());
145 self
146 }
147
148 #[must_use]
150 pub fn os(mut self, os: impl Into<String>) -> Self {
151 self.os = Some(os.into());
152 self
153 }
154
155 #[must_use]
157 pub fn device(mut self, device: impl Into<String>) -> Self {
158 self.device = Some(device.into());
159 self
160 }
161
162 #[must_use]
167 pub fn timestamp(mut self, unix_seconds: i64) -> Self {
168 self.timestamp = Some(unix_seconds);
169 self
170 }
171
172 #[must_use]
174 pub fn at(mut self, time: SystemTime) -> Self {
175 self.timestamp = Some(unix_seconds(time));
176 self
177 }
178}
179
180fn unix_seconds(time: SystemTime) -> i64 {
181 match time.duration_since(UNIX_EPOCH) {
182 Ok(duration) => i64::try_from(duration.as_secs()).unwrap_or(i64::MAX),
183 Err(error) => {
184 let seconds = i64::try_from(error.duration().as_secs()).unwrap_or(i64::MAX);
185 -seconds
186 }
187 }
188}
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193 use std::time::Duration;
194
195 #[test]
196 fn converts_system_time_on_both_sides_of_epoch() {
197 assert_eq!(
198 Context::new()
199 .at(UNIX_EPOCH + Duration::from_secs(42))
200 .timestamp,
201 Some(42)
202 );
203 assert_eq!(
204 Context::new()
205 .at(UNIX_EPOCH - Duration::from_secs(42))
206 .timestamp,
207 Some(-42)
208 );
209 }
210}