torsh-core 0.1.2

Core types and traits for ToRSh deep learning framework
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
//! Error recovery mechanisms for graceful degradation

use crate::error::{Result, TorshError};
use std::fmt;

/// Strategy for recovering from errors
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecoveryStrategy {
    /// Fail immediately (no recovery)
    FailFast,
    /// Retry the operation with the same parameters
    Retry {
        max_attempts: usize,
        delay_ms: Option<u64>,
    },
    /// Use a fallback implementation
    Fallback,
    /// Skip the operation and continue
    Skip,
    /// Use default/safe values
    UseDefault,
    /// Custom recovery strategy
    Custom(String),
}

/// Recovery context containing information about the failed operation
#[derive(Debug, Clone)]
pub struct RecoveryContext {
    /// The original error that triggered recovery
    pub error: TorshError,
    /// Operation that failed
    pub operation: String,
    /// Number of recovery attempts made so far
    pub attempt_count: usize,
    /// Maximum number of attempts allowed
    pub max_attempts: usize,
    /// Additional context information
    pub context: std::collections::HashMap<String, String>,
}

/// Result of a recovery attempt
#[derive(Debug)]
pub enum RecoveryResult<T> {
    /// Recovery succeeded with a result
    Success(T),
    /// Recovery failed, but operation can continue with degraded functionality
    Degraded(T, String),
    /// Recovery failed, operation should be retried
    Retry,
    /// Recovery failed completely
    Failed(TorshError),
}

/// Trait for implementing error recovery mechanisms
pub trait ErrorRecovery<T> {
    /// Attempt to recover from an error
    fn recover(&self, context: &RecoveryContext) -> RecoveryResult<T>;

    /// Check if recovery is possible for the given error
    fn can_recover(&self, error: &TorshError) -> bool;

    /// Get the recovery strategy used by this implementation
    fn strategy(&self) -> &RecoveryStrategy;
}

/// Basic recovery implementation that uses fallback strategies
#[derive(Debug)]
pub struct BasicRecovery<T> {
    strategy: RecoveryStrategy,
    fallback_value: Option<T>,
}

impl<T: Clone> BasicRecovery<T> {
    /// Create a new basic recovery with the given strategy
    pub fn new(strategy: RecoveryStrategy) -> Self {
        Self {
            strategy,
            fallback_value: None,
        }
    }

    /// Create a recovery that uses a default value
    pub fn with_default(default_value: T) -> Self {
        Self {
            strategy: RecoveryStrategy::UseDefault,
            fallback_value: Some(default_value),
        }
    }

    /// Create a retry recovery with specified parameters
    pub fn retry(max_attempts: usize, delay_ms: Option<u64>) -> Self {
        Self {
            strategy: RecoveryStrategy::Retry {
                max_attempts,
                delay_ms,
            },
            fallback_value: None,
        }
    }
}

impl<T: Clone + fmt::Debug> ErrorRecovery<T> for BasicRecovery<T> {
    fn recover(&self, context: &RecoveryContext) -> RecoveryResult<T> {
        match &self.strategy {
            RecoveryStrategy::FailFast => RecoveryResult::Failed(context.error.clone()),
            RecoveryStrategy::Retry {
                max_attempts,
                delay_ms,
            } => {
                if context.attempt_count < *max_attempts {
                    if let Some(delay) = delay_ms {
                        std::thread::sleep(std::time::Duration::from_millis(*delay));
                    }
                    RecoveryResult::Retry
                } else {
                    RecoveryResult::Failed(TorshError::RuntimeError(format!(
                        "Failed after {max_attempts} retry attempts"
                    )))
                }
            }
            RecoveryStrategy::UseDefault => {
                if let Some(ref default_val) = self.fallback_value {
                    RecoveryResult::Degraded(
                        default_val.clone(),
                        "Using default value due to error".to_string(),
                    )
                } else {
                    RecoveryResult::Failed(TorshError::RuntimeError(
                        "No default value available for recovery".to_string(),
                    ))
                }
            }
            RecoveryStrategy::Skip => RecoveryResult::Failed(TorshError::RuntimeError(
                "Skip recovery not supported for this type".to_string(),
            )),
            RecoveryStrategy::Fallback => RecoveryResult::Failed(TorshError::RuntimeError(
                "Fallback recovery not implemented".to_string(),
            )),
            RecoveryStrategy::Custom(name) => RecoveryResult::Failed(TorshError::RuntimeError(
                format!("Custom recovery '{name}' not implemented"),
            )),
        }
    }

    fn can_recover(&self, _error: &TorshError) -> bool {
        match self.strategy {
            RecoveryStrategy::FailFast => false,
            RecoveryStrategy::UseDefault => self.fallback_value.is_some(),
            _ => true,
        }
    }

    fn strategy(&self) -> &RecoveryStrategy {
        &self.strategy
    }
}

/// Recovery manager that coordinates multiple recovery strategies
pub struct RecoveryManager<T> {
    strategies: Vec<Box<dyn ErrorRecovery<T>>>,
    default_strategy: RecoveryStrategy,
}

impl<T> fmt::Debug for RecoveryManager<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RecoveryManager")
            .field("strategy_count", &self.strategies.len())
            .field("default_strategy", &self.default_strategy)
            .finish()
    }
}

impl<T: Clone + fmt::Debug + 'static> RecoveryManager<T> {
    /// Create a new recovery manager
    pub fn new() -> Self {
        Self {
            strategies: Vec::new(),
            default_strategy: RecoveryStrategy::FailFast,
        }
    }

    /// Add a recovery strategy
    pub fn add_strategy(mut self, strategy: Box<dyn ErrorRecovery<T>>) -> Self {
        self.strategies.push(strategy);
        self
    }

    /// Set the default recovery strategy
    pub fn with_default_strategy(mut self, strategy: RecoveryStrategy) -> Self {
        self.default_strategy = strategy;
        self
    }

    /// Attempt recovery using available strategies
    pub fn attempt_recovery(&self, mut context: RecoveryContext) -> RecoveryResult<T> {
        // Try each strategy in order
        for strategy in &self.strategies {
            if strategy.can_recover(&context.error) {
                match strategy.recover(&context) {
                    RecoveryResult::Success(result) => return RecoveryResult::Success(result),
                    RecoveryResult::Degraded(result, msg) => {
                        return RecoveryResult::Degraded(result, msg)
                    }
                    RecoveryResult::Retry => {
                        context.attempt_count += 1;
                        if context.attempt_count < context.max_attempts {
                            return RecoveryResult::Retry;
                        }
                    }
                    RecoveryResult::Failed(_) => continue,
                }
            }
        }

        // If no strategy worked, use default strategy
        let default_recovery = BasicRecovery::new(self.default_strategy.clone());
        default_recovery.recover(&context)
    }
}

impl<T: Clone + fmt::Debug + 'static> Default for RecoveryManager<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Wrapper for operations that can recover from errors
#[derive(Debug)]
pub struct RecoverableOperation<T> {
    recovery_manager: RecoveryManager<T>,
    operation_name: String,
    max_attempts: usize,
}

impl<T: Clone + fmt::Debug + 'static> RecoverableOperation<T> {
    /// Create a new recoverable operation
    pub fn new(operation_name: impl Into<String>) -> Self {
        Self {
            recovery_manager: RecoveryManager::new(),
            operation_name: operation_name.into(),
            max_attempts: 3,
        }
    }

    /// Set the recovery manager
    pub fn with_recovery_manager(mut self, manager: RecoveryManager<T>) -> Self {
        self.recovery_manager = manager;
        self
    }

    /// Set maximum retry attempts
    pub fn with_max_attempts(mut self, attempts: usize) -> Self {
        self.max_attempts = attempts;
        self
    }

    /// Execute an operation with recovery
    pub fn execute<F>(&self, mut operation: F) -> Result<T>
    where
        F: FnMut() -> Result<T>,
    {
        let mut attempt_count = 0;

        loop {
            match operation() {
                Ok(result) => return Ok(result),
                Err(error) => {
                    let context = RecoveryContext {
                        error: error.clone(),
                        operation: self.operation_name.clone(),
                        attempt_count,
                        max_attempts: self.max_attempts,
                        context: std::collections::HashMap::new(),
                    };

                    match self.recovery_manager.attempt_recovery(context) {
                        RecoveryResult::Success(result) => return Ok(result),
                        RecoveryResult::Degraded(result, _msg) => {
                            // Could log the degradation message here
                            return Ok(result);
                        }
                        RecoveryResult::Retry => {
                            attempt_count += 1;
                            if attempt_count >= self.max_attempts {
                                return Err(TorshError::RuntimeError(format!(
                                    "Operation '{}' failed after {} attempts",
                                    self.operation_name, self.max_attempts
                                )));
                            }
                            continue;
                        }
                        RecoveryResult::Failed(recovery_error) => {
                            return Err(TorshError::wrap_with_location(
                                recovery_error,
                                format!("Recovery failed for operation '{}'", self.operation_name),
                            ));
                        }
                    }
                }
            }
        }
    }

    /// Execute an operation with recovery and context
    pub fn execute_with_context<F>(
        &self,
        mut operation: F,
        context: std::collections::HashMap<String, String>,
    ) -> Result<T>
    where
        F: FnMut() -> Result<T>,
    {
        let mut attempt_count = 0;

        loop {
            match operation() {
                Ok(result) => return Ok(result),
                Err(error) => {
                    let recovery_context = RecoveryContext {
                        error: error.clone(),
                        operation: self.operation_name.clone(),
                        attempt_count,
                        max_attempts: self.max_attempts,
                        context: context.clone(),
                    };

                    match self.recovery_manager.attempt_recovery(recovery_context) {
                        RecoveryResult::Success(result) => return Ok(result),
                        RecoveryResult::Degraded(result, _msg) => return Ok(result),
                        RecoveryResult::Retry => {
                            attempt_count += 1;
                            if attempt_count >= self.max_attempts {
                                return Err(error);
                            }
                            continue;
                        }
                        RecoveryResult::Failed(recovery_error) => return Err(recovery_error),
                    }
                }
            }
        }
    }
}

/// Convenience function to create a simple retry operation
pub fn with_retry<T, F>(operation: F, max_attempts: usize) -> Result<T>
where
    F: FnMut() -> Result<T>,
    T: Clone + fmt::Debug + 'static,
{
    let recovery_manager = RecoveryManager::new()
        .add_strategy(Box::new(BasicRecovery::retry(max_attempts, Some(100))));

    let recoverable = RecoverableOperation::new("retry_operation")
        .with_recovery_manager(recovery_manager)
        .with_max_attempts(max_attempts);

    recoverable.execute(operation)
}

/// Convenience function to create an operation with a fallback value
pub fn with_fallback<T, F>(operation: F, fallback: T) -> Result<T>
where
    F: FnMut() -> Result<T>,
    T: Clone + fmt::Debug + 'static,
{
    let recovery_manager =
        RecoveryManager::new().add_strategy(Box::new(BasicRecovery::with_default(fallback)));

    let recoverable =
        RecoverableOperation::new("fallback_operation").with_recovery_manager(recovery_manager);

    recoverable.execute(operation)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_recovery_retry() {
        let recovery = BasicRecovery::<i32>::retry(3, Some(1));
        let context = RecoveryContext {
            error: TorshError::RuntimeError("Test error".to_string()),
            operation: "test_op".to_string(),
            attempt_count: 1,
            max_attempts: 3,
            context: std::collections::HashMap::new(),
        };

        match recovery.recover(&context) {
            RecoveryResult::Retry => {} // Expected retry result
            _ => panic!("Expected retry result"),
        }
    }

    #[test]
    fn test_basic_recovery_default() {
        let recovery = BasicRecovery::with_default(42);
        let context = RecoveryContext {
            error: TorshError::RuntimeError("Test error".to_string()),
            operation: "test_op".to_string(),
            attempt_count: 1,
            max_attempts: 3,
            context: std::collections::HashMap::new(),
        };

        match recovery.recover(&context) {
            RecoveryResult::Degraded(value, _) => assert_eq!(value, 42),
            _ => panic!("Expected degraded result with default value"),
        }
    }

    #[test]
    fn test_recoverable_operation_success() {
        let recoverable = RecoverableOperation::new("test_operation");
        let result = recoverable.execute(|| Ok(42));
        assert_eq!(result.expect("execute should succeed"), 42);
    }

    #[test]
    fn test_with_retry_convenience() {
        let mut counter = 0;
        let result = with_retry(
            || {
                counter += 1;
                if counter < 3 {
                    Err(TorshError::RuntimeError("Not ready".to_string()))
                } else {
                    Ok(42)
                }
            },
            5,
        );

        assert_eq!(result.expect("with_retry should succeed"), 42);
        assert_eq!(counter, 3);
    }

    #[test]
    fn test_with_fallback_convenience() {
        let result = with_fallback(
            || Err(TorshError::RuntimeError("Operation failed".to_string())),
            100,
        );

        assert_eq!(result.expect("with_fallback should succeed"), 100);
    }

    #[test]
    fn test_recovery_manager() {
        let manager = RecoveryManager::new()
            .add_strategy(Box::new(BasicRecovery::retry(2, Some(1))))
            .add_strategy(Box::new(BasicRecovery::with_default(999)));

        let context = RecoveryContext {
            error: TorshError::RuntimeError("Test error".to_string()),
            operation: "test_op".to_string(),
            attempt_count: 3, // Exceed retry limit
            max_attempts: 3,
            context: std::collections::HashMap::new(),
        };

        match manager.attempt_recovery(context) {
            RecoveryResult::Degraded(value, _) => assert_eq!(value, 999),
            _ => panic!("Expected degraded result with default value"),
        }
    }
}