rust / expert
Snippet
Recursive Error Recovery Pipeline via Fallback Decoders
This design establishes a recursive error-recovery flow where failed primary parsing results fall back to secondary parsing logic, and recursively attempts recovery using a safe configuration state.
snippet.rs
rust
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
enum PipelineError {InvalidFormat(String),NetworkFailure(String),}fn primary_decoder(input: &str) -> Result<String, PipelineError> {if input.starts_with("RAW:") {Ok(input[4..].to_string())} else {Err(PipelineError::InvalidFormat("Missing RAW prefix".to_string()))}}fn secondary_decoder(input: &str) -> Result<String, PipelineError> {if input.starts_with("HEX:") {Ok(format!("Decoded Hex: {}", &input[4..]))} else {Err(PipelineError::InvalidFormat("Missing HEX prefix".to_string()))}}fn run_pipeline(input: &str, depth: usize) -> Result<String, PipelineError> {if depth == 0 {return Err(PipelineError::NetworkFailure("Max recovery depth reached".to_string()));}primary_decoder(input).or_else(|err| match err {PipelineError::InvalidFormat(_) => {secondary_decoder(input).or_else(|_| run_pipeline("RAW:fallback", depth - 1))}other => Err(other),})}
Breakdown
1
primary_decoder(input).or_else(...)
Attempts the first decoding stage and intercepts error occurrences to redirect execution.
2
match err { PipelineError::InvalidFormat(_) => ... }
Filters error variants to apply context-specific fallback handling.
3
run_pipeline("RAW:fallback", depth - 1)
Recursively calls the pipeline with a modified input and decreased recursion depth to avoid stack overflows.
4
other => Err(other)
Passes non-recoverable error variants straight up the execution stack.