//! Configuration use super::*; use prov::prelude::*; /// Application state pub struct State { pub throttle_generator: Option + 'static>>, pub buffer_size: Box, } impl State { /// Convert into an instance that can be used to create a `StateThrottleAdaptor` #[inline(always)] pub fn with_stream(self, stream: S) -> (Self, S) { (self, stream) } } /// Configuration for application #[derive(Debug, Clone)] pub struct Config { pub throttle: Option<(Duration, Option)>, pub buffer_size: Option<(usize, Option)>, } impl From for State { #[inline] fn from(from: Config) -> Self { Self { throttle_generator: if let Some(throttle) = from.throttle { Some(match throttle { (Duration::ZERO, Some(Duration::ZERO) | None) => Box::new(NoThrottleProvider), (low, Some(high)) if low == high => Box::new(SingleThrottleProvider::from(low)), (low, Some(high)) => Box::new(UniformThrottleProvider::from(low..high)), (low, _) => Box::new(SingleThrottleProvider::from(low)), }) } else { None }, buffer_size: if let Some(size) = from.buffer_size { match size { (0, Some(0) | None) => Box::new(NoBufferProvider), (low, Some(high)) if low == high => Box::new(low), (low, Some(high)) => Box::new(UniformBufferProvider::from(low..high)), (low, _) => Box::new(low) } } else { Box::new(DefaultBufferSize) } } } } impl Default for Config { #[inline] fn default() -> Self { Self::new() } } impl Config { /// Create an empty new config #[inline(always)] pub const fn new() -> Self { Self { throttle: None, buffer_size: None, } } }