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
//! Derive partial (“relaxed”) types
//!
//! # Usage
//!
//! ## Generate “relaxed” types
//!
//! ```
//! use relax::Relax;
//!
//! #[derive(Relax)]
//! #[relax(PartialData)]
//! pub(crate) struct Data {
//! id: u16,
//! pub name: String,
//! pub favorite: Option<String>,
//! }
//! ```
//!
//! yields the following partial (“relaxed”) struct:
//!
//! ```
//! pub(crate) struct PartialData {
//! id: Option<u16>,
//! pub name: Option<String>,
//! pub favorite: Option<String>,
//! }
//! ```
//!
//! Nesting is also supported:
//!
//! ```
//! use relax::Relax;
//!
//! #[derive(Relax)]
//! #[relax(PartialFoo)]
//! struct Foo {
//! id: u16,
//! #[relax]
//! bar: Bar,
//! #[relax]
//! bar2: Option<Bar>,
//! }
//!
//! #[derive(Relax)]
//! #[relax(PartialBar)]
//! struct Bar {
//! name: String,
//! favorite: Option<String>,
//! }
//! ```
//!
//! yields
//!
//! ```
//! struct PatrialFoo {
//! id: Option<u16>,
//! bar: Option<PartialBar>,
//! bar2: Option<PartialBar>,
//! }
//!
//! struct PartialBar {
//! name: Option<String>,
//! favorite: Option<String>,
//! }
//! ```
//!
//! You can also add attributes to generated structs:
//!
//! ```
//! use relax::Relax;
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Relax)]
//! #[relax(PartialData, derive(Serialize, Deserialize), serde(rename_all = "UPPERCASE"))]
//! pub(crate) struct Data {
//! id: u16,
//! pub name: String,
//! pub favorite: Option<String>,
//! }
//! ```
//!
//! yields
//!
//! ```ignore
//! #[derive(Serialize, Deserialize)]
//! #[serde(rename_all = "UPPERCASE")]
//! pub(crate) struct PartialData {
//! id: Option<u16>,
//! pub name: Option<String>,
//! pub favorite: Option<String>,
//! }
//! ```
//!
//! ## Create empty “relaxed” instances
//!
//! Relaxed structs derive [`Relaxed`] trait, which has [`new`](Relaxed::new) associated function.
//!
//! ```
//! use relax::{Relax, Relaxed};
//!
//! #[derive(Relax)]
//! #[relax(PartialData, derive(Debug, PartialEq))]
//! pub(crate) struct Data {
//! id: u16,
//! pub name: String,
//! pub favorite: Option<String>,
//! }
//!
//! # fn main() {
//! let empty = PartialData {
//! id: None,
//! name: None,
//! favorite: None,
//! };
//!
//! assert_eq!(PartialData::new(), empty);
//! # }
//! ```
//!
//! ## Combine multiple “relaxed” instances
//!
//! [`Relaxed`] trait also has [`merge`](Relaxed::merge) function. The behavior is similar to [`Option::or`], except that this function is applied for each struct field.
//!
//! ```
//! use relax::{Relax, Relaxed};
//!
//! #[derive(Relax)]
//! #[relax(PartialData, derive(Debug, PartialEq))]
//! pub(crate) struct Data {
//! id: u16,
//! pub name: String,
//! pub favorite: Option<String>,
//! }
//!
//! # fn main() {
//!
//! let partial_data_1 = PartialData {
//! id: None,
//! name: Some("John".to_owned()),
//! favorite: None,
//! };
//!
//! let partial_data_2 = PartialData {
//! id: None,
//! name: None,
//! favorite: Some("Rust".to_owned()),
//! };
//!
//! let merged = partial_data_1.merge(partial_data_2);
//!
//! let expected = PartialData {
//! id: None,
//! name: Some("John".to_owned()),
//! favorite: Some("Rust".to_owned()),
//! };
//!
//! assert_eq!(merged, expected);
//!
//! # }
//! ```
//!
//! ## Create an original struct instance from a relaxed instance
//!
//! If all the required fields of the original struct are set in a relaxed struct instance, you can use [`try_into()`](std::convert::TryInto::try_into) to convert it to the original struct.
//!
//! ```ignore
//! use relax::{Relax, Relaxed};
//!
//! let partial_data_1: PartialData = read_data(file_1);
//! let partial_data_2: PartialData = read_data(file_2);
//! let partial_data_3: PartialData = read_data(file_3);
//!
//! let data: Data = partial_data_1
//! .merge(partial_data_2)
//! .merge(partial_data_3)
//! .try_into()?;
//! ```
pub use Relax;
use ;
;