1use crate::{u64_to_i64, unix_timestamp_millis};
4
5pub const DEFAULT_QUEUE_LEASE_MS: u64 = 30_000;
7
8#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
10pub struct ObjectKey {
11 pub collection: String,
13 pub id: String,
15}
16
17impl ObjectKey {
18 pub fn new(collection: impl Into<String>, id: impl Into<String>) -> Self {
20 Self {
21 collection: collection.into(),
22 id: id.into(),
23 }
24 }
25}
26
27#[derive(Clone, Debug, Eq, PartialEq)]
29pub struct MemoryObject {
30 pub key: ObjectKey,
32 pub body: String,
34 pub version: u64,
36 pub created_at: String,
38 pub updated_at: String,
40}
41
42impl MemoryObject {
43 pub fn new(
45 collection: impl Into<String>,
46 id: impl Into<String>,
47 body: impl Into<String>,
48 ) -> Self {
49 Self {
50 key: ObjectKey::new(collection, id),
51 body: body.into(),
52 version: 0,
53 created_at: String::new(),
54 updated_at: String::new(),
55 }
56 }
57}
58
59#[derive(Clone, Debug, Eq, PartialEq)]
61pub struct MemoryEvent {
62 pub stream: String,
64 pub event_type: String,
66 pub body: String,
68 pub sequence: u64,
70 pub created_at: String,
72}
73
74impl MemoryEvent {
75 pub fn new(
77 stream: impl Into<String>,
78 event_type: impl Into<String>,
79 body: impl Into<String>,
80 ) -> Self {
81 Self {
82 stream: stream.into(),
83 event_type: event_type.into(),
84 body: body.into(),
85 sequence: 0,
86 created_at: String::new(),
87 }
88 }
89}
90
91#[derive(Clone, Copy, Debug, Eq, PartialEq)]
93pub enum QueueJobStatus {
94 Ready,
96 Leased,
98 Completed,
100 Dead,
102}
103
104#[derive(Clone, Debug, Eq, PartialEq)]
106pub struct QueueJob {
107 pub queue: String,
109 pub id: String,
111 pub body: String,
113 pub attempts: u32,
115 pub max_attempts: u32,
117 pub status: QueueJobStatus,
119 pub available_at_ms: i64,
121 pub leased_at_ms: Option<i64>,
123 pub lease_expires_at_ms: Option<i64>,
125 pub completed_at_ms: Option<i64>,
127 pub dead_at_ms: Option<i64>,
129 pub created_at: String,
131 pub last_error: String,
133}
134
135impl QueueJob {
136 pub fn new(
138 queue: impl Into<String>,
139 id: impl Into<String>,
140 body: impl Into<String>,
141 max_attempts: u32,
142 ) -> Self {
143 Self {
144 queue: queue.into(),
145 id: id.into(),
146 body: body.into(),
147 attempts: 0,
148 max_attempts,
149 status: QueueJobStatus::Ready,
150 available_at_ms: 0,
151 leased_at_ms: None,
152 lease_expires_at_ms: None,
153 completed_at_ms: None,
154 dead_at_ms: None,
155 created_at: String::new(),
156 last_error: String::new(),
157 }
158 }
159
160 #[must_use]
162 pub fn delay_by_ms(mut self, delay_ms: u64) -> Self {
163 self.available_at_ms = unix_timestamp_millis().saturating_add(u64_to_i64(delay_ms));
164 self
165 }
166
167 #[must_use]
169 pub const fn available_at_ms(mut self, available_at_ms: i64) -> Self {
170 self.available_at_ms = available_at_ms;
171 self
172 }
173}
174
175#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
177pub struct ListEventsOptions {
178 pub from_sequence: Option<u64>,
180 pub limit: Option<u64>,
182}
183
184#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
186pub enum SortDirection {
187 #[default]
189 Asc,
190 Desc,
192}
193
194#[derive(Clone, Debug, Eq, PartialEq)]
196pub struct SortBy {
197 pub field: String,
199 pub direction: SortDirection,
201}
202
203impl SortBy {
204 pub fn asc(field: impl Into<String>) -> Self {
206 Self {
207 field: field.into(),
208 direction: SortDirection::Asc,
209 }
210 }
211
212 pub fn desc(field: impl Into<String>) -> Self {
214 Self {
215 field: field.into(),
216 direction: SortDirection::Desc,
217 }
218 }
219}
220
221#[derive(Clone, Debug, Default)]
223pub struct ListObjectsOptions {
224 pub filter: Vec<(String, serde_json::Value)>,
228 pub sort_by: Option<SortBy>,
230 pub limit: Option<u64>,
232 pub offset: Option<u64>,
234}
235
236#[derive(Clone, Copy, Debug, Eq, PartialEq)]
238pub struct PutObjectOptions {
239 pub index: bool,
243}
244
245impl Default for PutObjectOptions {
246 fn default() -> Self {
247 Self { index: true }
248 }
249}
250
251#[derive(Clone, Copy, Debug, Eq, PartialEq)]
253pub struct QueueClaimOptions {
254 pub lease_ms: u64,
256}
257
258impl Default for QueueClaimOptions {
259 fn default() -> Self {
260 Self {
261 lease_ms: DEFAULT_QUEUE_LEASE_MS,
262 }
263 }
264}
265
266impl QueueClaimOptions {
267 #[must_use]
269 pub const fn new(lease_ms: u64) -> Self {
270 Self { lease_ms }
271 }
272}
273
274#[derive(Clone, Debug, Eq, PartialEq, Default)]
276pub struct QueueNackOptions {
277 pub delay_ms: u64,
279 pub error: String,
281}
282
283impl QueueNackOptions {
284 #[must_use]
286 pub const fn new(delay_ms: u64) -> Self {
287 Self {
288 delay_ms,
289 error: String::new(),
290 }
291 }
292
293 #[must_use]
295 pub fn with_error(delay_ms: u64, error: impl Into<String>) -> Self {
296 Self {
297 delay_ms,
298 error: error.into(),
299 }
300 }
301}
302
303#[derive(Clone, Debug, Eq, PartialEq, Default)]
305pub struct SearchOptions {
306 pub collections: Option<Vec<String>>,
308 pub limit: Option<usize>,
310 pub filter: Option<serde_json::Value>,
312}
313
314#[derive(Clone, Debug, PartialEq)]
316pub struct SearchHit {
317 pub kind: String,
319 pub collection: String,
321 pub id: String,
323 pub text: String,
325 pub score: f64,
327 pub body: String,
329 pub version: Option<u64>,
331 pub created_at: String,
333 pub updated_at: Option<String>,
335 pub event_type: Option<String>,
337}
338
339#[derive(Clone, Debug, PartialEq)]
341pub struct Link {
342 pub id: String,
344 pub from_ref: String,
346 pub link_type: String,
348 pub to_ref: String,
350 pub weight: Option<f64>,
352 pub metadata_json: String,
354 pub created_at: String,
356}
357
358impl Link {
359 pub fn new(
361 from_ref: impl Into<String>,
362 link_type: impl Into<String>,
363 to_ref: impl Into<String>,
364 ) -> Self {
365 Self {
366 id: String::new(),
367 from_ref: from_ref.into(),
368 link_type: link_type.into(),
369 to_ref: to_ref.into(),
370 weight: None,
371 metadata_json: "{}".to_string(),
372 created_at: String::new(),
373 }
374 }
375
376 #[must_use]
378 pub const fn with_weight(mut self, weight: f64) -> Self {
379 self.weight = Some(weight);
380 self
381 }
382
383 #[must_use]
385 pub fn with_metadata(mut self, metadata: impl Into<String>) -> Self {
386 self.metadata_json = metadata.into();
387 self
388 }
389}
390
391#[derive(Clone, Debug, Default, Eq, PartialEq)]
393pub struct LinkQueryOptions {
394 pub link_type: Option<String>,
396 pub limit: Option<usize>,
398}
399
400#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
402pub enum LinkDirection {
403 Outgoing,
405 Incoming,
407 #[default]
409 Both,
410}