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
pub use Error;
pub type Result<T> = Result;
/// Attribute macro to define a value object struct with optional validation, normalization.
///
/// Configuration options:
/// - `Normalize`: Calls a `normalize` function on the inner value before creating the value object.
/// Requires the user to implement `Normalize<T>` for the value object.
/// - `Validate`: Calls a `validate` function on the inner value before creating the value object.
/// Requires the user to implement `Validate<T>` for the value object. If validation fails, the `TryFrom` implementation will return an error instead of creating the value object.
///
/// Example usage:
/// ```
/// use valobj::{value_object, Normalize, Validate};
///
/// #[value_object(Normalize, Validate)]
/// pub struct OrderId(i32);
///
/// impl Validate<i32> for OrderId {
/// fn validate(value: &i32) -> Result<(), valobj::Error> {
/// if *value > 0 {
/// Ok(())
/// } else {
/// Err(valobj::Error::InvalidValue(
/// "OrderId must be greater than 0".to_string(),
/// ))
/// }
/// }
/// }
///
/// impl Normalize<i32> for OrderId {
/// fn normalize(value: i32) -> i32 {
/// value * 10
/// }
/// }
/// ```
pub use value_object;
/// Trait for validating a value when a `value_object` is created.
///
/// Only called if the `Validate` option is enabled in the `#[value_object]` attribute.
///
/// Example usage:
/// ```
/// use valobj::{value_object, Validate};
///
/// #[value_object(Validate)]
/// pub struct OrderId(i32);
///
/// impl Validate<i32> for OrderId {
/// fn validate(value: &i32) -> Result<(), valobj::Error> {
/// if *value > 0 {
/// Ok(())
/// } else {
/// Err(valobj::Error::InvalidValue(
/// "OrderId must be greater than 0".to_string(),
/// ))
/// }
/// }
/// }
/// ```
/// Trait for normalizing a value when a `value_object` is created.
///
/// Only called if the `Normalize` option is enabled in the `#[value_object]` attribute.
///
/// Example usage:
/// ```
/// use valobj::{value_object, Normalize};
///
/// #[value_object(Normalize)]
/// pub struct OrderId(i32);
///
/// impl Normalize<i32> for OrderId {
/// fn normalize(value: i32) -> i32 {
/// value * 10
/// }
/// }
/// ```