use std::path::{Path, PathBuf};
use thiserror::Error;
use crate::config::advanced::ConfigError;
use crate::state::advanced::StateError;
use crate::task::advanced::ModelCatalogError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum StudyError {
#[error("failed to load project declarations")]
Config(#[from] ConfigError),
#[error("failed to validate state schema `{state}` from `{path}`")]
State {
state: String,
path: PathBuf,
#[source]
source: StateError,
},
#[error("invalid compiled model registration: {reason}")]
InvalidModelRegistration {
reason: String,
},
#[error("phase `{phase}` references unregistered model `{model}`")]
UnknownModel {
phase: String,
model: String,
},
#[error(
"model `{model}` failed preflight for resolved parameters {ordinal} in phase `{phase}`"
)]
ModelPreflight {
phase: String,
model: String,
ordinal: u64,
#[source]
source: Box<dyn std::error::Error + Send + Sync + 'static>,
},
#[error("the study contains more expanded task invocations than can be identified")]
TaskIdentityOverflow,
}
impl From<ModelCatalogError> for StudyError {
fn from(source: ModelCatalogError) -> Self {
Self::InvalidModelRegistration {
reason: source.to_string(),
}
}
}
impl StudyError {
pub(crate) fn state_schema(state: &str, path: &Path, source: StateError) -> Self {
Self::State {
state: state.to_owned(),
path: path.to_path_buf(),
source,
}
}
pub(crate) fn model_preflight(
phase: &str,
model: &str,
ordinal: u64,
source: Box<dyn std::error::Error + Send + Sync + 'static>,
) -> Self {
Self::ModelPreflight {
phase: phase.to_owned(),
model: model.to_owned(),
ordinal,
source,
}
}
}