1use std::fmt;
7use std::time::Duration;
8
9#[derive(Debug, Clone, PartialEq)]
11pub enum StreamError {
12 IO(String),
14 Timeout,
16 ResourceExhausted,
18 Cancelled,
20 BackpressureOverflow,
22 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
53pub type StreamResult<T> = Result<T, StreamError>;
55
56#[derive(Debug, Clone)]
58pub enum RetryPolicy {
59 None,
61 Immediate { max_retries: usize },
63 Fixed { max_retries: usize, delay: Duration },
65 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}