reinhardt-core 0.1.0

Core components for Reinhardt framework
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! Database-integrated cursor pagination for O(k) performance
//!
//! This module provides cursor-based pagination that integrates directly with
//! database queries, avoiding the O(n) cost of OFFSET/LIMIT by using indexed
//! cursor fields (id, timestamp) for efficient seek operations.

use base64::{Engine, engine::general_purpose::STANDARD};
use serde::{Deserialize, Serialize};

/// Cursor structure for database pagination
///
/// Contains the primary key and a tie-breaker timestamp to ensure stable
/// ordering even when multiple records share the same primary key.
///
/// # Examples
///
/// ```
/// use reinhardt_core::pagination::cursor::database::Cursor;
///
/// let cursor = Cursor::new(42, 1234567890);
/// let encoded = cursor.encode();
/// let decoded = Cursor::decode(&encoded).unwrap();
/// assert_eq!(decoded.id, 42);
/// assert_eq!(decoded.timestamp, 1234567890);
/// ```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Cursor {
	/// Primary key (id) of the record
	pub id: i64,
	/// Timestamp for tie-breaking when IDs are equal
	pub timestamp: i64,
}

impl Cursor {
	/// Create a new cursor with the given id and timestamp
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::pagination::cursor::database::Cursor;
	///
	/// let cursor = Cursor::new(100, 1609459200);
	/// assert_eq!(cursor.id, 100);
	/// assert_eq!(cursor.timestamp, 1609459200);
	/// ```
	pub fn new(id: i64, timestamp: i64) -> Self {
		Self { id, timestamp }
	}

	/// Encode the cursor to a base64 string
	///
	/// The cursor is serialized to JSON and then base64-encoded to create
	/// an opaque token that users cannot easily manipulate.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::pagination::cursor::database::Cursor;
	///
	/// let cursor = Cursor::new(42, 1234567890);
	/// let encoded = cursor.encode();
	/// assert!(!encoded.is_empty());
	/// ```
	pub fn encode(&self) -> String {
		let json = serde_json::to_string(self).expect("Failed to serialize cursor");
		STANDARD.encode(json.as_bytes())
	}

	/// Decode a base64-encoded cursor string
	///
	/// # Errors
	///
	/// Returns a `PaginationError::InvalidCursor` if:
	/// - The string is not valid base64
	/// - The decoded data is not valid JSON
	/// - The JSON does not match the Cursor structure
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::pagination::cursor::database::Cursor;
	///
	/// let cursor = Cursor::new(42, 1234567890);
	/// let encoded = cursor.encode();
	/// let decoded = Cursor::decode(&encoded).unwrap();
	/// assert_eq!(decoded, cursor);
	/// ```
	pub fn decode(cursor: &str) -> Result<Self, PaginationError> {
		let bytes = STANDARD
			.decode(cursor)
			.map_err(|e| PaginationError::InvalidCursor(format!("Base64 decode error: {}", e)))?;

		serde_json::from_slice(&bytes)
			.map_err(|e| PaginationError::InvalidCursor(format!("JSON parse error: {}", e)))
	}
}

/// Direction for cursor-based pagination
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
	/// Move forward through the dataset (next page)
	Forward,
	/// Move backward through the dataset (previous page)
	Backward,
}

/// Pagination errors specific to cursor operations
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PaginationError {
	/// Invalid cursor format or decoding error
	InvalidCursor(String),
	/// Database query error
	DatabaseError(String),
}

impl std::fmt::Display for PaginationError {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::InvalidCursor(msg) => write!(f, "Invalid cursor: {}", msg),
			Self::DatabaseError(msg) => write!(f, "Database error: {}", msg),
		}
	}
}

impl std::error::Error for PaginationError {}

/// Trait for models that have id and timestamp fields
///
/// This trait is required for cursor-based pagination to work efficiently.
/// It provides access to the primary key (id) and a timestamp field that
/// can be used as a tie-breaker for stable ordering.
///
/// # Examples
///
/// ```
/// use reinhardt_core::pagination::cursor::database::HasTimestamp;
///
/// #[derive(Clone)]
/// struct User {
///     id: i64,
///     created_at: i64,
///     name: String,
/// }
///
/// impl HasTimestamp for User {
///     fn id(&self) -> i64 {
///         self.id
///     }
///
///     fn timestamp(&self) -> i64 {
///         self.created_at
///     }
/// }
/// ```
pub trait HasTimestamp {
	/// Returns the primary key (id) of the record
	fn id(&self) -> i64;

	/// Returns the timestamp field value
	///
	/// This is used as a tie-breaker when multiple records have the same id,
	/// ensuring stable pagination order.
	fn timestamp(&self) -> i64;
}

/// Cursor-based paginator for efficient database pagination
///
/// This paginator uses id/timestamp-based cursors to achieve O(k) performance
/// instead of the O(n) cost of OFFSET/LIMIT pagination.
///
/// # Examples
///
/// ```
/// use reinhardt_core::pagination::cursor::database::{CursorPaginator, HasTimestamp};
///
/// #[derive(Clone)]
/// struct User {
///     id: i64,
///     created_at: i64,
///     name: String,
/// }
///
/// impl HasTimestamp for User {
///     fn id(&self) -> i64 { self.id }
///     fn timestamp(&self) -> i64 { self.created_at }
/// }
///
/// let users = vec![
///     User { id: 1, created_at: 1000, name: "Alice".to_string() },
///     User { id: 2, created_at: 2000, name: "Bob".to_string() },
/// ];
///
/// let paginator = CursorPaginator::new(10);
/// // let page = paginator.paginate(users, None);
/// ```
pub struct CursorPaginator {
	page_size: usize,
}

impl CursorPaginator {
	/// Create a new cursor paginator with the given page size
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::pagination::cursor::database::CursorPaginator;
	///
	/// let paginator = CursorPaginator::new(20);
	/// ```
	pub fn new(page_size: usize) -> Self {
		Self { page_size }
	}

	/// Paginate a collection of items using cursor-based pagination
	///
	/// # Arguments
	///
	/// * `items` - The collection to paginate (must implement HasTimestamp)
	/// * `cursor` - Optional cursor string from previous page
	///
	/// # Returns
	///
	/// A `CursorPaginatedResponse` containing:
	/// - `results`: The items for this page
	/// - `next_cursor`: Cursor for next page (if available)
	/// - `prev_cursor`: Cursor for previous page (if available)
	/// - `has_next`: Whether there is a next page
	/// - `has_prev`: Whether there is a previous page
	///
	/// # Performance
	///
	/// For in-memory slices, this is O(n) for finding the cursor position.
	/// For database queries with proper indexes, this becomes O(k) where k is page_size.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::pagination::cursor::database::{CursorPaginator, HasTimestamp};
	///
	/// #[derive(Clone)]
	/// struct Item {
	///     id: i64,
	///     timestamp: i64,
	/// }
	///
	/// impl HasTimestamp for Item {
	///     fn id(&self) -> i64 { self.id }
	///     fn timestamp(&self) -> i64 { self.timestamp }
	/// }
	///
	/// let items = vec![
	///     Item { id: 1, timestamp: 100 },
	///     Item { id: 2, timestamp: 200 },
	///     Item { id: 3, timestamp: 300 },
	/// ];
	///
	/// let paginator = CursorPaginator::new(2);
	/// let page1 = paginator.paginate(&items, None).unwrap();
	///
	/// assert_eq!(page1.results.len(), 2);
	/// assert!(page1.has_next);
	/// assert!(!page1.has_prev);
	/// ```
	pub fn paginate<T>(
		&self,
		items: &[T],
		cursor: Option<String>,
	) -> Result<CursorPaginatedResponse<T>, PaginationError>
	where
		T: HasTimestamp + Clone,
	{
		// Decode cursor if provided
		let start_pos = if let Some(cursor_str) = cursor {
			let cursor = Cursor::decode(&cursor_str)?;
			// Find the position after the cursor
			items
				.iter()
				.position(|item| {
					item.id() > cursor.id
						|| (item.id() == cursor.id && item.timestamp() > cursor.timestamp)
				})
				.unwrap_or(items.len())
		} else {
			0
		};

		// Get page_size + 1 items to check if there's a next page
		let end_pos = std::cmp::min(start_pos + self.page_size + 1, items.len());
		let page_items = &items[start_pos..end_pos];

		let has_next = page_items.len() > self.page_size;
		let results: Vec<T> = page_items.iter().take(self.page_size).cloned().collect();

		// Generate next cursor
		let next_cursor = if has_next && !results.is_empty() {
			let last = results.last().unwrap();
			Some(Cursor::new(last.id(), last.timestamp()).encode())
		} else {
			None
		};

		// Generate previous cursor
		let prev_cursor = if start_pos > 0 && !results.is_empty() {
			let first = results.first().unwrap();
			Some(Cursor::new(first.id(), first.timestamp()).encode())
		} else {
			None
		};

		Ok(CursorPaginatedResponse {
			results,
			next_cursor,
			prev_cursor,
			has_next,
			has_prev: start_pos > 0,
		})
	}
}

/// Paginated response with cursor navigation
///
/// Contains the results and metadata for navigating through pages using cursors.
///
/// # Examples
///
/// ```
/// use reinhardt_core::pagination::cursor::database::CursorPaginatedResponse;
///
/// let response: CursorPaginatedResponse<i32> = CursorPaginatedResponse {
///     results: vec![1, 2, 3],
///     next_cursor: Some("encoded_cursor".to_string()),
///     prev_cursor: None,
///     has_next: true,
///     has_prev: false,
/// };
///
/// assert_eq!(response.results.len(), 3);
/// assert!(response.has_next);
/// assert!(!response.has_prev);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CursorPaginatedResponse<T> {
	/// The items on this page
	pub results: Vec<T>,
	/// Cursor for the next page (if available)
	pub next_cursor: Option<String>,
	/// Cursor for the previous page (if available)
	pub prev_cursor: Option<String>,
	/// Whether there is a next page
	pub has_next: bool,
	/// Whether there is a previous page
	pub has_prev: bool,
}

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

	#[test]
	fn test_cursor_new() {
		let cursor = Cursor::new(42, 1234567890);
		assert_eq!(cursor.id, 42);
		assert_eq!(cursor.timestamp, 1234567890);
	}

	#[test]
	fn test_cursor_encode_decode() {
		let cursor = Cursor::new(100, 9876543210);
		let encoded = cursor.encode();

		// Encoded cursor should be non-empty and base64
		assert!(!encoded.is_empty());

		// Should decode back to original
		let decoded = Cursor::decode(&encoded).unwrap();
		assert_eq!(decoded, cursor);
	}

	#[test]
	fn test_cursor_decode_invalid_base64() {
		let result = Cursor::decode("not-valid-base64!!!");
		assert!(result.is_err());
		match result {
			Err(PaginationError::InvalidCursor(msg)) => {
				assert!(
					msg.starts_with("Base64 decode error:"),
					"Cursor decode should return base64 error message. Got: {}",
					msg
				);
			}
			_ => panic!("Expected InvalidCursor error"),
		}
	}

	#[test]
	fn test_cursor_decode_invalid_json() {
		// Valid base64 but invalid JSON
		let invalid = STANDARD.encode(b"not json");
		let result = Cursor::decode(&invalid);
		assert!(result.is_err());
		match result {
			Err(PaginationError::InvalidCursor(msg)) => {
				assert!(
					msg.starts_with("JSON parse error:"),
					"Cursor decode should return JSON parse error message. Got: {}",
					msg
				);
			}
			_ => panic!("Expected InvalidCursor error"),
		}
	}

	#[test]
	fn test_cursor_decode_malformed_json() {
		// Valid JSON but wrong structure
		let invalid_json = serde_json::json!({"wrong": "structure"});
		let invalid = STANDARD.encode(invalid_json.to_string().as_bytes());
		let result = Cursor::decode(&invalid);
		assert!(result.is_err());
	}

	#[test]
	fn test_cursor_roundtrip_edge_cases() {
		// Test with edge case values
		let test_cases = vec![
			Cursor::new(0, 0),
			Cursor::new(i64::MAX, i64::MAX),
			Cursor::new(i64::MIN, i64::MIN),
			Cursor::new(-1, -1),
		];

		for cursor in test_cases {
			let encoded = cursor.encode();
			let decoded = Cursor::decode(&encoded).unwrap();
			assert_eq!(decoded, cursor);
		}
	}

	#[test]
	fn test_direction() {
		assert_eq!(Direction::Forward, Direction::Forward);
		assert_eq!(Direction::Backward, Direction::Backward);
		assert_ne!(Direction::Forward, Direction::Backward);
	}

	#[test]
	fn test_pagination_error_display() {
		let err1 = PaginationError::InvalidCursor("bad cursor".to_string());
		assert_eq!(format!("{}", err1), "Invalid cursor: bad cursor");

		let err2 = PaginationError::DatabaseError("connection failed".to_string());
		assert_eq!(format!("{}", err2), "Database error: connection failed");
	}

	#[test]
	fn test_cursor_opaque() {
		// Ensure cursor is not easily human-readable
		let cursor = Cursor::new(42, 1234567890);
		let encoded = cursor.encode();

		// Should not contain raw values
		assert!(!encoded.contains("42"));
		assert!(!encoded.contains("1234567890"));
	}

	#[test]
	fn test_cursor_paginated_response() {
		let response: CursorPaginatedResponse<i32> = CursorPaginatedResponse {
			results: vec![1, 2, 3, 4, 5],
			next_cursor: Some("next".to_string()),
			prev_cursor: Some("prev".to_string()),
			has_next: true,
			has_prev: true,
		};

		assert_eq!(response.results.len(), 5);
		assert!(response.next_cursor.is_some());
		assert!(response.prev_cursor.is_some());
		assert!(response.has_next);
		assert!(response.has_prev);
	}

	// ========================================
	// HasTimestamp and CursorPaginator Tests
	// ========================================

	#[derive(Debug, Clone, PartialEq)]
	struct TestItem {
		id: i64,
		timestamp: i64,
		name: String,
	}

	impl HasTimestamp for TestItem {
		fn id(&self) -> i64 {
			self.id
		}

		fn timestamp(&self) -> i64 {
			self.timestamp
		}
	}

	fn create_test_items(count: usize) -> Vec<TestItem> {
		(1..=count)
			.map(|i| TestItem {
				id: i as i64,
				timestamp: (i as i64) * 1000,
				name: format!("Item {}", i),
			})
			.collect()
	}

	#[test]
	fn test_cursor_paginator_first_page() {
		let items = create_test_items(25);
		let paginator = CursorPaginator::new(10);

		let page = paginator.paginate(&items, None).unwrap();

		assert_eq!(page.results.len(), 10);
		assert_eq!(page.results[0].id, 1);
		assert_eq!(page.results[9].id, 10);
		assert!(page.has_next);
		assert!(!page.has_prev);
		assert!(page.next_cursor.is_some());
		assert!(page.prev_cursor.is_none());
	}

	#[test]
	fn test_cursor_paginator_navigation() {
		let items = create_test_items(25);
		let paginator = CursorPaginator::new(10);

		// First page
		let page1 = paginator.paginate(&items, None).unwrap();
		assert_eq!(page1.results.len(), 10);
		assert!(page1.has_next);
		assert!(!page1.has_prev);

		// Second page using cursor from page1
		let cursor = page1.next_cursor.unwrap();
		let page2 = paginator.paginate(&items, Some(cursor)).unwrap();
		assert_eq!(page2.results.len(), 10);
		assert_eq!(page2.results[0].id, 11);
		assert!(page2.has_next);
		assert!(page2.has_prev);

		// Third page
		let cursor = page2.next_cursor.unwrap();
		let page3 = paginator.paginate(&items, Some(cursor)).unwrap();
		assert_eq!(page3.results.len(), 5);
		assert_eq!(page3.results[0].id, 21);
		assert!(!page3.has_next);
		assert!(page3.has_prev);
	}

	#[test]
	fn test_cursor_paginator_empty_list() {
		let items: Vec<TestItem> = vec![];
		let paginator = CursorPaginator::new(10);

		let page = paginator.paginate(&items, None).unwrap();

		assert_eq!(page.results.len(), 0);
		assert!(!page.has_next);
		assert!(!page.has_prev);
		assert!(page.next_cursor.is_none());
		assert!(page.prev_cursor.is_none());
	}

	#[test]
	fn test_cursor_paginator_single_page() {
		let items = create_test_items(5);
		let paginator = CursorPaginator::new(10);

		let page = paginator.paginate(&items, None).unwrap();

		assert_eq!(page.results.len(), 5);
		assert!(!page.has_next);
		assert!(!page.has_prev);
		assert!(page.next_cursor.is_none());
	}

	#[test]
	fn test_cursor_paginator_exact_page_size() {
		let items = create_test_items(10);
		let paginator = CursorPaginator::new(10);

		let page = paginator.paginate(&items, None).unwrap();

		assert_eq!(page.results.len(), 10);
		assert!(!page.has_next);
		assert!(!page.has_prev);
	}

	#[test]
	fn test_cursor_paginator_one_more_than_page_size() {
		let items = create_test_items(11);
		let paginator = CursorPaginator::new(10);

		let page1 = paginator.paginate(&items, None).unwrap();
		assert_eq!(page1.results.len(), 10);
		assert!(page1.has_next);

		let cursor = page1.next_cursor.unwrap();
		let page2 = paginator.paginate(&items, Some(cursor)).unwrap();
		assert_eq!(page2.results.len(), 1);
		assert!(!page2.has_next);
	}

	#[test]
	fn test_cursor_paginator_invalid_cursor() {
		let items = create_test_items(25);
		let paginator = CursorPaginator::new(10);

		let result = paginator.paginate(&items, Some("invalid_cursor".to_string()));
		assert!(result.is_err());
	}

	#[test]
	fn test_cursor_paginator_tie_breaker() {
		// Test items with same id but different timestamps
		let items = vec![
			TestItem {
				id: 1,
				timestamp: 1000,
				name: "Item 1a".to_string(),
			},
			TestItem {
				id: 1,
				timestamp: 2000,
				name: "Item 1b".to_string(),
			},
			TestItem {
				id: 2,
				timestamp: 3000,
				name: "Item 2".to_string(),
			},
		];

		let paginator = CursorPaginator::new(1);

		// First page
		let page1 = paginator.paginate(&items, None).unwrap();
		assert_eq!(page1.results.len(), 1);
		assert_eq!(page1.results[0].timestamp, 1000);

		// Second page - should get item with same id but higher timestamp
		let cursor = page1.next_cursor.unwrap();
		let page2 = paginator.paginate(&items, Some(cursor)).unwrap();
		assert_eq!(page2.results.len(), 1);
		assert_eq!(page2.results[0].timestamp, 2000);
	}

	#[test]
	fn test_cursor_stability() {
		// Verify cursor is stable and reproducible
		let items = create_test_items(10);
		let paginator = CursorPaginator::new(5);

		let page1_a = paginator.paginate(&items, None).unwrap();
		let page1_b = paginator.paginate(&items, None).unwrap();

		// Same cursor should be generated for the same position
		assert_eq!(page1_a.next_cursor, page1_b.next_cursor);

		// Navigate with cursor
		let cursor = page1_a.next_cursor.unwrap();
		let page2_a = paginator.paginate(&items, Some(cursor.clone())).unwrap();
		let page2_b = paginator.paginate(&items, Some(cursor)).unwrap();

		assert_eq!(page2_a.results, page2_b.results);
	}

	#[test]
	fn test_cursor_paginator_performance_vs_offset() {
		// This test demonstrates the theoretical performance difference
		// In practice, database indexes make the difference more pronounced

		let items = create_test_items(10000);
		let paginator = CursorPaginator::new(100);

		// Navigate to "deep" page (page 50)
		let mut cursor: Option<String> = None;
		for _ in 0..49 {
			let page = paginator.paginate(&items, cursor).unwrap();
			cursor = page.next_cursor;
		}

		let page50 = paginator.paginate(&items, cursor).unwrap();
		assert_eq!(page50.results[0].id, 4901);

		// With cursor pagination, each page fetch is independent
		// With OFFSET/LIMIT, each page gets progressively slower
	}
}