Skip to content

Commit 1bf684a

Browse files
committed
refactor: [#61] extract step execution result type (Proposal #2)
1 parent fb532eb commit 1bf684a

File tree

5 files changed

+47
-21
lines changed

5 files changed

+47
-21
lines changed

docs/refactors/plans/command-handlers-refactoring.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ This refactoring addresses code quality issues in `src/application/command_handl
2828
**Total Active Proposals**: 7
2929
**Total Postponed**: 2
3030
**Total Discarded**: 2
31-
**Completed**: 2
31+
**Completed**: 3
3232
**In Progress**: 0
33-
**Not Started**: 5
33+
**Not Started**: 4
3434

3535
### Phase Summary
3636

37-
- **Phase 0 - Quick Wins (High Impact, Low Effort)**: ⏳ 2/3 completed (66%)
37+
- **Phase 0 - Quick Wins (High Impact, Low Effort)**: ✅ 3/3 completed (100%)
3838
- **Phase 1 - Structural Improvements (High Impact, Medium Effort)**: ⏳ 0/2 completed (0%)
3939
- **Phase 2 - Consistency & Polish (Medium Impact, Low Effort)**: ⏳ 0/2 completed (0%)
4040

@@ -441,7 +441,8 @@ Created `TypedEnvironmentRepository` wrapper in the domain layer (`src/domain/en
441441

442442
### Proposal #2: Extract Step Execution Result Type
443443

444-
**Status**:Not Started
444+
**Status**:Completed
445+
**Commit**: c04ef8b
445446
**Impact**: 🟢🟢 Medium
446447
**Effort**: 🔵 Low
447448
**Priority**: P0
@@ -516,14 +517,14 @@ async fn execute_provisioning_with_tracking(
516517

517518
#### Implementation Checklist
518519

519-
- [ ] Create `src/application/command_handlers/common/step_tracking.rs`
520-
- [ ] Define `StepResult` type alias
521-
- [ ] Update provision handler signatures
522-
- [ ] Update configure handler signatures
523-
- [ ] Update destroy handler signatures
524-
- [ ] Verify all tests pass
525-
- [ ] Run linter
526-
- [ ] Update documentation
520+
- [x] Create type alias in `src/application/command_handlers/common/mod.rs`
521+
- [x] Define `StepResult<T, E, S>` type alias with documentation
522+
- [x] Update provision handler signatures (1 method)
523+
- [x] Update configure handler signatures (1 method)
524+
- [x] Update destroy handler signatures (1 method)
525+
- [x] Verify all tests pass (991 unit tests + 48 integration tests)
526+
- [x] Run linter (all linters pass)
527+
- [x] Update documentation (comprehensive doc comments added)
527528

528529
#### Testing Strategy
529530

src/application/command_handlers/common/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,30 @@
44
//! to reduce code duplication and improve maintainability.
55
66
pub mod failure_context;
7+
8+
/// Result type for step execution in command handlers
9+
///
10+
/// This type alias captures the common pattern used across all command handlers
11+
/// where step execution can fail with both an error and the step that was being
12+
/// executed when the failure occurred.
13+
///
14+
/// # Type Parameters
15+
///
16+
/// * `T` - The success value type (e.g., `Environment<Provisioned>` or `()`)
17+
/// * `E` - The error type (e.g., `ProvisionCommandHandlerError`)
18+
/// * `S` - The step type (e.g., `ProvisionStep`)
19+
///
20+
/// # Example
21+
///
22+
/// ```rust,ignore
23+
/// fn execute_with_tracking(
24+
/// &self,
25+
/// environment: &Environment<Provisioning>,
26+
/// ) -> StepResult<Environment<Provisioned>, ProvisionCommandHandlerError, ProvisionStep> {
27+
/// let current_step = ProvisionStep::RenderTemplates;
28+
/// self.render_templates()
29+
/// .map_err(|e| (e, current_step))?;
30+
/// // ... more steps
31+
/// }
32+
/// ```
33+
pub type StepResult<T, E, S> = Result<T, (E, S)>;

src/application/command_handlers/configure/handler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use tracing::{info, instrument};
66

77
use super::errors::ConfigureCommandHandlerError;
88
use crate::adapters::ansible::AnsibleClient;
9+
use crate::application::command_handlers::common::StepResult;
910
use crate::application::steps::{InstallDockerComposeStep, InstallDockerStep};
1011
use crate::domain::environment::repository::{EnvironmentRepository, TypedEnvironmentRepository};
1112
use crate::domain::environment::state::{ConfigureFailureContext, ConfigureStep};
@@ -137,7 +138,7 @@ impl ConfigureCommandHandler {
137138
fn execute_configuration_with_tracking(
138139
&self,
139140
environment: &Environment<Configuring>,
140-
) -> Result<Environment<Configured>, (ConfigureCommandHandlerError, ConfigureStep)> {
141+
) -> StepResult<Environment<Configured>, ConfigureCommandHandlerError, ConfigureStep> {
141142
// Track current step and execute each step
142143
// If an error occurs, we return it along with the current step
143144

src/application/command_handlers/destroy/handler.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::Arc;
55
use tracing::{info, instrument};
66

77
use super::errors::DestroyCommandHandlerError;
8+
use crate::application::command_handlers::common::StepResult;
89
use crate::application::steps::DestroyInfrastructureStep;
910
use crate::domain::environment::repository::{EnvironmentRepository, TypedEnvironmentRepository};
1011
use crate::domain::environment::{Destroyed, Environment};
@@ -201,13 +202,8 @@ impl DestroyCommandHandler {
201202
crate::domain::environment::Destroying,
202203
>,
203204
opentofu_client: &Arc<crate::adapters::tofu::client::OpenTofuClient>,
204-
) -> Result<
205-
(),
206-
(
207-
DestroyCommandHandlerError,
208-
crate::domain::environment::state::DestroyStep,
209-
),
210-
> {
205+
) -> StepResult<(), DestroyCommandHandlerError, crate::domain::environment::state::DestroyStep>
206+
{
211207
use crate::domain::environment::state::DestroyStep;
212208

213209
// Step 1: Conditionally destroy infrastructure via OpenTofu

src/application/command_handlers/provision/handler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::errors::ProvisionCommandHandlerError;
99
use crate::adapters::ansible::AnsibleClient;
1010
use crate::adapters::ssh::{SshConfig, SshCredentials};
1111
use crate::adapters::tofu::client::InstanceInfo;
12+
use crate::application::command_handlers::common::StepResult;
1213
use crate::application::steps::{
1314
ApplyInfrastructureStep, GetInstanceInfoStep, InitializeInfrastructureStep,
1415
PlanInfrastructureStep, RenderAnsibleTemplatesStep, RenderOpenTofuTemplatesStep,
@@ -175,7 +176,7 @@ impl ProvisionCommandHandler {
175176
async fn execute_provisioning_with_tracking(
176177
&self,
177178
environment: &Environment<Provisioning>,
178-
) -> Result<(Environment<Provisioned>, IpAddr), (ProvisionCommandHandlerError, ProvisionStep)>
179+
) -> StepResult<(Environment<Provisioned>, IpAddr), ProvisionCommandHandlerError, ProvisionStep>
179180
{
180181
let ssh_credentials = environment.ssh_credentials();
181182

0 commit comments

Comments
 (0)