use std::io;
use thiserror::Error;
use super::SubmissionError;
#[derive(Debug, Error)]
pub enum ExecutorServiceBuilderError {
#[error("executor service pool size must be greater than zero")]
ZeroPoolSize,
#[error("executor service maximum pool size must be greater than zero")]
ZeroMaximumPoolSize,
#[error(
"executor service core pool size {core_pool_size} exceeds maximum pool size {maximum_pool_size}"
)]
CorePoolSizeExceedsMaximum {
core_pool_size: usize,
maximum_pool_size: usize,
},
#[error("executor service queue capacity must be greater than zero")]
ZeroQueueCapacity,
#[error("executor service stack size must be greater than zero")]
ZeroStackSize,
#[error("executor service keep-alive timeout must be greater than zero")]
ZeroKeepAlive,
#[error("failed to spawn executor service worker {}: {source}", format_worker_index(*index))]
SpawnWorker {
index: Option<usize>,
source: io::Error,
},
}
impl ExecutorServiceBuilderError {
pub fn from_submission_error(error: SubmissionError) -> Self {
match error {
SubmissionError::WorkerSpawnFailed { source } => Self::SpawnWorker {
index: None,
source: io::Error::new(source.kind(), source.to_string()),
},
SubmissionError::Shutdown => Self::SpawnWorker {
index: None,
source: io::Error::other("executor service shut down during prestart"),
},
SubmissionError::Saturated => Self::SpawnWorker {
index: None,
source: io::Error::other("executor service saturated during prestart"),
},
}
}
}
fn format_worker_index(index: Option<usize>) -> String {
index
.map(|index| index.to_string())
.unwrap_or_else(|| "unknown".to_owned())
}
impl From<SubmissionError> for ExecutorServiceBuilderError {
fn from(error: SubmissionError) -> Self {
Self::from_submission_error(error)
}
}