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
//! Lazy loading and eager loading strategies for association proxies

use async_trait::async_trait;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use tracing::warn;

use crate::proxy::ProxyResult;

/// Loading strategy for relationships
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoadStrategy {
	/// Load immediately when accessed
	Eager,
	/// Load only when needed
	Lazy,
	/// Load using a separate SELECT query
	Select,
	/// Load using a JOIN
	Joined,
}

/// Trait for lazy-loadable relationships
#[async_trait]
pub trait LazyLoadable: Send + Sync {
	/// The type of the loaded data
	type Data: Clone + Send + Sync;

	/// Check if data is loaded
	fn is_loaded(&self) -> bool;

	/// Load the data if not already loaded
	async fn load(&mut self) -> ProxyResult<()>;

	/// Get the loaded data, loading if necessary
	async fn get(&mut self) -> ProxyResult<Self::Data>;

	/// Get the loaded data without loading (returns None if not loaded)
	fn get_if_loaded(&self) -> Option<Self::Data>;
}

/// Lazy-loaded wrapper for relationship data
pub struct LazyLoaded<T, F>
where
	T: Clone + Send + Sync,
	F: Fn() -> futures::future::BoxFuture<'static, ProxyResult<T>> + Send + Sync,
{
	/// The cached data
	data: Arc<RwLock<Option<T>>>,
	/// The loader function
	loader: Arc<F>,
}

impl<T, F> LazyLoaded<T, F>
where
	T: Clone + Send + Sync,
	F: Fn() -> futures::future::BoxFuture<'static, ProxyResult<T>> + Send + Sync,
{
	/// Create a new lazy-loaded value
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::loading::LazyLoaded;
	///
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// let lazy = LazyLoaded::new(|| {
	///     Box::pin(async { Ok(vec![1, 2, 3]) })
	/// });
	///
	/// // Data is not loaded yet
	/// assert!(!lazy.is_loaded());
	/// # Ok(())
	/// # }
	/// ```
	pub fn new(loader: F) -> Self {
		Self {
			data: Arc::new(RwLock::new(None)),
			loader: Arc::new(loader),
		}
	}

	/// Create a pre-loaded lazy value
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::loading::LazyLoaded;
	///
	/// let lazy = LazyLoaded::preloaded(vec![1, 2, 3], || {
	///     Box::pin(async { Ok(vec![4, 5, 6]) })
	/// });
	///
	/// // Data is already loaded
	/// assert!(lazy.is_loaded());
	/// ```
	pub fn preloaded(data: T, loader: F) -> Self {
		Self {
			data: Arc::new(RwLock::new(Some(data))),
			loader: Arc::new(loader),
		}
	}

	/// Acquire a read lock, recovering from poison if necessary
	fn read_lock(&self) -> RwLockReadGuard<'_, Option<T>> {
		self.data.read().unwrap_or_else(|e| {
			warn!("RwLock poisoned on read, recovering with inner value");
			e.into_inner()
		})
	}

	/// Acquire a write lock, recovering from poison if necessary
	fn write_lock(&self) -> RwLockWriteGuard<'_, Option<T>> {
		self.data.write().unwrap_or_else(|e| {
			warn!("RwLock poisoned on write, recovering with inner value");
			e.into_inner()
		})
	}

	/// Check if data is loaded
	pub fn is_loaded(&self) -> bool {
		self.read_lock().is_some()
	}

	/// Load the data if not already loaded
	pub async fn load(&self) -> ProxyResult<()> {
		// Check if already loaded
		if self.is_loaded() {
			return Ok(());
		}

		// Load the data
		let data = (self.loader)().await?;

		// Store it
		let mut guard = self.write_lock();
		*guard = Some(data);

		Ok(())
	}

	/// Get the loaded data, loading if necessary
	pub async fn get(&self) -> ProxyResult<T> {
		// Ensure data is loaded
		self.load().await?;

		// Handle the case where data was reset between load() and this read
		// by another task calling reset() concurrently.
		// Extract the cloned data while holding the lock briefly, then drop it
		// before any await point to satisfy clippy::await_holding_lock.
		let cached = {
			let guard = self.read_lock();
			guard.as_ref().cloned()
		};

		match cached {
			Some(data) => Ok(data),
			None => {
				// Data was reset after load() completed; reload
				let data = (self.loader)().await?;
				let mut write_guard = self.write_lock();
				*write_guard = Some(data.clone());
				Ok(data)
			}
		}
	}

	/// Get the loaded data without loading (returns None if not loaded)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::loading::LazyLoaded;
	///
	/// let lazy = LazyLoaded::new(|| {
	///     Box::pin(async { Ok(vec![1, 2, 3]) })
	/// });
	///
	/// // Returns None since not loaded yet
	/// assert!(lazy.get_if_loaded().is_none());
	/// ```
	pub fn get_if_loaded(&self) -> Option<T> {
		self.read_lock().as_ref().cloned()
	}

	/// Reset the lazy-loaded value, forcing a reload on next access
	pub fn reset(&self) {
		let mut guard = self.write_lock();
		*guard = None;
	}
}

/// Eager loading configuration
#[derive(Debug, Clone)]
pub struct EagerLoadConfig {
	/// Maximum depth to eager load
	pub max_depth: usize,
	/// Relationships to eager load
	pub relationships: Vec<String>,
}

impl EagerLoadConfig {
	/// Create a new eager load configuration
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::loading::EagerLoadConfig;
	///
	/// let config = EagerLoadConfig::new()
	///     .with_relationship("posts")
	///     .with_relationship("comments")
	///     .max_depth(3);
	///
	/// assert_eq!(config.max_depth, 3);
	/// assert_eq!(config.relationships.len(), 2);
	/// ```
	pub fn new() -> Self {
		Self {
			max_depth: 2,
			relationships: Vec::new(),
		}
	}

	/// Add a relationship to eager load
	pub fn with_relationship(mut self, relationship: &str) -> Self {
		self.relationships.push(relationship.to_string());
		self
	}

	/// Set maximum depth
	pub fn max_depth(mut self, depth: usize) -> Self {
		self.max_depth = depth;
		self
	}
}

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

/// Trait for models that support eager loading
#[async_trait]
pub trait EagerLoadable: Send + Sync {
	/// Eager load specified relationships
	async fn eager_load(&mut self, config: &EagerLoadConfig) -> ProxyResult<()>;

	/// Check if a relationship is loaded
	fn is_relationship_loaded(&self, name: &str) -> bool;
}

/// Cache for loaded relationship data
pub struct RelationshipCache {
	cache: Arc<RwLock<std::collections::HashMap<String, Box<dyn std::any::Any + Send + Sync>>>>,
}

impl RelationshipCache {
	/// Create a new relationship cache
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::loading::RelationshipCache;
	///
	/// let cache = RelationshipCache::new();
	/// assert!(!cache.contains("posts"));
	/// ```
	pub fn new() -> Self {
		Self {
			cache: Arc::new(RwLock::new(std::collections::HashMap::new())),
		}
	}

	/// Acquire a read lock, recovering from poison if necessary
	fn read_lock(
		&self,
	) -> RwLockReadGuard<'_, std::collections::HashMap<String, Box<dyn std::any::Any + Send + Sync>>>
	{
		self.cache.read().unwrap_or_else(|e| {
			warn!("RelationshipCache RwLock poisoned on read, recovering with inner value");
			e.into_inner()
		})
	}

	/// Acquire a write lock, recovering from poison if necessary
	fn write_lock(
		&self,
	) -> RwLockWriteGuard<'_, std::collections::HashMap<String, Box<dyn std::any::Any + Send + Sync>>>
	{
		self.cache.write().unwrap_or_else(|e| {
			warn!("RelationshipCache RwLock poisoned on write, recovering with inner value");
			e.into_inner()
		})
	}

	/// Check if a relationship is cached
	pub fn contains(&self, key: &str) -> bool {
		self.read_lock().contains_key(key)
	}

	/// Get a cached relationship
	pub fn get<T>(&self, key: &str) -> Option<T>
	where
		T: 'static + Clone,
	{
		let cache = self.read_lock();
		cache.get(key).and_then(|v| v.downcast_ref::<T>().cloned())
	}

	/// Set a cached relationship
	pub fn set<T: 'static + Send + Sync>(&self, key: String, value: T) {
		let mut cache = self.write_lock();
		cache.insert(key, Box::new(value));
	}

	/// Remove a cached relationship
	pub fn remove(&self, key: &str) -> bool {
		let mut cache = self.write_lock();
		cache.remove(key).is_some()
	}

	/// Clear all cached relationships
	pub fn clear(&self) {
		let mut cache = self.write_lock();
		cache.clear();
	}
}

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

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

	#[tokio::test]
	async fn test_lazy_loaded() {
		let lazy = LazyLoaded::new(|| Box::pin(async { Ok(vec![1, 2, 3]) }));

		assert!(!lazy.is_loaded());

		lazy.load().await.unwrap();
		assert!(lazy.is_loaded());

		let data = lazy.get_if_loaded().unwrap();
		assert_eq!(data, vec![1, 2, 3]);
	}

	#[tokio::test]
	async fn test_lazy_loaded_preloaded() {
		let lazy = LazyLoaded::preloaded(vec![1, 2, 3], || Box::pin(async { Ok(vec![4, 5, 6]) }));

		assert!(lazy.is_loaded());

		let data = lazy.get_if_loaded().unwrap();
		assert_eq!(data, vec![1, 2, 3]);
	}

	#[test]
	fn test_eager_load_config() {
		let config = EagerLoadConfig::new()
			.with_relationship("posts")
			.with_relationship("comments")
			.max_depth(5);

		assert_eq!(config.max_depth, 5);
		assert_eq!(config.relationships, vec!["posts", "comments"]);
	}

	#[tokio::test]
	async fn test_lazy_loaded_get_returns_owned_clone() {
		// Arrange
		let lazy = LazyLoaded::new(|| Box::pin(async { Ok(vec![10, 20, 30]) }));

		// Act - get() loads and returns owned T via clone
		let data = lazy.get().await.unwrap();

		// Assert
		assert_eq!(data, vec![10, 20, 30]);
		assert!(lazy.is_loaded());
	}

	#[tokio::test]
	async fn test_lazy_loaded_get_if_loaded_returns_none_when_not_loaded() {
		// Arrange
		let lazy = LazyLoaded::new(|| Box::pin(async { Ok(42) }));

		// Act
		let result = lazy.get_if_loaded();

		// Assert
		assert_eq!(result, None);
	}

	#[tokio::test]
	async fn test_lazy_loaded_get_if_loaded_returns_cloned_value() {
		// Arrange
		let lazy = LazyLoaded::preloaded(String::from("hello"), || {
			Box::pin(async { Ok(String::from("world")) })
		});

		// Act
		let result = lazy.get_if_loaded();

		// Assert
		assert_eq!(result, Some(String::from("hello")));
	}

	#[tokio::test]
	async fn test_lazy_loaded_reset_forces_reload() {
		// Arrange
		let lazy = LazyLoaded::preloaded(vec![1], || Box::pin(async { Ok(vec![2]) }));
		assert!(lazy.is_loaded());

		// Act
		lazy.reset();

		// Assert
		assert!(!lazy.is_loaded());
		assert_eq!(lazy.get_if_loaded(), None);

		// Reload
		let data = lazy.get().await.unwrap();
		assert_eq!(data, vec![2]);
	}

	#[test]
	fn test_relationship_cache() {
		let cache = RelationshipCache::new();

		assert!(!cache.contains("key1"));

		cache.set("key1".to_string(), vec![1, 2, 3]);
		assert!(cache.contains("key1"));

		let value: Vec<i32> = cache.get("key1").unwrap();
		assert_eq!(value, vec![1, 2, 3]);

		assert!(cache.remove("key1"));
		assert!(!cache.contains("key1"));
	}
}