#[cfg(test)]
#[path = "../../tests/settings/builder.rs"]
mod tests;
use super::error::SettingsError;
use super::override_map::{Key, OverrideMap, SettingType};
use super::statics::consts;
use super::structure::Settings;
use crate::bytes::BytePool;
use crate::utils::sync::AsyncExecutor;
#[derive(Default)]
pub struct SettingsBuilder<AE: AsyncExecutor> {
overrides: OverrideMap,
executor: Option<AE>,
pool: Option<BytePool>,
mtu: usize,
skip_env: bool,
}
impl<AE: AsyncExecutor> SettingsBuilder<AE> {
pub fn new() -> Self {
Self {
overrides: OverrideMap::default(),
executor: None,
pool: None,
mtu: consts::DEFAULT_TYPHOON_MTU_LENGTH,
skip_env: false,
}
}
pub fn without_env(mut self) -> Self {
self.skip_env = false;
self
}
pub fn with_executor(mut self, executor: AE) -> Self {
self.executor = Some(executor);
self
}
pub fn with_pool(mut self, pool: BytePool) -> Self {
self.pool = Some(pool);
self
}
pub fn with_mtu(mut self, mtu: usize) -> Self {
self.mtu = mtu;
self
}
#[inline]
pub fn set<T: SettingType>(mut self, key: &Key<T>, value: T) -> Self {
self.overrides.insert(key.name, value.to_value());
self
}
pub fn build(self) -> Result<Settings<AE>, SettingsError> {
let settings = Settings::new(
self.overrides,
self.executor.unwrap_or_else(AE::new),
self.pool.unwrap_or_else(|| {
BytePool::new(self.mtu, self.mtu, self.mtu / 2, consts::DEFAULT_POOL_INITIAL_SIZE, consts::DEFAULT_POOL_CAPACITY)
}),
self.mtu,
);
settings.assert()?;
Ok(settings)
}
}