reinhardt-urls 0.2.2

URL routing and proxy utilities 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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! Join operations for proxy relationships

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Configuration for join operations on proxy relationships.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JoinConfig {
	/// Whether to eagerly load the relationship.
	pub eager_load: bool,
	/// Maximum depth for nested relationship traversal.
	pub max_depth: Option<usize>,
	/// Strategy to use when loading the relationship.
	#[serde(skip)]
	pub loading_strategy: Option<crate::proxy::LoadingStrategy>,
	/// SQL join type (e.g., `"LEFT JOIN"`, `"INNER JOIN"`).
	pub join_type: Option<String>,
	/// SQL join condition expression.
	pub condition: Option<String>,
}

impl JoinConfig {
	/// Create a new `JoinConfig` with default values.
	pub fn new() -> Self {
		Self {
			eager_load: false,
			max_depth: None,
			loading_strategy: None,
			join_type: None,
			condition: None,
		}
	}

	/// Set the loading strategy (builder pattern)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::{JoinConfig, LoadingStrategy};
	///
	/// let config = JoinConfig::new()
	///     .with_loading_strategy(LoadingStrategy::Joined);
	/// ```
	pub fn with_loading_strategy(mut self, strategy: crate::proxy::LoadingStrategy) -> Self {
		self.loading_strategy = Some(strategy);
		self
	}

	/// Set the join type (builder pattern)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::JoinConfig;
	///
	/// let config = JoinConfig::new()
	///     .with_join_type("LEFT JOIN");
	/// ```
	pub fn with_join_type(mut self, join_type: &str) -> Self {
		self.join_type = Some(join_type.to_string());
		self
	}

	/// Set the join condition (builder pattern)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::JoinConfig;
	///
	/// let config = JoinConfig::new()
	///     .with_condition("users.id = posts.user_id");
	/// ```
	pub fn with_condition(mut self, condition: &str) -> Self {
		self.condition = Some(condition.to_string());
		self
	}
}

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

// LoadingStrategy is now re-exported from reinhardt-orm in lib.rs

/// A proxy for traversing nested relationships with conditions.
#[derive(Debug, Clone)]
pub struct NestedProxy {
	/// Sequence of relationship names forming the nested path.
	pub path: Vec<String>,
	/// Filter conditions applied along the path.
	pub conditions: Vec<String>,
	/// Final attribute to extract from the deepest relationship.
	pub final_attribute: Option<String>,
}

impl NestedProxy {
	/// Create a new empty nested proxy
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::NestedProxy;
	///
	/// let proxy = NestedProxy::new();
	/// assert_eq!(proxy.depth(), 0);
	/// ```
	pub fn new() -> Self {
		Self {
			path: Vec::new(),
			conditions: Vec::new(),
			final_attribute: None,
		}
	}

	/// Create from a path vector (for backward compatibility)
	pub fn from_path(path: Vec<String>) -> Self {
		Self {
			path,
			conditions: Vec::new(),
			final_attribute: None,
		}
	}

	/// Add a level to the nested path (builder pattern)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::NestedProxy;
	///
	/// let proxy = NestedProxy::new()
	///     .add_level("posts")
	///     .add_level("comments");
	/// assert_eq!(proxy.depth(), 2);
	/// ```
	pub fn add_level(mut self, level: &str) -> Self {
		self.path.push(level.to_string());
		self
	}

	/// Add a condition to the nested proxy (builder pattern)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::NestedProxy;
	///
	/// let proxy = NestedProxy::new()
	///     .add_level("posts")
	///     .with_condition("published = true");
	/// assert_eq!(proxy.conditions().len(), 1);
	/// ```
	pub fn with_condition(mut self, condition: &str) -> Self {
		self.conditions.push(condition.to_string());
		self
	}

	/// Set the final attribute to access (builder pattern)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::NestedProxy;
	///
	/// let proxy = NestedProxy::new()
	///     .add_level("posts")
	///     .with_attribute("title");
	/// assert_eq!(proxy.attribute(), "title");
	/// ```
	pub fn with_attribute(mut self, attr: &str) -> Self {
		self.final_attribute = Some(attr.to_string());
		self
	}

	/// Get the depth (number of levels) in the nested path
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::NestedProxy;
	///
	/// let proxy = NestedProxy::new()
	///     .add_level("posts")
	///     .add_level("comments")
	///     .add_level("author");
	/// assert_eq!(proxy.depth(), 3);
	/// ```
	pub fn depth(&self) -> usize {
		self.path.len()
	}

	/// Get the final attribute name
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::NestedProxy;
	///
	/// let proxy = NestedProxy::new()
	///     .add_level("posts")
	///     .with_attribute("title");
	/// assert_eq!(proxy.attribute(), "title");
	/// ```
	pub fn attribute(&self) -> &str {
		self.final_attribute.as_deref().unwrap_or("")
	}

	/// Get all conditions
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::NestedProxy;
	///
	/// let proxy = NestedProxy::new()
	///     .add_level("posts")
	///     .with_condition("published = true")
	///     .add_level("comments")
	///     .with_condition("approved = true");
	/// assert_eq!(proxy.conditions().len(), 2);
	/// ```
	pub fn conditions(&self) -> &[String] {
		&self.conditions
	}
}

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

/// A path through relationships with circular reference detection
///
/// RelationshipPath provides a builder API for constructing paths through
/// model relationships while detecting and preventing circular references.
///
/// # Examples
///
/// ```rust,no_run
/// # use reinhardt_urls::proxy::RelationshipPath;
/// // Valid path: user -> posts -> comments
/// let path = RelationshipPath::new()
///     .through("posts")
///     .through("comments")
///     .attribute("content");
///
/// // Circular path: posts -> author -> posts (ERROR)
/// let result = RelationshipPath::new()
///     .try_through("posts").unwrap()
///     .try_through("author").unwrap()
///     .try_through("posts");  // This creates a cycle
/// assert!(result.is_err());
/// ```
#[derive(Debug, Clone)]
pub struct RelationshipPath {
	/// Sequence of relationship names in the path
	pub segments: Vec<String>,
	/// Set of visited relationship names for cycle detection
	visited: HashSet<String>,
	/// Filters applied at each relationship level
	filters: HashMap<String, Vec<(String, String)>>,
	/// Transformations applied at each relationship level
	transforms: HashMap<String, Vec<(String, String)>>,
	/// Final attribute to access
	attribute: Option<String>,
}

impl RelationshipPath {
	/// Create a new empty relationship path
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::RelationshipPath;
	///
	/// let path = RelationshipPath::new();
	/// assert_eq!(path.path().len(), 0);
	/// ```
	pub fn new() -> Self {
		Self {
			segments: Vec::new(),
			visited: HashSet::new(),
			filters: HashMap::new(),
			transforms: HashMap::new(),
			attribute: None,
		}
	}

	/// Add a relationship to the path
	///
	/// Returns self for chaining. Use `try_through()` if you need error handling.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::RelationshipPath;
	///
	/// let path = RelationshipPath::new()
	///     .through("posts")
	///     .through("comments");
	/// assert_eq!(path.path().len(), 2);
	/// ```
	pub fn through(mut self, relationship: &str) -> Self {
		let rel = relationship.to_string();
		self.segments.push(rel.clone());
		self.visited.insert(rel);
		self
	}

	/// Add a relationship to the path with error handling for circular references
	///
	/// # Errors
	///
	/// Returns an error if adding this relationship would create a cycle.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::RelationshipPath;
	///
	/// let path = RelationshipPath::new()
	///     .try_through("posts").unwrap()
	///     .try_through("author").unwrap();
	///
	/// // This would create a cycle
	/// let result = path.try_through("posts");
	/// assert!(result.is_err());
	/// ```
	pub fn try_through(mut self, relationship: &str) -> Result<Self, CircularReferenceError> {
		let rel = relationship.to_string();

		if self.visited.contains(&rel) {
			return Err(CircularReferenceError {
				relationship: rel,
				path: self.segments.clone(),
			});
		}

		self.segments.push(rel.clone());
		self.visited.insert(rel);
		Ok(self)
	}

	/// Add a filter at the current relationship level
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::RelationshipPath;
	///
	/// let path = RelationshipPath::new()
	///     .through("posts")
	///     .with_filter("published", "true");
	/// assert!(path.has_filters());
	/// ```
	pub fn with_filter(mut self, field: &str, value: &str) -> Self {
		let current_rel = self.segments.last().cloned().unwrap_or_default();
		self.filters
			.entry(current_rel)
			.or_default()
			.push((field.to_string(), value.to_string()));
		self
	}

	/// Add a transformation at a specific relationship level
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::RelationshipPath;
	///
	/// let path = RelationshipPath::new()
	///     .through("posts")
	///     .through("comments")
	///     .with_transform("author", "upper");
	/// assert!(path.has_transforms());
	/// ```
	pub fn with_transform(mut self, relationship: &str, transform: &str) -> Self {
		self.transforms
			.entry(relationship.to_string())
			.or_default()
			.push((relationship.to_string(), transform.to_string()));
		self
	}

	/// Set the final attribute to access
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::RelationshipPath;
	///
	/// let path = RelationshipPath::new()
	///     .through("posts")
	///     .through("comments")
	///     .attribute("content");
	/// assert_eq!(path.get_attribute(), "content");
	/// ```
	pub fn attribute(mut self, attr: &str) -> Self {
		self.attribute = Some(attr.to_string());
		self
	}

	/// Get the path segments
	pub fn path(&self) -> &[String] {
		&self.segments
	}

	/// Get the final attribute name
	pub fn get_attribute(&self) -> &str {
		self.attribute.as_deref().unwrap_or("")
	}

	/// Check if any filters are configured
	pub fn has_filters(&self) -> bool {
		!self.filters.is_empty()
	}

	/// Get all filters
	pub fn filters(&self) -> Vec<(String, String)> {
		self.filters
			.values()
			.flat_map(|v| v.iter().cloned())
			.collect()
	}

	/// Check if any transformations are configured
	pub fn has_transforms(&self) -> bool {
		!self.transforms.is_empty()
	}

	/// Get all transformations
	pub fn transforms(&self) -> Vec<(String, String)> {
		self.transforms
			.values()
			.flat_map(|v| v.iter().cloned())
			.collect()
	}

	/// Check if a relationship is in the path (for cycle detection)
	pub fn contains(&self, relationship: &str) -> bool {
		self.visited.contains(relationship)
	}

	/// Validate the path and return self or error
	///
	/// This method is primarily for testing - the builder methods
	/// already prevent invalid paths from being constructed.
	pub fn validate(self) -> Result<Self, CircularReferenceError> {
		// Path is already validated during construction
		Ok(self)
	}
}

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

/// Error returned when a circular reference is detected in a relationship path
#[derive(Debug, Clone, thiserror::Error)]
#[error("Circular reference detected: relationship '{relationship}' already exists in path {}", path_display(.path))]
pub struct CircularReferenceError {
	/// The relationship that would create a cycle
	pub relationship: String,
	/// The current path when the cycle was detected
	pub path: Vec<String>,
}

fn path_display(path: &[String]) -> String {
	if path.is_empty() {
		"(empty)".to_string()
	} else {
		format!("[{}]", path.join(" -> "))
	}
}

/// Extract relationship segments from a dot-separated path string.
pub fn extract_through_path(path: &str) -> Vec<String> {
	path.split('.').map(|s| s.to_string()).collect()
}

/// Check if any segment in a `RelationshipPath` matches the given predicate.
pub fn filter_through_path(path: &RelationshipPath, predicate: impl Fn(&str) -> bool) -> bool {
	path.segments.iter().any(|s| predicate(s))
}

/// Extract the path segments from a `NestedProxy`.
pub fn traverse_and_extract(proxy: &NestedProxy) -> Vec<String> {
	proxy.path.clone()
}

/// Extract the path segments from a `RelationshipPath`.
pub fn traverse_relationships(path: &RelationshipPath) -> Vec<String> {
	path.segments.clone()
}

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

	/// Test basic path construction without cycles
	#[test]
	fn test_relationship_path_no_cycle() {
		let path = RelationshipPath::new()
			.through("posts")
			.through("comments")
			.through("author")
			.attribute("name");

		assert_eq!(path.path().len(), 3);
		assert_eq!(path.path()[0], "posts");
		assert_eq!(path.path()[1], "comments");
		assert_eq!(path.path()[2], "author");
		assert_eq!(path.get_attribute(), "name");
	}

	/// Test simple cycle detection: A -> B -> A
	#[test]
	fn test_simple_cycle_detection() {
		let path = RelationshipPath::new().through("posts").through("author");

		// Try to add "posts" again - should create a cycle
		let result = path.try_through("posts");

		assert!(result.is_err());
		let err = result.unwrap_err();
		assert_eq!(err.relationship, "posts");
		assert_eq!(err.path, vec!["posts", "author"]);
	}

	/// Test complex cycle detection: A -> B -> C -> A
	#[test]
	fn test_complex_cycle_detection() {
		let path = RelationshipPath::new()
			.through("user")
			.through("posts")
			.through("comments");

		// Try to add "user" again - should create a cycle
		let result = path.try_through("user");

		assert!(result.is_err());
		let err = result.unwrap_err();
		assert_eq!(err.relationship, "user");
		assert_eq!(err.path, vec!["user", "posts", "comments"]);
	}

	/// Test that different relationships don't trigger cycle detection
	#[test]
	fn test_no_false_positive_cycle() {
		let path = RelationshipPath::new()
			.through("posts")
			.through("comments")
			.through("author")
			.through("profile");

		assert_eq!(path.path().len(), 4);
	}

	/// Test contains method for cycle detection
	#[test]
	fn test_contains_relationship() {
		let path = RelationshipPath::new().through("posts").through("comments");

		assert!(path.contains("posts"));
		assert!(path.contains("comments"));
		assert!(!path.contains("author"));
	}

	/// Test path with filters
	#[test]
	fn test_path_with_filters() {
		let path = RelationshipPath::new()
			.through("posts")
			.with_filter("published", "true")
			.through("comments")
			.attribute("content");

		assert!(path.has_filters());
		assert_eq!(path.filters().len(), 1);
		assert_eq!(
			path.filters()[0],
			("published".to_string(), "true".to_string())
		);
	}

	/// Test path with transforms
	#[test]
	fn test_path_with_transforms() {
		let path = RelationshipPath::new()
			.through("posts")
			.through("comments")
			.with_transform("author", "upper")
			.attribute("name");

		assert!(path.has_transforms());
		assert_eq!(path.transforms().len(), 1);
	}

	/// Test multiple filters on different relationships
	#[test]
	fn test_multiple_filters() {
		let path = RelationshipPath::new()
			.through("posts")
			.with_filter("published", "true")
			.through("comments")
			.with_filter("approved", "true")
			.attribute("content");

		assert!(path.has_filters());
		assert_eq!(path.filters().len(), 2);
	}

	/// Test error message formatting
	#[test]
	fn test_error_message_format() {
		let path = RelationshipPath::new().through("posts").through("author");

		let result = path.try_through("posts");
		assert!(result.is_err());

		let err = result.unwrap_err();
		let error_msg = err.to_string();
		assert!(error_msg.contains("Circular reference detected"));
		assert!(error_msg.contains("posts"));
		assert!(error_msg.contains("author"));
	}

	/// Test default implementation
	#[test]
	fn test_default_path() {
		let path = RelationshipPath::default();
		assert_eq!(path.path().len(), 0);
		assert!(!path.has_filters());
		assert!(!path.has_transforms());
	}

	/// Test empty path display in error
	#[test]
	fn test_empty_path_error() {
		let err = CircularReferenceError {
			relationship: "posts".to_string(),
			path: vec![],
		};
		let msg = err.to_string();
		assert!(msg.contains("(empty)"));
	}

	/// Test clone functionality
	#[test]
	fn test_path_clone() {
		let path1 = RelationshipPath::new()
			.through("posts")
			.through("comments")
			.attribute("content");

		let path2 = path1.clone();

		assert_eq!(path1.path(), path2.path());
		assert_eq!(path1.get_attribute(), path2.get_attribute());
	}

	/// Test using through() method (non-checked version)
	#[test]
	fn test_through_allows_cycles() {
		// through() method doesn't check for cycles - it just adds to the path
		// This is by design for backwards compatibility
		let path = RelationshipPath::new()
			.through("posts")
			.through("author")
			.through("posts"); // No error, just adds to path

		assert_eq!(path.path().len(), 3);
		// But the visited set will detect it
		assert!(path.contains("posts"));
	}

	/// Test validate method
	#[test]
	fn test_validate_method() {
		let path = RelationshipPath::new()
			.through("posts")
			.through("comments")
			.attribute("content");

		let result = path.validate();
		assert!(result.is_ok());
	}

	/// Test path with only attribute (no relationships)
	#[test]
	fn test_attribute_only_path() {
		let path = RelationshipPath::new().attribute("name");

		assert_eq!(path.path().len(), 0);
		assert_eq!(path.get_attribute(), "name");
	}

	/// Test filter on empty path (edge case)
	#[test]
	fn test_filter_on_empty_path() {
		let path = RelationshipPath::new().with_filter("field", "value");

		// Filter is added with empty relationship name
		assert!(path.has_filters());
	}
}