Skip to content

Commit e2cdbe7

Browse files
committed
Improve devex ergonomics for impl Command
1 parent 8cc5693 commit e2cdbe7

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

src/command.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use crate::event::Event;
33
use crate::event_store::EventStreamId;
44
use std::fmt::Debug;
55

6-
pub trait Command<E: Event>: Clone {
7-
type State: AggregateState<E>;
6+
pub trait Command: Clone {
7+
type Event: Event;
8+
type State: AggregateState<Self::Event>;
89
type Error: std::error::Error + Send + Sync + 'static;
910

10-
fn handle(&self) -> Result<Vec<E>, Self::Error>;
11+
fn handle(&self) -> Result<Vec<Self::Event>, Self::Error>;
1112

1213
fn event_stream_id(&self) -> EventStreamId;
1314

@@ -26,29 +27,14 @@ pub trait Command<E: Event>: Clone {
2627
None
2728
}
2829

29-
fn apply(&mut self, event: &E)
30+
fn apply(&mut self, event: &Self::Event)
3031
where
3132
Self: Sized,
3233
{
3334
self.set_state(self.get_state().apply(event));
3435
}
3536
}
3637

37-
impl<E: Event> Command<E> for () {
38-
type State = ();
39-
type Error = std::convert::Infallible;
40-
41-
fn handle(&self) -> Result<Vec<E>, Self::Error> {
42-
Ok(vec![])
43-
}
44-
fn event_stream_id(&self) -> EventStreamId {
45-
EventStreamId::new()
46-
}
47-
fn get_state(&self) -> Self::State {}
48-
fn set_state(&mut self, _: Self::State) {}
49-
fn mark_retry(&self) -> Self {}
50-
}
51-
5238
pub trait AggregateState<E: Event>: Debug + Sized {
5339
fn apply(&self, event: &E) -> Self;
5440
}

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub async fn execute<E, C, S>(
2020
) -> Result<(), Error>
2121
where
2222
E: Event,
23-
C: Command<E>,
23+
C: Command<Event = E>,
2424
S: EventStore,
2525
{
2626
let mut retries = 0;
@@ -168,7 +168,8 @@ mod tests {
168168
}
169169
}
170170

171-
impl Command<TestEvent> for AlwaysConflictingCommand {
171+
impl Command for AlwaysConflictingCommand {
172+
type Event = TestEvent;
172173
type State = ();
173174
type Error = Error;
174175

@@ -324,7 +325,8 @@ mod tests {
324325
state: StatefulCommandState,
325326
}
326327

327-
impl Command<TestEvent> for ConcurrentModificationCommand {
328+
impl Command for ConcurrentModificationCommand {
329+
type Event = TestEvent;
328330
type State = StatefulCommandState;
329331
type Error = Error;
330332

@@ -459,7 +461,8 @@ mod tests {
459461
id: Uuid,
460462
}
461463

462-
impl Command<TestEvent> for EventProducingCommand {
464+
impl Command for EventProducingCommand {
465+
type Event = TestEvent;
463466
type State = ();
464467
type Error = Infallible;
465468

tests/test_cases.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ impl NoopCommand {
2121
}
2222
}
2323

24-
impl Command<()> for NoopCommand {
24+
impl Command for NoopCommand {
25+
type Event = ();
2526
type State = ();
2627
type Error = Infallible;
2728

@@ -50,7 +51,8 @@ impl RejectCommand {
5051
}
5152
}
5253

53-
impl Command<()> for RejectCommand {
54+
impl Command for RejectCommand {
55+
type Event = ();
5456
type State = ();
5557
type Error = RejectCommandError;
5658

@@ -75,7 +77,8 @@ impl EventProducingCommand {
7577
}
7678
}
7779

78-
impl Command<TestEvent> for EventProducingCommand {
80+
impl Command for EventProducingCommand {
81+
type Event = TestEvent;
7982
type State = ();
8083
type Error = Infallible;
8184

@@ -132,7 +135,8 @@ impl StatefulCommand {
132135
}
133136
}
134137

135-
impl Command<TestEvent> for StatefulCommand {
138+
impl Command for StatefulCommand {
139+
type Event = TestEvent;
136140
type State = StatefulCommandState;
137141
type Error = Infallible;
138142

0 commit comments

Comments
 (0)