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
use ;
use crateState;
type Decision<S, D> = ;
type Decide<C, S, D> =
dyn Fn + Send + Sync;
/// A decision on a command, given the state of its subject: the events to
/// write, or an error if the command is refused.
///
/// Create one from a function that receives the command and the state, and
/// returns the events in a future, so that it can load what else it needs to
/// decide, such as the state of another subject:
///
/// ```
/// # use architecturekit::{Event, Events, State};
/// # use serde::{Deserialize, Serialize};
/// # #[derive(Serialize, Deserialize, Event)]
/// # #[event(type = "io.eventsourcingdb.library.book-borrowed")]
/// # struct BookBorrowed { reader: String }
/// # #[derive(Events)]
/// # enum BookEvent { Borrowed(BookBorrowed) }
/// # #[derive(Clone, Default)]
/// # struct Book { is_borrowed: bool }
/// # impl State for Book {
/// # type Event = BookEvent;
/// # fn evolve(self, event: &BookEvent) -> Self { Book { is_borrowed: true } }
/// # }
/// # #[derive(Clone)]
/// # struct BorrowBook { book_id: String, reader: String }
/// use architecturekit::{Decider, DomainError};
///
/// let borrow_book = Decider::new(|cmd: BorrowBook, book: Book| async move {
/// if book.is_borrowed {
/// return Err(DomainError::new(format!("book {} is already borrowed", cmd.book_id)));
/// }
///
/// Ok(vec![BookEvent::Borrowed(BookBorrowed { reader: cmd.reader })])
/// });
/// ```
///
/// The types of the command, the state and the error come from the function,
/// so that none of them has to be named. A decider that never refuses names
/// its error type, as in `Decider::<_, _, DomainError>::new(…)`.
///
/// The function takes the command and the state rather than references to
/// them, so that its future can be sent across threads, which a web server
/// needs.