rs2_stream/
error.rs

1//! Error types and handling for RStream
2//!
3//! This module provides comprehensive error handling capabilities
4//! for streaming operations.
5
6use std::fmt;
7use std::time::Duration;
8
9/// Main error type for RStream operations
10#[derive(Debug, Clone, PartialEq)]
11pub enum StreamError {
12    /// I/O related errors
13    IO(String),
14    /// Operation timed out
15    Timeout,
16    /// Resource exhausted (memory, file descriptors, etc.)
17    ResourceExhausted,
18    /// Operation was cancelled
19    Cancelled,
20    /// Backpressure buffer overflow
21    BackpressureOverflow,
22    /// Custom error with message
23    Custom(String),
24}
25
26impl fmt::Display for StreamError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            StreamError::IO(msg) => write!(f, "IO error: {}", msg),
30            StreamError::Timeout => write!(f, "Operation timed out"),
31            StreamError::ResourceExhausted => write!(f, "Resource exhausted"),
32            StreamError::Cancelled => write!(f, "Operation cancelled"),
33            StreamError::BackpressureOverflow => write!(f, "Backpressure buffer overflow"),
34            StreamError::Custom(msg) => write!(f, "Stream error: {}", msg),
35        }
36    }
37}
38
39impl std::error::Error for StreamError {}
40
41impl From<std::io::Error> for StreamError {
42    fn from(err: std::io::Error) -> Self {
43        StreamError::IO(err.to_string())
44    }
45}
46
47impl From<tokio::time::error::Elapsed> for StreamError {
48    fn from(_: tokio::time::error::Elapsed) -> Self {
49        StreamError::Timeout
50    }
51}
52
53/// Result type for rs2_stream operations
54pub type StreamResult<T> = Result<T, StreamError>;
55
56/// Retry policy for error handling
57#[derive(Debug, Clone)]
58pub enum RetryPolicy {
59    /// No retries
60    None,
61    /// Immediate retry up to max_retries
62    Immediate { max_retries: usize },
63    /// Fixed delay between retries
64    Fixed { max_retries: usize, delay: Duration },
65    /// Exponential backoff
66    Exponential {
67        max_retries: usize,
68        initial_delay: Duration,
69        multiplier: f64
70    },
71}
72
73impl Default for RetryPolicy {
74    fn default() -> Self {
75        RetryPolicy::Fixed {
76            max_retries: 3,
77            delay: Duration::from_millis(100),
78        }
79    }
80}