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
//! Core association proxy implementation

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

use crate::proxy::builder::{GetterFn, SetterFn, ValidatorFn};
use crate::proxy::{ProxyError, ProxyResult};

/// Association proxy for transparent access to related object attributes
///
/// ## Example
///
/// ```rust,no_run
/// # use reinhardt_urls::proxy::AssociationProxy;
/// # #[derive(Clone)]
/// # struct UserKeyword;
/// # #[derive(Clone)]
/// # struct User;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let user = User;
/// // Access keyword names through user_keywords relationship
/// let proxy: AssociationProxy<UserKeyword, String> = AssociationProxy::new("user_keywords", "keyword");
/// // let names = proxy.get_collection(&user).await?;
/// # Ok(())
/// # }
/// ```
pub struct AssociationProxy<T, U> {
	/// Optional name/alias for this proxy
	pub name: Option<String>,

	/// Name of the relationship attribute
	pub relationship: String,

	/// Name of the attribute on the related object
	pub attribute: String,

	/// Optional creator function for new associations
	pub creator: Option<fn(U) -> T>,

	/// Optional custom getter function
	pub getter: Option<GetterFn<T, U>>,

	/// Optional custom setter function
	pub setter: Option<SetterFn<T, U>>,

	/// Optional validator function
	pub validator: Option<ValidatorFn<U>>,

	/// Optional transform function
	pub transform: Option<fn(U) -> U>,

	/// Phantom data for type parameters
	_phantom: PhantomData<(T, U)>,
}

impl<T, U> AssociationProxy<T, U> {
	/// Create a new association proxy
	///
	/// # Arguments
	///
	/// * `relationship` - Name of the relationship to traverse
	/// * `attribute` - Name of the attribute to access on related objects
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// let proxy: AssociationProxy<(), ()> = AssociationProxy::new("user_keywords", "keyword");
	/// assert_eq!(proxy.relationship, "user_keywords");
	/// assert_eq!(proxy.attribute, "keyword");
	/// ```
	pub fn new(relationship: &str, attribute: &str) -> Self {
		Self {
			name: None,
			relationship: relationship.to_string(),
			attribute: attribute.to_string(),
			creator: None,
			getter: None,
			setter: None,
			validator: None,
			transform: None,
			_phantom: PhantomData,
		}
	}
	/// Set a creator function for new associations
	///
	/// The creator function is called when adding new items to the association.
	/// It should create an association object from the target value.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// fn create_association(value: String) -> i32 { 42 }
	///
	/// let proxy = AssociationProxy::new("items", "value")
	///     .with_creator(create_association);
	/// assert!(proxy.creator.is_some());
	/// ```
	pub fn with_creator(mut self, creator: fn(U) -> T) -> Self {
		self.creator = Some(creator);
		self
	}

	/// Set a custom getter function
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// fn custom_getter(_obj: &()) -> Result<(), reinhardt_urls::proxy::ProxyError> {
	///     Ok(())
	/// }
	///
	/// let proxy = AssociationProxy::new("data", "value")
	///     .with_getter(custom_getter);
	/// assert!(proxy.getter.is_some());
	/// ```
	pub fn with_getter(mut self, getter: fn(&T) -> Result<U, crate::proxy::ProxyError>) -> Self {
		self.getter = Some(getter);
		self
	}

	/// Set a custom setter function
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// fn custom_setter(_obj: &mut (), _value: ()) -> Result<(), reinhardt_urls::proxy::ProxyError> {
	///     Ok(())
	/// }
	///
	/// let proxy = AssociationProxy::new("data", "value")
	///     .with_setter(custom_setter);
	/// assert!(proxy.setter.is_some());
	/// ```
	pub fn with_setter(
		mut self,
		setter: fn(&mut T, U) -> Result<(), crate::proxy::ProxyError>,
	) -> Self {
		self.setter = Some(setter);
		self
	}

	/// Set a validator function
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::{AssociationProxy, ProxyError};
	///
	/// fn validate_value(_value: &()) -> Result<(), ProxyError> {
	///     Ok(())
	/// }
	///
	/// let proxy: AssociationProxy<(), ()> = AssociationProxy::new("data", "value")
	///     .with_validator(validate_value);
	/// assert!(proxy.validator.is_some());
	/// ```
	pub fn with_validator(
		mut self,
		validator: fn(&U) -> Result<(), crate::proxy::ProxyError>,
	) -> Self {
		self.validator = Some(validator);
		self
	}

	/// Set a transform function
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// fn transform_value(value: ()) -> () {
	///     value
	/// }
	///
	/// let proxy: AssociationProxy<(), ()> = AssociationProxy::new("data", "value")
	///     .with_transform(transform_value);
	/// assert!(proxy.transform.is_some());
	/// ```
	pub fn with_transform(mut self, transform: fn(U) -> U) -> Self {
		self.transform = Some(transform);
		self
	}

	/// Get the proxy name if set
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// let mut proxy: AssociationProxy<(), ()> = AssociationProxy::new("rel", "attr");
	/// proxy.name = Some("my_proxy".to_string());
	/// assert_eq!(proxy.name(), Some("my_proxy"));
	/// ```
	pub fn name(&self) -> Option<&str> {
		self.name.as_deref()
	}

	/// Get the relationship name
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// let proxy: AssociationProxy<(), ()> = AssociationProxy::new("posts", "title");
	/// assert_eq!(proxy.relationship(), "posts");
	/// ```
	pub fn relationship(&self) -> &str {
		&self.relationship
	}

	/// Get the attribute name
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// let proxy: AssociationProxy<(), ()> = AssociationProxy::new("posts", "title");
	/// assert_eq!(proxy.attribute(), "title");
	/// ```
	pub fn attribute(&self) -> &str {
		&self.attribute
	}

	/// Check if custom accessors (getter/setter) are configured
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// fn custom_getter(_obj: &()) -> Result<(), reinhardt_urls::proxy::ProxyError> {
	///     Ok(())
	/// }
	///
	/// let proxy = AssociationProxy::new("data", "value")
	///     .with_getter(custom_getter);
	/// assert!(proxy.has_custom_accessors());
	/// ```
	pub fn has_custom_accessors(&self) -> bool {
		self.getter.is_some() || self.setter.is_some()
	}

	/// Check if a validator is configured
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::{AssociationProxy, ProxyError};
	///
	/// fn validate(_value: &()) -> Result<(), ProxyError> {
	///     Ok(())
	/// }
	///
	/// let proxy: AssociationProxy<(), ()> = AssociationProxy::new("data", "value")
	///     .with_validator(validate);
	/// assert!(proxy.has_validator());
	/// ```
	pub fn has_validator(&self) -> bool {
		self.validator.is_some()
	}

	/// Check if a transform function is configured
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::AssociationProxy;
	///
	/// fn transform(value: () ) -> () { value }
	///
	/// let proxy: AssociationProxy<(), ()> = AssociationProxy::new("data", "value")
	///     .with_transform(transform);
	/// assert!(proxy.has_transform());
	/// ```
	pub fn has_transform(&self) -> bool {
		self.transform.is_some()
	}
}

/// Trait for accessing proxy targets
#[async_trait]
pub trait ProxyAccessor<T> {
	/// Get the target value(s) from the source object
	async fn get(&self, source: &T) -> ProxyResult<ProxyTarget>;

	/// Set the target value(s) on the source object
	async fn set(&self, source: &mut T, value: ProxyTarget) -> ProxyResult<()>;
}

/// Represents the target of a proxy operation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ProxyTarget {
	/// Single scalar value
	Scalar(ScalarValue),

	/// Collection of values
	Collection(Vec<ScalarValue>),

	/// No value (None)
	None,
}

/// Scalar value types supported by proxies
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum ScalarValue {
	/// A string value.
	String(String),
	/// A 64-bit integer value.
	Integer(i64),
	/// A 64-bit floating-point value.
	Float(f64),
	/// A boolean value.
	Boolean(bool),
	/// A null (absent) value.
	Null,
}

impl From<String> for ScalarValue {
	fn from(s: String) -> Self {
		ScalarValue::String(s)
	}
}

impl From<&str> for ScalarValue {
	fn from(s: &str) -> Self {
		ScalarValue::String(s.to_string())
	}
}

impl From<i64> for ScalarValue {
	fn from(i: i64) -> Self {
		ScalarValue::Integer(i)
	}
}

impl From<f64> for ScalarValue {
	fn from(f: f64) -> Self {
		ScalarValue::Float(f)
	}
}

impl From<bool> for ScalarValue {
	fn from(b: bool) -> Self {
		ScalarValue::Boolean(b)
	}
}

impl ScalarValue {
	/// Try to convert to String
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::ScalarValue;
	///
	/// let value = ScalarValue::String("hello".to_string());
	/// assert_eq!(value.as_string().unwrap(), "hello");
	///
	/// let int_value = ScalarValue::Integer(42);
	/// assert!(int_value.as_string().is_err());
	/// ```
	pub fn as_string(&self) -> ProxyResult<String> {
		match self {
			ScalarValue::String(s) => Ok(s.clone()),
			_ => Err(ProxyError::TypeMismatch {
				expected: "String".to_string(),
				actual: format!("{:?}", self),
			}),
		}
	}
	/// Try to convert to i64
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::ScalarValue;
	///
	/// let value = ScalarValue::Integer(42);
	/// assert_eq!(value.as_integer().unwrap(), 42);
	///
	/// let str_value = ScalarValue::String("test".to_string());
	/// assert!(str_value.as_integer().is_err());
	/// ```
	pub fn as_integer(&self) -> ProxyResult<i64> {
		match self {
			ScalarValue::Integer(i) => Ok(*i),
			_ => Err(ProxyError::TypeMismatch {
				expected: "Integer".to_string(),
				actual: format!("{:?}", self),
			}),
		}
	}
	/// Try to convert to f64
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::ScalarValue;
	///
	/// let value = ScalarValue::Float(3.15);
	/// assert_eq!(value.as_float().unwrap(), 3.15);
	///
	/// let bool_value = ScalarValue::Boolean(true);
	/// assert!(bool_value.as_float().is_err());
	/// ```
	pub fn as_float(&self) -> ProxyResult<f64> {
		match self {
			ScalarValue::Float(f) => Ok(*f),
			_ => Err(ProxyError::TypeMismatch {
				expected: "Float".to_string(),
				actual: format!("{:?}", self),
			}),
		}
	}
	/// Try to convert to bool
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::ScalarValue;
	///
	/// let value = ScalarValue::Boolean(true);
	/// assert!(value.as_boolean().unwrap());
	///
	/// let int_value = ScalarValue::Integer(1);
	/// assert!(int_value.as_boolean().is_err());
	/// ```
	pub fn as_boolean(&self) -> ProxyResult<bool> {
		match self {
			ScalarValue::Boolean(b) => Ok(*b),
			_ => Err(ProxyError::TypeMismatch {
				expected: "Boolean".to_string(),
				actual: format!("{:?}", self),
			}),
		}
	}
	/// Check if value is null
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::proxy::ScalarValue;
	///
	/// let null_value = ScalarValue::Null;
	/// assert!(null_value.is_null());
	///
	/// let string_value = ScalarValue::String("test".to_string());
	/// assert!(!string_value.is_null());
	/// ```
	pub fn is_null(&self) -> bool {
		matches!(self, ScalarValue::Null)
	}
}

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

	#[test]
	fn test_proxy_scalar_conversions_unit() {
		let s = ScalarValue::String("test".to_string());
		assert_eq!(s.as_string().unwrap(), "test");

		let i = ScalarValue::Integer(42);
		assert_eq!(i.as_integer().unwrap(), 42);

		let f = ScalarValue::Float(3.15);
		assert_eq!(f.as_float().unwrap(), 3.15);

		let b = ScalarValue::Boolean(true);
		assert!(b.as_boolean().unwrap());
	}

	#[test]
	fn test_proxy_scalar_type_mismatch_unit() {
		let s = ScalarValue::String("test".to_string());
		assert!(s.as_integer().is_err());
	}
}