use std::fmt;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq)]
pub enum StreamError {
IO(String),
Timeout,
ResourceExhausted,
Cancelled,
BackpressureOverflow,
Custom(String),
}
impl fmt::Display for StreamError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StreamError::IO(msg) => write!(f, "IO error: {}", msg),
StreamError::Timeout => write!(f, "Operation timed out"),
StreamError::ResourceExhausted => write!(f, "Resource exhausted"),
StreamError::Cancelled => write!(f, "Operation cancelled"),
StreamError::BackpressureOverflow => write!(f, "Backpressure buffer overflow"),
StreamError::Custom(msg) => write!(f, "Stream error: {}", msg),
}
}
}
impl std::error::Error for StreamError {}
impl From<std::io::Error> for StreamError {
fn from(err: std::io::Error) -> Self {
StreamError::IO(err.to_string())
}
}
impl From<tokio::time::error::Elapsed> for StreamError {
fn from(_: tokio::time::error::Elapsed) -> Self {
StreamError::Timeout
}
}
pub type StreamResult<T> = Result<T, StreamError>;
#[derive(Debug, Clone)]
pub enum RetryPolicy {
None,
Immediate { max_retries: usize },
Fixed { max_retries: usize, delay: Duration },
Exponential {
max_retries: usize,
initial_delay: Duration,
multiplier: f64,
},
}
impl Default for RetryPolicy {
fn default() -> Self {
RetryPolicy::Fixed {
max_retries: 3,
delay: Duration::from_millis(100),
}
}
}