1use crate::{u64_to_i64, unix_timestamp_millis};
4
5pub const DEFAULT_QUEUE_LEASE_MS: u64 = 30_000;
7
8#[derive(
10 Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
11)]
12#[serde(rename_all = "camelCase")]
13pub struct ObjectKey {
14 pub collection: String,
16 pub id: String,
18}
19
20impl ObjectKey {
21 pub fn new(collection: impl Into<String>, id: impl Into<String>) -> Self {
23 Self {
24 collection: collection.into(),
25 id: id.into(),
26 }
27 }
28}
29
30#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
32#[serde(rename_all = "camelCase")]
33pub struct MemoryObject {
34 pub key: ObjectKey,
36 pub body: String,
38 pub version: u64,
40 pub created_at: String,
42 pub updated_at: String,
44}
45
46impl MemoryObject {
47 pub fn new(
49 collection: impl Into<String>,
50 id: impl Into<String>,
51 body: impl Into<String>,
52 ) -> Self {
53 Self {
54 key: ObjectKey::new(collection, id),
55 body: body.into(),
56 version: 0,
57 created_at: String::new(),
58 updated_at: String::new(),
59 }
60 }
61}
62
63#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
65#[serde(rename_all = "camelCase")]
66pub struct MemoryEvent {
67 pub stream: String,
69 pub event_type: String,
71 pub body: String,
73 pub sequence: u64,
75 pub created_at: String,
77 pub idempotency_key: String,
81}
82
83impl MemoryEvent {
84 pub fn new(
86 stream: impl Into<String>,
87 event_type: impl Into<String>,
88 body: impl Into<String>,
89 ) -> Self {
90 Self {
91 stream: stream.into(),
92 event_type: event_type.into(),
93 body: body.into(),
94 sequence: 0,
95 created_at: String::new(),
96 idempotency_key: String::new(),
97 }
98 }
99}
100
101#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
103#[serde(rename_all = "camelCase")]
104pub enum QueueJobStatus {
105 Ready,
107 Leased,
109 Completed,
111 Dead,
113}
114
115#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
117#[serde(rename_all = "camelCase")]
118pub struct QueueJob {
119 pub queue: String,
121 pub id: String,
123 pub body: String,
125 pub attempts: u32,
127 pub max_attempts: u32,
129 pub status: QueueJobStatus,
131 pub available_at_ms: i64,
133 pub leased_at_ms: Option<i64>,
135 pub lease_expires_at_ms: Option<i64>,
137 pub completed_at_ms: Option<i64>,
139 pub dead_at_ms: Option<i64>,
141 pub created_at: String,
143 pub last_error: String,
145}
146
147impl QueueJob {
148 pub fn new(
150 queue: impl Into<String>,
151 id: impl Into<String>,
152 body: impl Into<String>,
153 max_attempts: u32,
154 ) -> Self {
155 Self {
156 queue: queue.into(),
157 id: id.into(),
158 body: body.into(),
159 attempts: 0,
160 max_attempts,
161 status: QueueJobStatus::Ready,
162 available_at_ms: 0,
163 leased_at_ms: None,
164 lease_expires_at_ms: None,
165 completed_at_ms: None,
166 dead_at_ms: None,
167 created_at: String::new(),
168 last_error: String::new(),
169 }
170 }
171
172 #[must_use]
174 pub fn delay_by_ms(mut self, delay_ms: u64) -> Self {
175 self.available_at_ms = unix_timestamp_millis().saturating_add(u64_to_i64(delay_ms));
176 self
177 }
178
179 #[must_use]
181 pub const fn available_at_ms(mut self, available_at_ms: i64) -> Self {
182 self.available_at_ms = available_at_ms;
183 self
184 }
185}
186
187#[derive(Clone, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
189#[serde(rename_all = "camelCase")]
190pub struct ListEventsOptions {
191 pub from_sequence: Option<u64>,
193 pub limit: Option<u64>,
195}
196
197#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
199#[serde(rename_all = "camelCase")]
200pub enum SortDirection {
201 #[default]
203 Asc,
204 Desc,
206}
207
208#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
210#[serde(rename_all = "camelCase")]
211pub struct SortBy {
212 pub field: String,
214 pub direction: SortDirection,
216}
217
218impl SortBy {
219 pub fn asc(field: impl Into<String>) -> Self {
221 Self {
222 field: field.into(),
223 direction: SortDirection::Asc,
224 }
225 }
226
227 pub fn desc(field: impl Into<String>) -> Self {
229 Self {
230 field: field.into(),
231 direction: SortDirection::Desc,
232 }
233 }
234}
235
236#[derive(Clone, Debug, Default)]
238pub struct ListObjectsOptions {
239 pub filter: Vec<(String, serde_json::Value)>,
243 pub sort_by: Option<SortBy>,
245 pub limit: Option<u64>,
247 pub offset: Option<u64>,
249}
250
251#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
253#[serde(rename_all = "camelCase")]
254pub struct PutObjectOptions {
255 pub index: bool,
259 pub expected_version: Option<u64>,
264}
265
266impl Default for PutObjectOptions {
267 fn default() -> Self {
268 Self {
269 index: true,
270 expected_version: None,
271 }
272 }
273}
274
275#[derive(Clone, Copy, Debug, Eq, PartialEq)]
277pub struct QueueClaimOptions {
278 pub lease_ms: u64,
280}
281
282impl Default for QueueClaimOptions {
283 fn default() -> Self {
284 Self {
285 lease_ms: DEFAULT_QUEUE_LEASE_MS,
286 }
287 }
288}
289
290impl QueueClaimOptions {
291 #[must_use]
293 pub const fn new(lease_ms: u64) -> Self {
294 Self { lease_ms }
295 }
296}
297
298#[derive(Clone, Debug, Eq, PartialEq, Default)]
300pub struct QueueNackOptions {
301 pub delay_ms: u64,
303 pub error: String,
305}
306
307impl QueueNackOptions {
308 #[must_use]
310 pub const fn new(delay_ms: u64) -> Self {
311 Self {
312 delay_ms,
313 error: String::new(),
314 }
315 }
316
317 #[must_use]
319 pub fn with_error(delay_ms: u64, error: impl Into<String>) -> Self {
320 Self {
321 delay_ms,
322 error: error.into(),
323 }
324 }
325}
326
327#[derive(Clone, Debug, Eq, PartialEq, Default)]
329pub struct SearchOptions {
330 pub collections: Option<Vec<String>>,
332 pub limit: Option<usize>,
334 pub filter: Option<serde_json::Value>,
336}
337
338#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
340#[serde(rename_all = "camelCase")]
341pub struct SearchHit {
342 pub kind: String,
344 pub collection: String,
346 pub id: String,
348 pub text: String,
350 pub score: f64,
352 pub body: String,
354 pub version: Option<u64>,
356 pub created_at: String,
358 pub updated_at: Option<String>,
360 pub event_type: Option<String>,
362}
363
364#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
366#[serde(rename_all = "camelCase")]
367pub struct Link {
368 pub id: String,
370 pub from_ref: String,
372 pub link_type: String,
374 pub to_ref: String,
376 pub weight: Option<f64>,
378 pub metadata_json: String,
380 pub created_at: String,
382}
383
384impl Link {
385 pub fn new(
387 from_ref: impl Into<String>,
388 link_type: impl Into<String>,
389 to_ref: impl Into<String>,
390 ) -> Self {
391 Self {
392 id: String::new(),
393 from_ref: from_ref.into(),
394 link_type: link_type.into(),
395 to_ref: to_ref.into(),
396 weight: None,
397 metadata_json: "{}".to_string(),
398 created_at: String::new(),
399 }
400 }
401
402 #[must_use]
404 pub const fn with_weight(mut self, weight: f64) -> Self {
405 self.weight = Some(weight);
406 self
407 }
408
409 #[must_use]
411 pub fn with_metadata(mut self, metadata: impl Into<String>) -> Self {
412 self.metadata_json = metadata.into();
413 self
414 }
415}
416
417#[derive(Clone, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
419#[serde(rename_all = "camelCase")]
420pub struct LinkQueryOptions {
421 pub link_type: Option<String>,
423 pub limit: Option<usize>,
425}
426
427#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
429#[serde(rename_all = "camelCase")]
430pub enum LinkDirection {
431 Outgoing,
433 Incoming,
435 #[default]
437 Both,
438}