From 042d7dd8637c21357fa3de38ebeab06b9088e6bc Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Feb 2024 12:35:43 +0100 Subject: [PATCH 1/3] Add `Command::Result` --- crates/fj-core/src/layers/layer.rs | 8 ++++++++ crates/fj-core/src/layers/objects.rs | 1 + crates/fj-core/src/layers/validation.rs | 1 + 3 files changed, 10 insertions(+) diff --git a/crates/fj-core/src/layers/layer.rs b/crates/fj-core/src/layers/layer.rs index 4f4148a9a..ec2e7491b 100644 --- a/crates/fj-core/src/layers/layer.rs +++ b/crates/fj-core/src/layers/layer.rs @@ -63,6 +63,14 @@ where /// A command that encodes a request to update a layer's state pub trait Command { + /// The direct result of processing a command that is returned to the caller + /// + /// Changes to the state that result from a command are encoded as events + /// (see [`Command::Event`]). In addition to that, a command may return + /// information to the caller, and `Result` defines the type of that + /// information. + type Result; + /// An event that encodes a change to the state /// /// Events are produced by [`Command::decide`] and processed by diff --git a/crates/fj-core/src/layers/objects.rs b/crates/fj-core/src/layers/objects.rs index b5cff0ea2..f60ede341 100644 --- a/crates/fj-core/src/layers/objects.rs +++ b/crates/fj-core/src/layers/objects.rs @@ -36,6 +36,7 @@ pub struct InsertObject { } impl Command for InsertObject { + type Result = (); type Event = InsertObject; fn decide(self, _: &Objects, events: &mut Vec) { diff --git a/crates/fj-core/src/layers/validation.rs b/crates/fj-core/src/layers/validation.rs index 96071ee9e..c43fca8a5 100644 --- a/crates/fj-core/src/layers/validation.rs +++ b/crates/fj-core/src/layers/validation.rs @@ -21,6 +21,7 @@ impl Layer { } impl Command for InsertObject { + type Result = (); type Event = ValidationFailed; fn decide(self, state: &Validation, events: &mut Vec) { From 7154aac10ef6c255cbadef7f801d292e1d7bf355 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Feb 2024 12:36:33 +0100 Subject: [PATCH 2/3] Return `Command::Result` from `Command::decide` --- crates/fj-core/src/layers/layer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-core/src/layers/layer.rs b/crates/fj-core/src/layers/layer.rs index ec2e7491b..71bf360c3 100644 --- a/crates/fj-core/src/layers/layer.rs +++ b/crates/fj-core/src/layers/layer.rs @@ -81,7 +81,7 @@ pub trait Command { /// /// If the command must result in changes to the state, any number of events /// that describe these state changes can be produced. - fn decide(self, state: &S, events: &mut Vec); + fn decide(self, state: &S, events: &mut Vec) -> Self::Result; } /// An event that encodes a change to a layer's state From 8222147bfd475b49740aae57f16414d663a603cd Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Feb 2024 12:36:48 +0100 Subject: [PATCH 3/3] Return `Command::Result` from `Layer::process` --- crates/fj-core/src/layers/layer.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/fj-core/src/layers/layer.rs b/crates/fj-core/src/layers/layer.rs index 71bf360c3..140f1049b 100644 --- a/crates/fj-core/src/layers/layer.rs +++ b/crates/fj-core/src/layers/layer.rs @@ -27,15 +27,21 @@ impl Layer { /// /// The command is processed synchronously. When this method returns, the /// state has been updated. - pub fn process(&mut self, command: C, events: &mut Vec) + pub fn process( + &mut self, + command: C, + events: &mut Vec, + ) -> C::Result where C: Command, { - command.decide(&self.state, events); + let result = command.decide(&self.state, events); for event in events { event.evolve(&mut self.state); } + + result } /// Drop this instance, returning the wrapped state