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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! ## State Validation
//! `state-validation` is an implementation of the specification pattern. It lets you validate an input for a given state. Then, run an action using the validated output.
//!
//! Ex. You want to remove an admin from `UserStorage`, given a `UserID`, you want to retrieve the `User` who maps onto the `UserID` and validate they are an existing user whose privilege level is admin.
//! The state is `UserStorage`, the input is a `UserID`, and the valid output is an `AdminUser`.
//!
//! Here is our input `UserID`:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! struct UserID(usize);
//! ```
//! Our `User` which holds its `UserID` and `username`:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! #[derive(Clone)]
//! struct User {
//! id: UserID,
//! username: String,
//! }
//! ```
//! Our state will be `UserStorage`:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! # #[derive(Clone)]
//! # struct User {
//! # id: UserID,
//! # username: String,
//! # }
//! #[derive(Default)]
//! struct UserStorage {
//! maps: HashMap<UserID, User>,
//! }
//! ```
//! We will create a newtype `AdminUser`, which only users with admin privilege will be wrapped in.
//! This will also be the output of our filter which checks if a user is admin:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! # #[derive(Clone)]
//! # struct User {
//! # id: UserID,
//! # username: String,
//! # }
//! # #[derive(Default)]
//! # struct UserStorage {
//! # maps: HashMap<UserID, User>,
//! # }
//! struct AdminUser(User);
//! ```
//! Our first filter will check if a `User` exists given a `UserID`:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! # #[derive(Clone)]
//! # struct User {
//! # id: UserID,
//! # username: String,
//! # }
//! # #[derive(Default)]
//! # struct UserStorage {
//! # maps: HashMap<UserID, User>,
//! # }
//! # struct AdminUser(User);
//! struct UserExists;
//! # #[derive(Debug)]
//! # struct UserDoesNotExistError;
//! # impl std::error::Error for UserDoesNotExistError {}
//! # impl std::fmt::Display for UserDoesNotExistError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user does not exist")
//! # }
//! # }
//! impl StateFilter<UserStorage, UserID> for UserExists {
//! type ValidOutput = User;
//! type Error = UserDoesNotExistError;
//! fn filter(state: &UserStorage, user_id: UserID) -> Result<Self::ValidOutput, Self::Error> {
//! if let Some(user) = state.maps.get(&user_id) {
//! Ok(user.clone())
//! } else {
//! Err(UserDoesNotExistError)
//! }
//! }
//! }
//! ```
//! Our second filter will check if a `User` is admin:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! # #[derive(Clone)]
//! # struct User {
//! # id: UserID,
//! # username: String,
//! # }
//! # #[derive(Default)]
//! # struct UserStorage {
//! # maps: HashMap<UserID, User>,
//! # }
//! # struct AdminUser(User);
//! #
//! struct UserIsAdmin;
//! # #[derive(Debug)]
//! # struct UserIsNotAdminError;
//! # impl std::error::Error for UserIsNotAdminError {}
//! # impl std::fmt::Display for UserIsNotAdminError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user is not an admin")
//! # }
//! # }
//! impl<State> StateFilter<State, User> for UserIsAdmin {
//! type ValidOutput = AdminUser;
//! type Error = UserIsNotAdminError;
//! fn filter(state: &State, user: User) -> Result<Self::ValidOutput, Self::Error> {
//! if user.username == "ADMIN" {
//! Ok(AdminUser(user))
//! } else {
//! Err(UserIsNotAdminError)
//! }
//! }
//! }
//! ```
//! Note: in the above code, we don't care about the `state` so it is a generic.
//!
//! Now, we can finally implement an action that removes the admin from user storage:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! # #[derive(Clone)]
//! # struct User {
//! # id: UserID,
//! # username: String,
//! # }
//! # #[derive(Default)]
//! # struct UserStorage {
//! # maps: HashMap<UserID, User>,
//! # }
//! # struct AdminUser(User);
//! # struct UserExists;
//! # #[derive(Debug)]
//! # struct UserDoesNotExistError;
//! # impl std::error::Error for UserDoesNotExistError {}
//! # impl std::fmt::Display for UserDoesNotExistError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user does not exist")
//! # }
//! # }
//! # impl StateFilter<UserStorage, UserID> for UserExists {
//! # type ValidOutput = User;
//! # type Error = UserDoesNotExistError;
//! # fn filter(state: &UserStorage, user_id: UserID) -> Result<Self::ValidOutput, Self::Error> {
//! # if let Some(user) = state.maps.get(&user_id) {
//! # Ok(user.clone())
//! # } else {
//! # Err(UserDoesNotExistError)
//! # }
//! # }
//! # }
//! # struct UserIsAdmin;
//! # #[derive(Debug)]
//! # struct UserIsNotAdminError;
//! # impl std::error::Error for UserIsNotAdminError {}
//! # impl std::fmt::Display for UserIsNotAdminError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user is not an admin")
//! # }
//! # }
//! # impl<State> StateFilter<State, User> for UserIsAdmin {
//! # type ValidOutput = AdminUser;
//! # type Error = UserIsNotAdminError;
//! # fn filter(state: &State, user: User) -> Result<Self::ValidOutput, Self::Error> {
//! # if user.username == "ADMIN" {
//! # Ok(AdminUser(user))
//! # } else {
//! # Err(UserIsNotAdminError)
//! # }
//! # }
//! # }
//! #
//! struct RemoveAdmin;
//! impl ValidAction<UserStorage, UserID> for RemoveAdmin {
//! // To chain filters, use `Condition`.
//! type Filter = (
//! // <Input, Filter>
//! Condition<UserID, UserExists>,
//! // Previous filter outputs a `User`,
//! // so next filter can take `User` as input.
//! Condition<User, UserIsAdmin>,
//! );
//! type Output = UserStorage;
//! fn with_valid_input(
//! self,
//! mut state: UserStorage,
//! // The final output from the filters is an `AdminUser`.
//! admin_user: <Self::Filter as StateFilter<UserStorage, UserID>>::ValidOutput,
//! ) -> Self::Output {
//! let _ = state.maps.remove(&admin_user.0.id).unwrap();
//! state
//! }
//! }
//! ```
//! Now, let's put it all together. We create the state `UserStorage`,
//! and then use [`Validator::try_new`] to run our filters. An error is returned if any of the filters fail,
//! otherwise we get a validator that we can run an action on:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! # #[derive(Clone)]
//! # struct User {
//! # id: UserID,
//! # username: String,
//! # }
//! # #[derive(Default)]
//! # struct UserStorage {
//! # maps: HashMap<UserID, User>,
//! # }
//! # struct AdminUser(User);
//! # struct UserExists;
//! # #[derive(Debug)]
//! # struct UserDoesNotExistError;
//! # impl std::error::Error for UserDoesNotExistError {}
//! # impl std::fmt::Display for UserDoesNotExistError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user does not exist")
//! # }
//! # }
//! # impl StateFilter<UserStorage, UserID> for UserExists {
//! # type ValidOutput = User;
//! # type Error = UserDoesNotExistError;
//! # fn filter(state: &UserStorage, user_id: UserID) -> Result<Self::ValidOutput, Self::Error> {
//! # if let Some(user) = state.maps.get(&user_id) {
//! # Ok(user.clone())
//! # } else {
//! # Err(UserDoesNotExistError)
//! # }
//! # }
//! # }
//! # struct UserIsAdmin;
//! # #[derive(Debug)]
//! # struct UserIsNotAdminError;
//! # impl std::error::Error for UserIsNotAdminError {}
//! # impl std::fmt::Display for UserIsNotAdminError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user is not an admin")
//! # }
//! # }
//! # impl<State> StateFilter<State, User> for UserIsAdmin {
//! # type ValidOutput = AdminUser;
//! # type Error = UserIsNotAdminError;
//! # fn filter(state: &State, user: User) -> Result<Self::ValidOutput, Self::Error> {
//! # if user.username == "ADMIN" {
//! # Ok(AdminUser(user))
//! # } else {
//! # Err(UserIsNotAdminError)
//! # }
//! # }
//! # }
//! #
//! # struct RemoveAdmin;
//! # impl ValidAction<UserStorage, UserID> for RemoveAdmin {
//! # type Filter = (
//! # Condition<UserID, UserExists>,
//! # Condition<User, UserIsAdmin>,
//! # );
//! # type Output = UserStorage;
//! # fn with_valid_input(
//! # self,
//! # mut state: UserStorage,
//! # admin_user: <Self::Filter as StateFilter<UserStorage, UserID>>::ValidOutput,
//! # ) -> Self::Output {
//! # let _ = state.maps.remove(&admin_user.0.id).unwrap();
//! # state
//! # }
//! # }
//! #
//! // State setup
//! let mut user_storage = UserStorage::default();
//! user_storage.maps.insert(UserID(0), User {
//! id: UserID(0),
//! username: "ADMIN".to_string(),
//! });
//!
//! // Create validator which will validate the input.
//! // No error is returned if validation succeeds.
//! let validator = Validator::try_new(user_storage, UserID(0)).expect("admin user did not exist");
//!
//! // Execute an action which requires the state and input above.
//! let user_storage = validator.execute(RemoveAdmin);
//!
//! assert!(user_storage.maps.is_empty());
//! ```
//! ---
//! Another example using `UserExists` filter, and `UserIsAdmin` filter in the body of the [`ValidAction`]:
//! ```
//! # use std::collections::{HashSet, HashMap};
//! # use state_validation::{Validator, ValidAction, StateFilter, Condition};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! #
//! # #[derive(Default)]
//! # struct UserStorage {
//! # maps: HashMap<UserID, User>,
//! # }
//! # #[derive(Clone)]
//! # struct User {
//! # id: UserID,
//! # username: String,
//! # }
//! #
//! # struct AdminUser(User);
//! #
//! # struct UserExists;
//! # #[derive(Debug)]
//! # struct UserDoesNotExistError;
//! # impl std::error::Error for UserDoesNotExistError {}
//! # impl std::fmt::Display for UserDoesNotExistError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user does not exist")
//! # }
//! # }
//! # impl StateFilter<UserStorage, UserID> for UserExists {
//! # type ValidOutput = User;
//! # type Error = UserDoesNotExistError;
//! # fn filter(state: &UserStorage, user_id: UserID) -> Result<Self::ValidOutput, Self::Error> {
//! # if let Some(user) = state.maps.get(&user_id) {
//! # Ok(user.clone())
//! # } else {
//! # Err(UserDoesNotExistError)
//! # }
//! # }
//! # }
//! #
//! # struct UserIsAdmin;
//! # #[derive(Debug)]
//! # struct UserIsNotAdminError;
//! # impl std::error::Error for UserIsNotAdminError {}
//! # impl std::fmt::Display for UserIsNotAdminError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user is not an admin")
//! # }
//! # }
//! # impl StateFilter<UserStorage, User> for UserIsAdmin {
//! # type ValidOutput = AdminUser;
//! # type Error = UserIsNotAdminError;
//! # fn filter(state: &UserStorage, user: User) -> Result<Self::ValidOutput, Self::Error> {
//! # if user.username == "ADMIN" {
//! # Ok(AdminUser(user))
//! # } else {
//! # Err(UserIsNotAdminError)
//! # }
//! # }
//! # }
//! #
//! enum Privilege {
//! None,
//! Admin,
//! }
//!
//! struct UpdateUserPrivilege(Privilege);
//! impl ValidAction<UserStorage, UserID> for UpdateUserPrivilege {
//! type Filter = UserExists;
//! type Output = UserStorage;
//! fn with_valid_input(
//! self,
//! mut state: UserStorage,
//! mut user: <Self::Filter as StateFilter<UserStorage, UserID>>::ValidOutput,
//! ) -> Self::Output {
//! match self.0 {
//! Privilege::None => {
//! if let Ok(AdminUser(mut user)) = UserIsAdmin::filter(&state, user) {
//! user.username = "NOT_ADMIN".to_string();
//! let _ = state.maps.insert(user.id, user);
//! }
//! }
//! Privilege::Admin => {
//! user.username = "ADMIN".to_string();
//! let _ = state.maps.insert(user.id, user);
//! }
//! }
//! state
//! }
//! }
//!
//! # let mut user_storage = UserStorage::default();
//! # user_storage.maps.insert(UserID(0), User {
//! # id: UserID(0),
//! # username: "ADMIN".to_string(),
//! # });
//! # assert_eq!(user_storage.maps.len(), 1);
//! # let user = user_storage.maps.get(&UserID(0));
//! # assert!(user.is_some());
//! # assert_eq!(user.unwrap().username, "ADMIN");
//!
//! // This `Validator` has its generic `Filter` parameter implicitly changed
//! // based on what action we call. On a type level, this is a different type
//! // than the validator in the previous example even though we write the same code
//! // due to its `Filter` generic parameter being different.
//! let validator = Validator::try_new(user_storage, UserID(0)).expect("user did not exist");
//!
//! let user_storage = validator.execute(UpdateUserPrivilege(Privilege::None));
//!
//! # assert_eq!(user_storage.maps.len(), 1);
//! let user = user_storage.maps.get(&UserID(0));
//! # assert!(user.is_some());
//! assert_eq!(user.unwrap().username, "NOT_ADMIN");
//! ```
//!
//! ## [`StateFilterInputConversion`] & [`StateFilterInputCombination`]
//! Automatic implementations of [`StateFilterInputConversion`] and [`StateFilterInputCombination`]
//! are generated with the [`StateFilterConversion`] derive macro.
//! Example:
//! ```
//! # use state_validation::StateFilterConversion;
//! # struct UserID(usize);
//! # struct User;
//! #[derive(StateFilterConversion)]
//! struct UserWithUserName {
//! #[conversion(User)]
//! user_id: UserID,
//! username: String,
//! }
//! ```
//! Now, `UserWithUsername` can be broken down into `User`, `UserID`, and `String`.
//! Take advantage of the newtype pattern to breakdown the input further.
//! For example, instead of having username as a `String`, use:
//! ```
//! struct Username(String);
//! ```
//! This way, the compiler can differentiate between a `String` and a `Username`.
//!
//!
//! The [`StateFilterInputConversion`] and [`StateFilterInputCombination`] traits work together
//! to allow splitting the input down into its parts and then back together.
//!
//! The usefulness of this is, if a [`StateFilter`] only requires a part of the input,
//! [`StateFilterInputConversion`] can split it down to just that part, and leave the rest in [`StateFilterInputConversion::Remainder`]
//! which will be combined with the output of the filter. And, each consecutive filter can split
//! whatever input they desire and combine their output with the remainder they did not touch.
//!
//! Here is an example with manual implementations: Assume we wanted to change the username of a user,
//! if its `UserID` and current username matched that of the input.
//! First, let's create its input:
//! ```
//! # struct UserID(usize);
//! struct UserWithUsername {
//! user_id: UserID,
//! username: String,
//! }
//! ```
//!
//! We want to check if a user with `UserID` exists, and their current username is `username`, and then update their username.
//! We already have a filter that will check if a user exists. It is called: `UserExists`.
//! But, `UserExists` does not take `UserWithUsername` as an input. It only takes `UserID` as input.
//! `UserWithUsername` does contain a `UserID`. So, we should be able to retrieve the `UserID`,
//! pass it into `UserExists`, then reconstruct the output of `UserExists` with the leftover `username`.
//! The first part of solving this issue is input conversion into `UserID`, so we implement [`StateFilterInputConversion`]:
//! ```
//! # use state_validation::StateFilterInputConversion;
//! # struct UserID(usize);
//! # struct UserWithUsername {
//! # user_id: UserID,
//! # username: String,
//! # }
//! struct UsernameForUserID(String);
//! impl StateFilterInputConversion<UserID> for UserWithUsername {
//! type Remainder = UsernameForUserID;
//! fn split_take(self) -> (UserID, Self::Remainder) {
//! (self.user_id, UsernameForUserID(self.username))
//! }
//! }
//! ```
//! Notice in the above code, within `split_take`, the first element of the tuple is the input our `UserExists` filter cares about.
//! However, for the `Remainder`, we have a newtype which stores the leftover to combine later with the output of `UserExists`.
//! ```
//! # use state_validation::StateFilterInputCombination;
//! # struct User;
//! # struct UserID(usize);
//! # struct UsernameForUserID(String);
//! struct UsernameForUser {
//! user: User,
//! username: String,
//! }
//! impl StateFilterInputCombination<User> for UsernameForUserID {
//! type Combined = UsernameForUser;
//! fn combine(self, user: User) -> Self::Combined {
//! UsernameForUser {
//! user,
//! username: self.0,
//! }
//! }
//! }
//! ```
//! Now, the combination of `UserExists`'s output (which is a `User`) and the leftover `username`,
//! results in a new struct called `UsernameForUser`.
//!
//! Now, we need a new filter that checks if the username of the `User` is equal to `username`.
//! ```
//! # use state_validation::StateFilter;
//! # struct User {
//! # username: String,
//! # }
//! # struct UsernameForUser {
//! # user: User,
//! # username: String,
//! # }
//! struct UsernameEquals;
//! # #[derive(Debug)]
//! # struct UsernameIsNotEqual;
//! # impl std::error::Error for UsernameIsNotEqual {}
//! # impl std::fmt::Display for UsernameIsNotEqual {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "username is not equal")
//! # }
//! # }
//! impl<State> StateFilter<State, UsernameForUser> for UsernameEquals {
//! type ValidOutput = User;
//! type Error = UsernameIsNotEqual;
//! fn filter(state: &State, value: UsernameForUser) -> Result<Self::ValidOutput, Self::Error> {
//! if value.user.username == value.username {
//! Ok(value.user)
//! } else {
//! Err(UsernameIsNotEqual)
//! }
//! }
//! }
//! ```
//!
//! Finally, we can run our action to update the user's username:
//! ```
//! # use std::collections::HashMap;
//! # use state_validation::{ValidAction, Condition, StateFilter, StateFilterInputConversion, StateFilterInputCombination};
//! # #[derive(Hash, PartialEq, Eq, Clone, Copy)]
//! # struct UserID(usize);
//! # #[derive(Clone)]
//! # struct User {
//! # id: UserID,
//! # username: String,
//! # }
//! # #[derive(Default)]
//! # struct UserStorage {
//! # maps: HashMap<UserID, User>,
//! # }
//! # struct UserExists;
//! # #[derive(Debug)]
//! # struct UserDoesNotExistError;
//! # impl std::error::Error for UserDoesNotExistError {}
//! # impl std::fmt::Display for UserDoesNotExistError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "user does not exist")
//! # }
//! # }
//! # impl StateFilter<UserStorage, UserID> for UserExists {
//! # type ValidOutput = User;
//! # type Error = UserDoesNotExistError;
//! # fn filter(state: &UserStorage, user_id: UserID) -> Result<Self::ValidOutput, Self::Error> {
//! # if let Some(user) = state.maps.get(&user_id) {
//! # Ok(user.clone())
//! # } else {
//! # Err(UserDoesNotExistError)
//! # }
//! # }
//! # }
//! # struct UserWithUsername {
//! # user_id: UserID,
//! # username: String,
//! # }
//! # struct UsernameForUserID(String);
//! # impl StateFilterInputConversion<UserID> for UserWithUsername {
//! # type Remainder = UsernameForUserID;
//! # fn split_take(self) -> (UserID, Self::Remainder) {
//! # (self.user_id, UsernameForUserID(self.username))
//! # }
//! # }
//! # struct UsernameForUser {
//! # user: User,
//! # username: String,
//! # }
//! # impl StateFilterInputCombination<User> for UsernameForUserID {
//! # type Combined = UsernameForUser;
//! # fn combine(self, user: User) -> Self::Combined {
//! # UsernameForUser {
//! # user,
//! # username: self.0,
//! # }
//! # }
//! # }
//! # struct UsernameEquals;
//! # #[derive(Debug)]
//! # struct UsernameIsNotEqual;
//! # impl std::error::Error for UsernameIsNotEqual {}
//! # impl std::fmt::Display for UsernameIsNotEqual {
//! # fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! # write!(f, "username is not equal")
//! # }
//! # }
//! # impl<State> StateFilter<State, UsernameForUser> for UsernameEquals {
//! # type ValidOutput = User;
//! # type Error = UsernameIsNotEqual;
//! # fn filter(state: &State, value: UsernameForUser) -> Result<Self::ValidOutput, Self::Error> {
//! # if value.user.username == value.username {
//! # Ok(value.user)
//! # } else {
//! # Err(UsernameIsNotEqual)
//! # }
//! # }
//! # }
//! struct UpdateUsername {
//! new_username: String,
//! }
//! impl ValidAction<UserStorage, UserWithUsername> for UpdateUsername {
//! type Filter = (
//! Condition<UserID, UserExists>,
//! Condition<UsernameForUser, UsernameEquals>,
//! );
//! type Output = UserStorage;
//! fn with_valid_input(
//! self,
//! mut state: UserStorage,
//! mut user: <Self::Filter as StateFilter<UserStorage, UserWithUsername>>::ValidOutput,
//! ) -> Self::Output {
//! user.username = self.new_username;
//! let _ = state.maps.insert(user.id, user);
//! state
//! }
//! }
//! ```
//!
//! ## Soundness Rules
//! [`Validator::try_new`] takes ownership of the `state` to disallow consecutive
//! [`Validator::execute`] calls because an action is assumed to mutate the `state`.
//! Since an action is assumed to mutate the `state`, any validators using the same `state`
//! cannot be created.
//!
//! It is up to you to make sure the filters properly validate what they promise.
//!
//! ## Limitations
//! Currently, the amount of filters that can be chained is eight.
//! The reason for this is because of variadics not being supported as of Rust 2024.
//! Having no more than eight implementations is arbitrary because having more than eight filters is unlikely.
//! There is no reason not to implement more in the future, if more than eight filters are required.
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;