Skip to main content

flux_limiter/
errors.rs

1// src/errors.rs
2
3// error handling for the flux limiter type
4
5// dependencies
6use std::error::Error;
7use std::fmt;
8
9use crate::clock::ClockError;
10
11/// Error type for FluxLimiter configuration issues.
12#[non_exhaustive]
13#[derive(Debug)]
14pub enum FluxLimiterError {
15    InvalidRate,            // for rate <= 0
16    InvalidBurst,           // for burst < 0
17    ClockError(ClockError), // error variant for issues with the system clock
18}
19
20// implement the Display trait for the FluxLimiterError type
21impl fmt::Display for FluxLimiterError {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        match self {
24            FluxLimiterError::InvalidRate => write!(f, "Rate must be positive"),
25            FluxLimiterError::InvalidBurst => write!(f, "Burst must be non-negative"),
26            FluxLimiterError::ClockError(err) => {
27                write!(f, "Clock error occurred: {}", err)
28            }
29        }
30    }
31}
32
33// implement the Error trait for the FluxLimiterError type
34impl Error for FluxLimiterError {
35    fn source(&self) -> Option<&(dyn Error + 'static)> {
36        match self {
37            FluxLimiterError::ClockError(err) => Some(err),
38            _ => None,
39        }
40    }
41}
42
43// implement From<ClockError> for ergonomic ? conversion
44impl From<ClockError> for FluxLimiterError {
45    fn from(err: ClockError) -> Self {
46        FluxLimiterError::ClockError(err)
47    }
48}