use super::CoreError;
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CoreConfig {
max_workers: usize,
max_batch_size: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CoreConfigError {
ZeroWorkers,
ZeroBatchSize,
}
impl CoreConfig {
pub const fn new(max_workers: usize, max_batch_size: usize) -> Result<Self, CoreConfigError> {
if max_workers == 0 {
return Err(CoreConfigError::ZeroWorkers);
}
if max_batch_size == 0 {
return Err(CoreConfigError::ZeroBatchSize);
}
Ok(Self {
max_workers,
max_batch_size,
})
}
pub const fn default_config() -> Self {
Self {
max_workers: 1,
max_batch_size: 64,
}
}
pub const fn max_workers(self) -> usize {
self.max_workers
}
pub const fn max_batch_size(self) -> usize {
self.max_batch_size
}
pub const fn validate(self) -> Result<(), CoreConfigError> {
if self.max_workers == 0 {
return Err(CoreConfigError::ZeroWorkers);
}
if self.max_batch_size == 0 {
return Err(CoreConfigError::ZeroBatchSize);
}
Ok(())
}
}
impl Default for CoreConfig {
fn default() -> Self {
Self::default_config()
}
}
impl fmt::Display for CoreConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroWorkers => f.write_str("max_workers must be greater than zero"),
Self::ZeroBatchSize => f.write_str("max_batch_size must be greater than zero"),
}
}
}
impl std::error::Error for CoreConfigError {}
impl From<CoreConfigError> for CoreError {
fn from(_: CoreConfigError) -> Self {
Self::InvalidConfiguration
}
}