use serde_json::Value;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum PoolsimError {
#[error("{message}")]
InvalidInput {
code: &'static str,
message: String,
details: Option<Value>,
},
#[error("system saturated at rho={rho:.4}")]
Saturated {
rho: f64,
},
#[error("distribution error: {0}")]
Distribution(String),
#[error("simulation error: {0}")]
Simulation(String),
}
impl PoolsimError {
pub fn invalid_input(code: &'static str, message: impl Into<String>, details: Option<Value>) -> Self {
Self::InvalidInput {
code,
message: message.into(),
details,
}
}
pub fn code(&self) -> &'static str {
match self {
Self::InvalidInput { code, .. } => code,
Self::Saturated { .. } => "SATURATED",
Self::Distribution(_) => "DISTRIBUTION_ERROR",
Self::Simulation(_) => "SIMULATION_ERROR",
}
}
pub fn details(&self) -> Option<&Value> {
match self {
Self::InvalidInput { details, .. } => details.as_ref(),
_ => None,
}
}
}