alux_shape/construct.rs
1//! How a value is made from the members its shape names, and taken apart into them.
2//!
3//! A shape describes a value; it never builds one. An interpretation that reads a value member by
4//! member, from command-line words, named parameters, or a form, needs the other direction too: a
5//! product is its fields taken together, and a sum is the one alternative it carries. A sum is an
6//! isomorphism with a `SumN`. A product is one with a tuple unless a member is skipped: that member is
7//! no field and is made with `Default`, so a product made from its fields answers those fields, and its members are named as identifiers, the way an
8//! operation's argument names are, so `words_of` reads them as the words a shape names them by.
9
10use alux_ext::ext;
11
12/// States that a value is its fields taken together.
13///
14/// `#[derive(Shape)]` states it for every struct it reads.
15#[diagnostic::on_unimplemented(
16 message = "`{Self}` is not stated as a product of its fields",
17 note = "`#[derive(Shape)]` states `Product` for a struct with named members; a skipped member is made with `Default`, and a flattened one is one field"
18)]
19pub trait Product: Sized {
20 /// The fields, in order, as a tuple.
21 type Fields;
22 /// The fields' names, in order, as identifiers.
23 const FIELDS: &'static [&'static str];
24
25 /// Makes the value from its fields.
26 fn from_fields(fields: Self::Fields) -> Self;
27
28 /// Takes the value apart into its fields.
29 fn into_fields(self) -> Self::Fields;
30}
31
32/// States that a value is exactly one of its alternatives.
33///
34/// An alternative carrying no value is `()`.
35///
36/// `#[derive(Shape)]` states it for every enum it reads with two to sixteen alternatives.
37#[diagnostic::on_unimplemented(
38 message = "`{Self}` is not stated as a sum of its alternatives",
39 note = "`#[derive(Shape)]` states `Sum` for an enum of two to sixteen alternatives, each carrying one value or none"
40)]
41pub trait Sum: Sized {
42 /// The alternatives, in order, as a `Sum2` through `Sum16`.
43 type Alternatives;
44 /// The alternatives' names, in order, as identifiers.
45 const ALTERNATIVES: &'static [&'static str];
46
47 /// Makes the value from the alternative it carries.
48 fn from_alternative(alternative: Self::Alternatives) -> Self;
49
50 /// Answers the alternative the value carries.
51 fn into_alternative(self) -> Self::Alternatives;
52}
53
54/// Holds the one alternative of two a value carries.
55#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
56pub enum Sum2<A, B> {
57 /// The first alternative.
58 A(A),
59 /// The second alternative.
60 B(B),
61}
62
63/// Holds the one alternative of three a value carries.
64#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
65pub enum Sum3<A, B, C> {
66 /// The first alternative.
67 A(A),
68 /// The second alternative.
69 B(B),
70 /// The third alternative.
71 C(C),
72}
73
74/// Holds the one alternative of four a value carries.
75#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
76pub enum Sum4<A, B, C, D> {
77 /// The first alternative.
78 A(A),
79 /// The second alternative.
80 B(B),
81 /// The third alternative.
82 C(C),
83 /// The fourth alternative.
84 D(D),
85}
86
87/// Holds the one alternative of five a value carries.
88#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
89pub enum Sum5<A, B, C, D, E> {
90 /// The first alternative.
91 A(A),
92 /// The second alternative.
93 B(B),
94 /// The third alternative.
95 C(C),
96 /// The fourth alternative.
97 D(D),
98 /// The fifth alternative.
99 E(E),
100}
101
102/// Holds the one alternative of six a value carries.
103#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
104pub enum Sum6<A, B, C, D, E, F> {
105 /// The first alternative.
106 A(A),
107 /// The second alternative.
108 B(B),
109 /// The third alternative.
110 C(C),
111 /// The fourth alternative.
112 D(D),
113 /// The fifth alternative.
114 E(E),
115 /// The sixth alternative.
116 F(F),
117}
118
119/// Holds the one alternative of seven a value carries.
120#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
121pub enum Sum7<A, B, C, D, E, F, G> {
122 /// The first alternative.
123 A(A),
124 /// The second alternative.
125 B(B),
126 /// The third alternative.
127 C(C),
128 /// The fourth alternative.
129 D(D),
130 /// The fifth alternative.
131 E(E),
132 /// The sixth alternative.
133 F(F),
134 /// The seventh alternative.
135 G(G),
136}
137
138/// Holds the one alternative of eight a value carries.
139#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
140pub enum Sum8<A, B, C, D, E, F, G, H> {
141 /// The first alternative.
142 A(A),
143 /// The second alternative.
144 B(B),
145 /// The third alternative.
146 C(C),
147 /// The fourth alternative.
148 D(D),
149 /// The fifth alternative.
150 E(E),
151 /// The sixth alternative.
152 F(F),
153 /// The seventh alternative.
154 G(G),
155 /// The eighth alternative.
156 H(H),
157}
158
159/// Holds the one alternative of nine a value carries.
160#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
161pub enum Sum9<A, B, C, D, E, F, G, H, I> {
162 /// The first alternative.
163 A(A),
164 /// The second alternative.
165 B(B),
166 /// The third alternative.
167 C(C),
168 /// The fourth alternative.
169 D(D),
170 /// The fifth alternative.
171 E(E),
172 /// The sixth alternative.
173 F(F),
174 /// The seventh alternative.
175 G(G),
176 /// The eighth alternative.
177 H(H),
178 /// The ninth alternative.
179 I(I),
180}
181
182/// Holds the one alternative of ten a value carries.
183#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
184pub enum Sum10<A, B, C, D, E, F, G, H, I, J> {
185 /// The first alternative.
186 A(A),
187 /// The second alternative.
188 B(B),
189 /// The third alternative.
190 C(C),
191 /// The fourth alternative.
192 D(D),
193 /// The fifth alternative.
194 E(E),
195 /// The sixth alternative.
196 F(F),
197 /// The seventh alternative.
198 G(G),
199 /// The eighth alternative.
200 H(H),
201 /// The ninth alternative.
202 I(I),
203 /// The tenth alternative.
204 J(J),
205}
206
207/// Holds the one alternative of eleven a value carries.
208#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
209pub enum Sum11<A, B, C, D, E, F, G, H, I, J, K> {
210 /// The first alternative.
211 A(A),
212 /// The second alternative.
213 B(B),
214 /// The third alternative.
215 C(C),
216 /// The fourth alternative.
217 D(D),
218 /// The fifth alternative.
219 E(E),
220 /// The sixth alternative.
221 F(F),
222 /// The seventh alternative.
223 G(G),
224 /// The eighth alternative.
225 H(H),
226 /// The ninth alternative.
227 I(I),
228 /// The tenth alternative.
229 J(J),
230 /// The eleventh alternative.
231 K(K),
232}
233
234/// Holds the one alternative of twelve a value carries.
235#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
236pub enum Sum12<A, B, C, D, E, F, G, H, I, J, K, L> {
237 /// The first alternative.
238 A(A),
239 /// The second alternative.
240 B(B),
241 /// The third alternative.
242 C(C),
243 /// The fourth alternative.
244 D(D),
245 /// The fifth alternative.
246 E(E),
247 /// The sixth alternative.
248 F(F),
249 /// The seventh alternative.
250 G(G),
251 /// The eighth alternative.
252 H(H),
253 /// The ninth alternative.
254 I(I),
255 /// The tenth alternative.
256 J(J),
257 /// The eleventh alternative.
258 K(K),
259 /// The twelfth alternative.
260 L(L),
261}
262
263/// Holds the one alternative of thirteen a value carries.
264#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
265pub enum Sum13<A, B, C, D, E, F, G, H, I, J, K, L, M> {
266 /// The first alternative.
267 A(A),
268 /// The second alternative.
269 B(B),
270 /// The third alternative.
271 C(C),
272 /// The fourth alternative.
273 D(D),
274 /// The fifth alternative.
275 E(E),
276 /// The sixth alternative.
277 F(F),
278 /// The seventh alternative.
279 G(G),
280 /// The eighth alternative.
281 H(H),
282 /// The ninth alternative.
283 I(I),
284 /// The tenth alternative.
285 J(J),
286 /// The eleventh alternative.
287 K(K),
288 /// The twelfth alternative.
289 L(L),
290 /// The thirteenth alternative.
291 M(M),
292}
293
294/// Holds the one alternative of fourteen a value carries.
295#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
296pub enum Sum14<A, B, C, D, E, F, G, H, I, J, K, L, M, N> {
297 /// The first alternative.
298 A(A),
299 /// The second alternative.
300 B(B),
301 /// The third alternative.
302 C(C),
303 /// The fourth alternative.
304 D(D),
305 /// The fifth alternative.
306 E(E),
307 /// The sixth alternative.
308 F(F),
309 /// The seventh alternative.
310 G(G),
311 /// The eighth alternative.
312 H(H),
313 /// The ninth alternative.
314 I(I),
315 /// The tenth alternative.
316 J(J),
317 /// The eleventh alternative.
318 K(K),
319 /// The twelfth alternative.
320 L(L),
321 /// The thirteenth alternative.
322 M(M),
323 /// The fourteenth alternative.
324 N(N),
325}
326
327/// Holds the one alternative of fifteen a value carries.
328#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
329pub enum Sum15<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O> {
330 /// The first alternative.
331 A(A),
332 /// The second alternative.
333 B(B),
334 /// The third alternative.
335 C(C),
336 /// The fourth alternative.
337 D(D),
338 /// The fifth alternative.
339 E(E),
340 /// The sixth alternative.
341 F(F),
342 /// The seventh alternative.
343 G(G),
344 /// The eighth alternative.
345 H(H),
346 /// The ninth alternative.
347 I(I),
348 /// The tenth alternative.
349 J(J),
350 /// The eleventh alternative.
351 K(K),
352 /// The twelfth alternative.
353 L(L),
354 /// The thirteenth alternative.
355 M(M),
356 /// The fourteenth alternative.
357 N(N),
358 /// The fifteenth alternative.
359 O(O),
360}
361
362/// Holds the one alternative of sixteen a value carries.
363#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
364pub enum Sum16<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P> {
365 /// The first alternative.
366 A(A),
367 /// The second alternative.
368 B(B),
369 /// The third alternative.
370 C(C),
371 /// The fourth alternative.
372 D(D),
373 /// The fifth alternative.
374 E(E),
375 /// The sixth alternative.
376 F(F),
377 /// The seventh alternative.
378 G(G),
379 /// The eighth alternative.
380 H(H),
381 /// The ninth alternative.
382 I(I),
383 /// The tenth alternative.
384 J(J),
385 /// The eleventh alternative.
386 K(K),
387 /// The twelfth alternative.
388 L(L),
389 /// The thirteenth alternative.
390 M(M),
391 /// The fourteenth alternative.
392 N(N),
393 /// The fifteenth alternative.
394 O(O),
395 /// The sixteenth alternative.
396 P(P),
397}
398
399/// States that a product made from its fields answers those fields.
400///
401/// A skipped member is no field, so the product made again holds its `Default`, not its value.
402#[ext(name = ProductLawsExt)]
403pub impl<This, Fields> This
404where
405 This: Product<Fields = Fields> + Clone,
406 Fields: Clone + PartialEq,
407{
408 /// Taking apart a product made from this one's fields answers the same fields.
409 fn law_product_round_trip(&self) -> bool {
410 let fields = self.clone().into_fields();
411 This::from_fields(fields.clone()).into_fields() == fields
412 }
413}
414
415/// States that answering a sum's alternative and making it again changes nothing.
416#[ext(name = SumLawsExt)]
417pub impl<This> This
418where
419 This: Sum + Clone + PartialEq,
420{
421 /// Making a sum from the alternative it carries answers the same sum.
422 fn law_sum_round_trip(&self) -> bool {
423 This::from_alternative(self.clone().into_alternative()) == *self
424 }
425}