allframe 0.1.28

Complete Rust web framework with built-in HTTP/2 server, REST/GraphQL/gRPC, compile-time DI, CQRS - TDD from day zero
Documentation
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
# Migration Guide: From Legacy Macros to Clean Architecture

**Status**: ✅ Production Ready
**Applies to**: AllFrame v0.1.13+
**Breaking Changes**: None (100% backward compatible)

---

## Overview

AllFrame v0.1.13 introduces a Clean Architecture-compliant resilience system while maintaining 100% backward compatibility. This guide helps you migrate from the old `#[retry]`, `#[circuit_breaker]`, and `#[rate_limited]` macros to the new architectural patterns.

**Key Benefits of Migration:**
- **Clean Architecture**: Domain logic stays pure and testable
-**Better Performance**: Reduced overhead and improved observability
-**Enhanced Flexibility**: Runtime policy changes and better configuration
-**Future-Proof**: Extensible for new resilience patterns

---

## Quick Assessment: Do You Need to Migrate?

### **Immediate Migration Recommended If:**
- You're starting a new project
- You want Clean Architecture compliance
- You need advanced resilience features (timeouts, bulkhead, adaptive policies)
- You want better testability and observability

### **Optional Migration If:**
- You have existing code that works
- You don't need advanced features
- You're on a tight timeline

### **No Migration Needed If:**
- Your current code works and meets requirements
- Legacy macros are deprecated but still functional

---

## Migration Strategies

### Strategy 1: Gradual Migration (Recommended)

Migrate one service or module at a time:

```rust
// Phase 1: Keep existing code working
#[retry(max_retries = 3)]
async fn legacy_payment_processing() { /* existing code */ }

// Phase 2: Add new implementation alongside
async fn new_payment_processing() -> Result<PaymentResult, PaymentError> {
    // New Clean Architecture implementation
}

// Phase 3: Gradually migrate callers
// Phase 4: Remove legacy code
```

### Strategy 2: Big Bang Migration

Replace all macros at once (higher risk, bigger reward):

```bash
# Use automated migration script (future feature)
find src/ -name "*.rs" -exec sed -i 's/#\[retry(/#[resilient(retry(/g' {} \;
```

### Strategy 3: Hybrid Approach

Critical paths use new architecture, non-critical keep legacy:

```rust
// Critical payment processing - new architecture
async fn process_payment() -> Result<(), Error> {
    // Clean, testable, observable
}

// Background job - keep legacy for now
#[retry(max_retries = 5)]
async fn send_notification() { /* low priority */ }
```

---

## Step-by-Step Migration

### Step 1: Enable New Features

Update your `Cargo.toml`:

```toml
[dependencies]
allframe-core = { version = "0.1.13", features = [
    "resilience",      # Required for new architecture
    "resilience-tokio" # Optional: advanced async features
] }
```

### Step 2: Identify Migration Candidates

Find all uses of legacy macros:

```bash
# Find retry macros
grep -r "#\[retry" src/

# Find circuit breaker macros
grep -r "#\[circuit_breaker" src/

# Find rate limiting macros
grep -r "#\[rate_limited" src/
```

### Step 3: Convert Domain Logic

**Before (Legacy):**
```rust
#[retry(max_retries = 3)]
async fn process_payment(payment: Payment) -> Result<PaymentResult, PaymentError> {
    // Business logic mixed with resilience concerns
    validate_payment(&payment)?;
    charge_credit_card(&payment).await?;
    update_database(&payment).await?;
    send_notification(&payment).await?;
    Ok(PaymentResult::Success)
}
```

**After (Clean Architecture):**
```rust
use allframe_core::domain::resilience::{ResilientOperation, ResiliencePolicy, policies};

struct PaymentProcessor {
    payment: Payment,
}

impl ResilientOperation<PaymentResult, PaymentError> for PaymentProcessor {
    fn resilience_policy(&self) -> ResiliencePolicy {
        policies::combine(vec![
            policies::retry(3),
            policies::circuit_breaker(5, 30),
            policies::timeout(30),
        ])
    }

    async fn execute(&self) -> Result<PaymentResult, PaymentError> {
        // Pure business logic - no infrastructure concerns
        validate_payment(&self.payment)?;
        charge_credit_card(&self.payment).await?;
        update_database(&self.payment).await?;
        send_notification(&self.payment).await?;
        Ok(PaymentResult::Success)
    }

    fn operation_id(&self) -> &str {
        "payment_processor"
    }
}
```

### Step 4: Update Application Layer

**Before:**
```rust
#[handler]
async fn payment_handler(request: PaymentRequest) -> Result<PaymentResponse, ApiError> {
    // Direct call to resilient function
    let result = process_payment(request.into()).await?;
    Ok(result.into())
}
```

**After:**
```rust
use allframe_core::application::resilience::{ResilienceOrchestrator, DefaultResilienceOrchestrator};

#[handler]
async fn payment_handler(
    request: PaymentRequest,
    orchestrator: &ResilienceOrchestrator, // Injected via DI
) -> Result<PaymentResponse, ApiError> {
    let processor = PaymentProcessor::from(request);

    // Application layer orchestrates resilience
    let result = orchestrator.execute_operation(processor).await?;
    Ok(result.into())
}
```

### Step 5: Update Tests

**Before:**
```rust
#[tokio::test]
async fn test_payment_processing() {
    // Hard to test - resilience logic is baked in
    let result = process_payment(test_payment()).await;
    assert!(result.is_ok());
}
```

**After:**
```rust
#[tokio::test]
async fn test_payment_business_logic() {
    // Test pure business logic in isolation
    let processor = PaymentProcessor::new(test_payment());
    let result = processor.execute().await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_payment_resilience_policy() {
    // Test resilience configuration
    let processor = PaymentProcessor::new(test_payment());
    let policy = processor.resilience_policy();

    match policy {
        ResiliencePolicy::Combined { policies } => {
            assert_eq!(policies.len(), 3);
            // Verify each policy is configured correctly
        }
        _ => panic!("Expected combined policy"),
    }
}

#[tokio::test]
async fn test_payment_with_orchestration() {
    // Test full integration
    let orchestrator = DefaultResilienceOrchestrator::new();
    let processor = PaymentProcessor::new(test_payment());

    let result = orchestrator.execute_operation(processor).await;
    assert!(result.is_ok());

    // Verify metrics were collected
    let metrics = orchestrator.metrics();
    assert_eq!(metrics.total_operations, 1);
}
```

---

## Specific Macro Migrations

### `#[retry]` Migration

**Legacy:**
```rust
#[retry(max_retries = 3)]
async fn api_call() -> Result<Data, Error> {
    call_external_api().await
}
```

**New Architecture:**
```rust
use allframe_core::domain::resilience::{ResilientOperation, policies};

struct ApiCaller;

impl ResilientOperation<Data, Error> for ApiCaller {
    fn resilience_policy(&self) -> ResiliencePolicy {
        policies::retry(3)
    }

    async fn execute(&self) -> Result<Data, Error> {
        call_external_api().await
    }
}

// Usage
let result = orchestrator.execute_operation(ApiCaller).await;
```

### `#[circuit_breaker]` Migration

**Legacy:**
```rust
#[circuit_breaker(failure_threshold = 5, recovery_timeout = 30)]
async fn database_query() -> Result<Records, DbError> {
    query_database().await
}
```

**New Architecture:**
```rust
struct DatabaseQuery {
    query: String,
}

impl ResilientOperation<Records, DbError> for DatabaseQuery {
    fn resilience_policy(&self) -> ResiliencePolicy {
        policies::circuit_breaker(5, 30)
    }

    async fn execute(&self) -> Result<Records, DbError> {
        query_database(&self.query).await
    }
}
```

### `#[rate_limited]` Migration

**Legacy:**
```rust
#[rate_limited(requests_per_second = 100)]
async fn api_endpoint() -> Result<Response, ApiError> {
    process_request().await
}
```

**New Architecture:**
```rust
struct ApiEndpoint {
    request: Request,
}

impl ResilientOperation<Response, ApiError> for ApiEndpoint {
    fn resilience_policy(&self) -> ResiliencePolicy {
        policies::rate_limit(100)
    }

    async fn execute(&self) -> Result<Response, ApiError> {
        process_request(&self.request).await
    }
}
```

### Complex Combined Policies

**Legacy:**
```rust
#[retry(max_retries = 3)]
#[circuit_breaker(failure_threshold = 5, recovery_timeout = 30)]
#[rate_limited(requests_per_second = 50)]
async fn complex_operation() -> Result<(), Error> {
    do_something().await
}
```

**New Architecture:**
```rust
struct ComplexOperation;

impl ResilientOperation<(), Error> for ComplexOperation {
    fn resilience_policy(&self) -> ResiliencePolicy {
        policies::combine(vec![
            policies::retry(3),
            policies::circuit_breaker(5, 30),
            policies::rate_limit(50),
        ])
    }

    async fn execute(&self) -> Result<(), Error> {
        do_something().await
    }
}
```

---

## Advanced Migration Patterns

### Policy Factories

Create reusable policy configurations:

```rust
mod resilience_policies {
    use allframe_core::domain::resilience::{ResiliencePolicy, policies};

    pub fn external_api_policy() -> ResiliencePolicy {
        policies::combine(vec![
            policies::retry(2),
            policies::circuit_breaker(3, 60),
            policies::timeout(10),
        ])
    }

    pub fn database_policy() -> ResiliencePolicy {
        policies::combine(vec![
            policies::retry(3),
            policies::circuit_breaker(5, 30),
            policies::timeout(5),
        ])
    }

    pub fn cache_policy() -> ResiliencePolicy {
        policies::retry(1) // Cache failures are usually fast
    }
}
```

### Context-Aware Policies

Policies based on runtime context:

```rust
impl ResilientOperation<Data, Error> for ContextualOperation {
    fn resilience_policy(&self) -> ResiliencePolicy {
        match self.priority {
            Priority::High => policies::combine(vec![
                policies::retry(5),  // More retries for high priority
                policies::circuit_breaker(10, 30),
                policies::timeout(60), // Longer timeout
            ]),
            Priority::Low => policies::retry(1), // Fail fast for low priority
        }
    }
}
```

### Dependency Injection Integration

Integrate with DI containers:

```rust
use allframe_core::application::resilience::ResilienceOrchestrator;

#[injectable]
struct PaymentService {
    orchestrator: Arc<dyn ResilienceOrchestrator>,
    payment_repo: Arc<dyn PaymentRepository>,
}

impl PaymentService {
    async fn process(&self, payment: Payment) -> Result<(), Error> {
        let processor = PaymentProcessor {
            payment,
            repo: self.payment_repo.clone(),
        };

        self.orchestrator.execute_operation(processor).await
    }
}
```

---

## Testing Migration

### Test Compatibility

Verify that migrated code maintains the same behavior:

```rust
#[tokio::test]
async fn test_behavior_unchanged() {
    // Test with new architecture
    let new_result = orchestrator.execute_operation(NewOperation).await;

    // Simulate old behavior (without actual macro)
    let old_result = retry_operation(|| old_implementation()).await;

    // Results should be equivalent
    assert_eq!(new_result, old_result);
}
```

### Performance Regression Testing

Ensure migration doesn't degrade performance:

```rust
use criterion::{criterion_group, criterion_main, Criterion};

fn benchmark_migration(c: &mut Criterion) {
    let orchestrator = DefaultResilienceOrchestrator::new();

    // Benchmark new architecture
    c.bench_function("new_architecture", |b| {
        b.iter(|| async {
            orchestrator
                .execute_operation(TestOperation)
                .await
        })
    });

    // Benchmark simulated old architecture
    c.bench_function("old_architecture", |b| {
        b.iter(|| async {
            // Simulate old macro behavior
            retry_operation(|| test_operation()).await
        })
    });
}
```

---

## Rollback Strategy

If migration causes issues, you can rollback:

### Temporary Rollback
```rust
// Keep both implementations during transition
async fn payment_processing(request: PaymentRequest) -> Result<(), Error> {
    if std::env::var("USE_NEW_ARCHITECTURE").unwrap_or("false".to_string()) == "true" {
        // New architecture
        orchestrator.execute_operation(PaymentProcessor::from(request)).await
    } else {
        // Legacy fallback
        legacy_payment_processing(request).await
    }
}
```

### Feature Flags
```toml
[features]
legacy-macros = ["allframe-macros/legacy"]  # If we add legacy feature
new-architecture = ["resilience"]           # Default for new projects
```

---

## Troubleshooting Migration

### Common Issues

1. **"Type doesn't implement ResilientOperation"**
   ```rust
   // Fix: Implement the trait
   impl ResilientOperation<ResultType, ErrorType> for YourType {
       fn resilience_policy(&self) -> ResiliencePolicy { /* ... */ }
       async fn execute(&self) -> Result<ResultType, ErrorType> { /* ... */ }
   }
   ```

2. **"Orchestrator not available"**
   ```rust
   // Fix: Inject orchestrator via DI
   fn new(orchestrator: Arc<dyn ResilienceOrchestrator>) -> Self {
       Self { orchestrator }
   }
   ```

3. **Performance degradation**
   ```rust
   // Fix: Check metrics and optimize policies
   let metrics = orchestrator.metrics();
   println!("Overhead: {:?}", metrics);
   ```

### Debug Mode

Enable detailed logging during migration:

```rust
use tracing_subscriber;

std::env::set_var("RUST_LOG", "allframe_core=debug");
tracing_subscriber::init();

// Now you'll see:
// - Policy application details
// - Orchestration decisions
// - Performance metrics
```

---

## Success Metrics

Track these metrics during migration:

- **Test Pass Rate**: All tests should pass post-migration
- **Performance**: <5% degradation acceptable, <1% preferred
- **Error Rates**: Should not increase
- **Code Coverage**: Should improve (domain logic easier to test)
- **Build Time**: <10% increase acceptable

---

## Support and Resources

### Getting Help

1. **Documentation**: [Resilience Architecture Guide]RESILIENCE_ARCHITECTURE.md
2. **Examples**: Check `examples/` directory for complete implementations
3. **Issues**: File bugs with "migration" label
4. **Discussions**: Use GitHub Discussions for migration questions

### Example Projects

- `examples/resilient_payment_service.rs` - Complete payment service migration
- `examples/microservice_resilience.rs` - Inter-service communication patterns
- `examples/legacy_migration.rs` - Side-by-side comparison

---

## Timeline Recommendations

### Week 1-2: Planning and Assessment
- Audit current macro usage
- Plan migration strategy
- Set up monitoring

### Week 3-4: Core Migration
- Migrate critical services first
- Update tests incrementally
- Monitor performance metrics

### Week 5-6: Advanced Features
- Implement custom policies
- Add observability
- Performance optimization

### Week 7-8: Cleanup and Validation
- Remove legacy code
- Final testing
- Documentation updates

---

**Migration Complete**: Your application now uses Clean Architecture patterns while maintaining all existing functionality. The new system is more testable, maintainable, and ready for future enhancements.

---

**Next**: [Resilience Architecture Guide](RESILIENCE_ARCHITECTURE.md) | [Configuration Reference](CONFIGURATION.md)