1use serde::Deserialize;
18use serde::Serialize;
19
20use crate::StoreContractViolation;
21
22pub const JAVA_COMPAT_TIMER_FORMAT_VERSION: u16 = 1;
24
25pub const EXTENDED_TIMELINE_FORMAT_VERSION: u16 = 1;
27
28#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
30pub struct TimerRecallRequest {
31 pub topic: String,
33 pub unique_key: String,
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
39pub enum TimerRecallStatus {
40 Cancelled,
42 AlreadyCancelled,
44 TooLate,
46 NotFound,
48 Retry,
50 Quarantined,
52 Unsupported,
54}
55
56#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
58#[serde(rename_all = "snake_case")]
59pub enum TimerStoreMode {
60 #[default]
62 JavaCompat,
63 ExtendedTimeline,
65}
66
67impl TimerStoreMode {
68 pub const fn as_str(self) -> &'static str {
70 match self {
71 Self::JavaCompat => "java_compat",
72 Self::ExtendedTimeline => "extended_timeline",
73 }
74 }
75}
76
77#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Hash, Serialize)]
79pub enum TimerEngineId {
80 #[serde(rename = "F")]
82 JavaCompat,
83 #[serde(rename = "R")]
85 ExtendedTimeline,
86}
87
88impl TimerEngineId {
89 pub const fn as_str(self) -> &'static str {
91 match self {
92 Self::JavaCompat => "F",
93 Self::ExtendedTimeline => "R",
94 }
95 }
96
97 pub fn parse(value: &str) -> Result<Self, StoreContractViolation> {
103 match value {
104 "F" => Ok(Self::JavaCompat),
105 "R" => Ok(Self::ExtendedTimeline),
106 value => Err(StoreContractViolation::TimerUnknownEngine(value.to_owned())),
107 }
108 }
109}
110
111#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
113pub struct TimerId(u128);
114
115impl TimerId {
116 pub const fn new(value: u128) -> Self {
118 Self(value)
119 }
120
121 pub const fn get(self) -> u128 {
123 self.0
124 }
125}
126
127#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
129pub struct TimerGeneration(u64);
130
131impl TimerGeneration {
132 pub const fn new(value: u64) -> Self {
134 Self(value)
135 }
136
137 pub const fn get(self) -> u64 {
139 self.0
140 }
141}
142
143#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
145pub struct TimerEngineEpoch(u64);
146
147impl TimerEngineEpoch {
148 pub const fn new(value: u64) -> Self {
150 Self(value)
151 }
152
153 pub const fn get(self) -> u64 {
155 self.0
156 }
157}
158
159#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
161pub struct TimerSourceCqOffset(i64);
162
163impl TimerSourceCqOffset {
164 pub const fn new(value: i64) -> Self {
166 Self(value)
167 }
168
169 pub const fn get(self) -> i64 {
171 self.0
172 }
173}
174
175#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Hash, Serialize)]
177pub struct TimerPayloadLocator {
178 commit_log_offset: i64,
179 size: u32,
180}
181
182impl TimerPayloadLocator {
183 pub const fn try_new(commit_log_offset: i64, size: u32) -> Result<Self, StoreContractViolation> {
189 if commit_log_offset < 0 || size == 0 {
190 return Err(StoreContractViolation::TimerInvalidPayloadLocator);
191 }
192 Ok(Self {
193 commit_log_offset,
194 size,
195 })
196 }
197
198 pub const fn commit_log_offset(self) -> i64 {
200 self.commit_log_offset
201 }
202
203 pub const fn size(self) -> u32 {
205 self.size
206 }
207}
208
209#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Hash, Serialize)]
214pub struct TimerPayloadStoreLocator {
215 due_day_utc: i32,
216 lane: u16,
217 segment_id: u64,
218 offset: u64,
219 length: u32,
220 checksum: u32,
221}
222
223impl TimerPayloadStoreLocator {
224 pub const fn try_new(
230 due_day_utc: i32,
231 lane: u16,
232 segment_id: u64,
233 offset: u64,
234 length: u32,
235 checksum: u32,
236 ) -> Result<Self, StoreContractViolation> {
237 if length == 0 {
238 return Err(StoreContractViolation::TimerInvalidPayloadStoreLocator);
239 }
240 Ok(Self {
241 due_day_utc,
242 lane,
243 segment_id,
244 offset,
245 length,
246 checksum,
247 })
248 }
249
250 pub const fn due_day_utc(self) -> i32 {
252 self.due_day_utc
253 }
254
255 pub const fn lane(self) -> u16 {
257 self.lane
258 }
259
260 pub const fn segment_id(self) -> u64 {
262 self.segment_id
263 }
264
265 pub const fn offset(self) -> u64 {
267 self.offset
268 }
269
270 pub const fn length(self) -> u32 {
272 self.length
273 }
274
275 pub const fn checksum(self) -> u32 {
277 self.checksum
278 }
279}
280
281#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
283pub struct TimerTimelineCursor {
284 due_time_ms: i64,
285 sequence: u64,
286}
287
288impl TimerTimelineCursor {
289 pub const fn new(due_time_ms: i64, sequence: u64) -> Self {
291 Self { due_time_ms, sequence }
292 }
293
294 pub const fn due_time_ms(self) -> i64 {
296 self.due_time_ms
297 }
298
299 pub const fn sequence(self) -> u64 {
301 self.sequence
302 }
303}
304
305#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
307pub struct PersistedTimerRoute {
308 engine_id: TimerEngineId,
309 format_version: u16,
310 normalization_policy_fingerprint: u64,
311 generation: TimerGeneration,
312 delivery_token: String,
313}
314
315impl PersistedTimerRoute {
316 pub fn try_new(
322 engine_id: TimerEngineId,
323 format_version: u16,
324 normalization_policy_fingerprint: u64,
325 generation: TimerGeneration,
326 delivery_token: impl Into<String>,
327 ) -> Result<Self, StoreContractViolation> {
328 let delivery_token = delivery_token.into();
329 if format_version == 0 || delivery_token.is_empty() {
330 return Err(StoreContractViolation::TimerInvalidRoute);
331 }
332 Ok(Self {
333 engine_id,
334 format_version,
335 normalization_policy_fingerprint,
336 generation,
337 delivery_token,
338 })
339 }
340
341 pub const fn engine_id(&self) -> TimerEngineId {
343 self.engine_id
344 }
345
346 pub const fn format_version(&self) -> u16 {
348 self.format_version
349 }
350
351 pub const fn normalization_policy_fingerprint(&self) -> u64 {
353 self.normalization_policy_fingerprint
354 }
355
356 pub const fn generation(&self) -> TimerGeneration {
358 self.generation
359 }
360
361 pub fn delivery_token(&self) -> &str {
363 &self.delivery_token
364 }
365}