allsource-core 0.19.1

High-performance event store core built in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Pagination request parameters
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PaginationRequest {
    /// Number of items to return (default: 20, max: 100)
    #[serde(default = "default_limit")]
    pub limit: usize,

    /// Number of items to skip (default: 0)
    #[serde(default)]
    pub offset: usize,
}

fn default_limit() -> usize {
    20
}

impl PaginationRequest {
    pub fn new(limit: usize, offset: usize) -> Self {
        Self {
            limit: limit.min(100),
            offset,
        }
    }

    /// Clamp limit to max 100
    pub fn clamped_limit(&self) -> usize {
        self.limit.min(100)
    }
}

/// Pagination metadata in responses
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginationResponse {
    /// Total number of items available
    pub total: usize,

    /// Number of items returned in this response
    pub count: usize,

    /// Current offset
    pub offset: usize,

    /// Limit used for this request
    pub limit: usize,

    /// Whether there are more items after this page
    pub has_more: bool,
}

impl PaginationResponse {
    pub fn new(total: usize, count: usize, offset: usize, limit: usize) -> Self {
        Self {
            total,
            count,
            offset,
            limit,
            has_more: offset + count < total,
        }
    }
}

/// Sort direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum SortDirection {
    #[default]
    Asc,
    Desc,
}

/// Generic sort request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SortRequest {
    /// Field to sort by
    pub field: String,

    /// Sort direction
    #[serde(default)]
    pub direction: SortDirection,
}

impl SortRequest {
    pub fn new(field: impl Into<String>, direction: SortDirection) -> Self {
        Self {
            field: field.into(),
            direction,
        }
    }

    pub fn asc(field: impl Into<String>) -> Self {
        Self::new(field, SortDirection::Asc)
    }

    pub fn desc(field: impl Into<String>) -> Self {
        Self::new(field, SortDirection::Desc)
    }
}

/// Time range filter for queries
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TimeRangeFilter {
    /// Start of time range (inclusive)
    pub since: Option<DateTime<Utc>>,

    /// End of time range (inclusive)
    pub until: Option<DateTime<Utc>>,

    /// Point-in-time query (time-travel)
    pub as_of: Option<DateTime<Utc>>,
}

impl TimeRangeFilter {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn since(mut self, since: DateTime<Utc>) -> Self {
        self.since = Some(since);
        self
    }

    pub fn until(mut self, until: DateTime<Utc>) -> Self {
        self.until = Some(until);
        self
    }

    pub fn as_of(mut self, as_of: DateTime<Utc>) -> Self {
        self.as_of = Some(as_of);
        self
    }

    /// Check if the filter has any time constraints
    pub fn has_constraints(&self) -> bool {
        self.since.is_some() || self.until.is_some() || self.as_of.is_some()
    }
}

/// Error severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorSeverity {
    Warning,
    Error,
    Critical,
}

/// Error code categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
    // Validation errors (400)
    ValidationError,
    InvalidInput,
    MissingField,
    InvalidFormat,

    // Authentication errors (401)
    Unauthorized,
    InvalidToken,
    TokenExpired,

    // Authorization errors (403)
    Forbidden,
    InsufficientPermissions,
    QuotaExceeded,

    // Not found errors (404)
    NotFound,
    EntityNotFound,
    ResourceNotFound,

    // Conflict errors (409)
    Conflict,
    DuplicateEntry,
    ConcurrencyConflict,
    VersionMismatch,

    // Rate limiting (429)
    RateLimited,
    TooManyRequests,

    // Server errors (500)
    InternalError,
    DatabaseError,
    ServiceUnavailable,
}

impl ErrorCode {
    /// Get the HTTP status code for this error code
    pub fn http_status(&self) -> u16 {
        match self {
            ErrorCode::ValidationError
            | ErrorCode::InvalidInput
            | ErrorCode::MissingField
            | ErrorCode::InvalidFormat => 400,

            ErrorCode::Unauthorized | ErrorCode::InvalidToken | ErrorCode::TokenExpired => 401,

            ErrorCode::Forbidden
            | ErrorCode::InsufficientPermissions
            | ErrorCode::QuotaExceeded => 403,

            ErrorCode::NotFound | ErrorCode::EntityNotFound | ErrorCode::ResourceNotFound => 404,

            ErrorCode::Conflict
            | ErrorCode::DuplicateEntry
            | ErrorCode::ConcurrencyConflict
            | ErrorCode::VersionMismatch => 409,

            ErrorCode::RateLimited | ErrorCode::TooManyRequests => 429,

            ErrorCode::InternalError | ErrorCode::DatabaseError | ErrorCode::ServiceUnavailable => {
                500
            }
        }
    }
}

/// Field-level validation error
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldError {
    /// Field name that has the error
    pub field: String,

    /// Error message for this field
    pub message: String,

    /// Error code for this field error
    pub code: Option<String>,
}

impl FieldError {
    pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            field: field.into(),
            message: message.into(),
            code: None,
        }
    }

    pub fn with_code(mut self, code: impl Into<String>) -> Self {
        self.code = Some(code.into());
        self
    }
}

/// Standard error response DTO for API errors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorResponse {
    /// Error code
    pub code: ErrorCode,

    /// Human-readable error message
    pub message: String,

    /// Additional details about the error
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<String>,

    /// Field-level validation errors
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub field_errors: Vec<FieldError>,

    /// Request ID for tracing
    #[serde(skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,

    /// Timestamp of the error
    pub timestamp: DateTime<Utc>,
}

impl ErrorResponse {
    pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            details: None,
            field_errors: Vec::new(),
            request_id: None,
            timestamp: Utc::now(),
        }
    }

    pub fn with_details(mut self, details: impl Into<String>) -> Self {
        self.details = Some(details.into());
        self
    }

    pub fn with_field_errors(mut self, errors: Vec<FieldError>) -> Self {
        self.field_errors = errors;
        self
    }

    pub fn with_request_id(mut self, request_id: impl Into<String>) -> Self {
        self.request_id = Some(request_id.into());
        self
    }

    /// Create a validation error response
    pub fn validation_error(message: impl Into<String>) -> Self {
        Self::new(ErrorCode::ValidationError, message)
    }

    /// Create a not found error response
    pub fn not_found(entity: impl Into<String>) -> Self {
        Self::new(ErrorCode::NotFound, format!("{} not found", entity.into()))
    }

    /// Create a conflict error response
    pub fn conflict(message: impl Into<String>) -> Self {
        Self::new(ErrorCode::Conflict, message)
    }

    /// Create an internal error response
    pub fn internal_error() -> Self {
        Self::new(ErrorCode::InternalError, "An internal error occurred")
    }
}

/// Success response wrapper with optional metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuccessResponse<T> {
    /// The response data
    pub data: T,

    /// Optional metadata about the response
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<ResponseMeta>,
}

impl<T> SuccessResponse<T> {
    pub fn new(data: T) -> Self {
        Self { data, meta: None }
    }

    pub fn with_meta(mut self, meta: ResponseMeta) -> Self {
        self.meta = Some(meta);
        self
    }
}

/// Response metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseMeta {
    /// Request processing time in milliseconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub processing_time_ms: Option<u64>,

    /// API version
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_version: Option<String>,

    /// Deprecation warning if applicable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecation_warning: Option<String>,
}

impl ResponseMeta {
    pub fn new() -> Self {
        Self {
            processing_time_ms: None,
            api_version: None,
            deprecation_warning: None,
        }
    }

    pub fn with_processing_time(mut self, ms: u64) -> Self {
        self.processing_time_ms = Some(ms);
        self
    }
}

impl Default for ResponseMeta {
    fn default() -> Self {
        Self::new()
    }
}

/// Paginated list response wrapper
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginatedResponse<T> {
    /// The list of items
    pub items: Vec<T>,

    /// Pagination metadata
    pub pagination: PaginationResponse,
}

impl<T> PaginatedResponse<T> {
    pub fn new(items: Vec<T>, total: usize, offset: usize, limit: usize) -> Self {
        let count = items.len();
        Self {
            items,
            pagination: PaginationResponse::new(total, count, offset, limit),
        }
    }

    pub fn empty() -> Self {
        Self {
            items: Vec::new(),
            pagination: PaginationResponse::new(0, 0, 0, 20),
        }
    }
}

/// Batch operation result for individual items
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchItemResult<T> {
    /// Index of the item in the original batch
    pub index: usize,

    /// Whether this item was processed successfully
    pub success: bool,

    /// The result data if successful
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<T>,

    /// Error information if failed
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<ErrorResponse>,
}

impl<T> BatchItemResult<T> {
    pub fn success(index: usize, data: T) -> Self {
        Self {
            index,
            success: true,
            data: Some(data),
            error: None,
        }
    }

    pub fn failure(index: usize, error: ErrorResponse) -> Self {
        Self {
            index,
            success: false,
            data: None,
            error: Some(error),
        }
    }
}

/// Batch operation response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchResponse<T> {
    /// Results for each item in the batch
    pub results: Vec<BatchItemResult<T>>,

    /// Total items in the batch
    pub total: usize,

    /// Number of successful operations
    pub successful: usize,

    /// Number of failed operations
    pub failed: usize,
}

impl<T> BatchResponse<T> {
    pub fn new(results: Vec<BatchItemResult<T>>) -> Self {
        let total = results.len();
        let successful = results.iter().filter(|r| r.success).count();
        let failed = total - successful;

        Self {
            results,
            total,
            successful,
            failed,
        }
    }

    /// Check if all operations succeeded
    pub fn all_successful(&self) -> bool {
        self.failed == 0
    }
}

/// Health check response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthResponse {
    /// Overall health status
    pub status: HealthStatus,

    /// Service version
    pub version: String,

    /// Uptime in seconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uptime_seconds: Option<u64>,

    /// Component health checks
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub components: Vec<ComponentHealth>,
}

/// Health status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HealthStatus {
    Healthy,
    Degraded,
    Unhealthy,
}

/// Component health status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentHealth {
    /// Component name
    pub name: String,

    /// Component status
    pub status: HealthStatus,

    /// Optional message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,

    /// Response time in milliseconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_time_ms: Option<u64>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pagination_request_clamping() {
        let pagination = PaginationRequest::new(200, 0);
        assert_eq!(pagination.clamped_limit(), 100);
    }

    #[test]
    fn test_pagination_response_has_more() {
        let response = PaginationResponse::new(100, 20, 0, 20);
        assert!(response.has_more);

        let response = PaginationResponse::new(100, 20, 80, 20);
        assert!(!response.has_more);
    }

    #[test]
    fn test_error_code_http_status() {
        assert_eq!(ErrorCode::ValidationError.http_status(), 400);
        assert_eq!(ErrorCode::Unauthorized.http_status(), 401);
        assert_eq!(ErrorCode::Forbidden.http_status(), 403);
        assert_eq!(ErrorCode::NotFound.http_status(), 404);
        assert_eq!(ErrorCode::Conflict.http_status(), 409);
        assert_eq!(ErrorCode::RateLimited.http_status(), 429);
        assert_eq!(ErrorCode::InternalError.http_status(), 500);
    }

    #[test]
    fn test_error_response_serialization() {
        let error =
            ErrorResponse::validation_error("Invalid email format").with_field_errors(vec![
                FieldError::new("email", "must be a valid email address"),
            ]);

        let json = serde_json::to_string(&error).unwrap();
        assert!(json.contains("VALIDATION_ERROR"));
        assert!(json.contains("email"));
    }

    #[test]
    fn test_time_range_filter() {
        let filter = TimeRangeFilter::new();
        assert!(!filter.has_constraints());

        let filter = filter.since(Utc::now());
        assert!(filter.has_constraints());
    }

    #[test]
    fn test_paginated_response() {
        let items = vec!["a", "b", "c"];
        let response = PaginatedResponse::new(items, 10, 0, 3);

        assert_eq!(response.items.len(), 3);
        assert_eq!(response.pagination.total, 10);
        assert!(response.pagination.has_more);
    }

    #[test]
    fn test_batch_response() {
        let results = vec![
            BatchItemResult::success(0, "item1"),
            BatchItemResult::failure(1, ErrorResponse::validation_error("Invalid")),
            BatchItemResult::success(2, "item3"),
        ];

        let batch = BatchResponse::new(results);
        assert_eq!(batch.total, 3);
        assert_eq!(batch.successful, 2);
        assert_eq!(batch.failed, 1);
        assert!(!batch.all_successful());
    }
}