use crate::SddpError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PrepPhase {
Config,
Stochastic,
HydroModels,
}
#[must_use]
pub fn prep_phase_metadata(phase: PrepPhase, err: &SddpError) -> (&'static str, &'static str) {
let kind = match phase {
PrepPhase::Config => "ConfigValidationError",
PrepPhase::Stochastic => "StochasticPreparationError",
PrepPhase::HydroModels => "HydroModelsPreparationError",
};
let file_label = match (phase, err) {
(PrepPhase::Config, _) => "config.json",
(PrepPhase::Stochastic, SddpError::Stochastic(_)) => "scenarios/inflow_history.parquet",
(PrepPhase::Stochastic, _) => "scenarios/",
(PrepPhase::HydroModels, _) => "system/hydro_production_models.json",
};
(kind, file_label)
}
#[cfg(test)]
mod tests {
use cobre_stochastic::StochasticError;
use super::*;
#[test]
fn config_phase_always_returns_config_json() {
let err = SddpError::Validation("bad window".to_string());
let (kind, file) = prep_phase_metadata(PrepPhase::Config, &err);
assert_eq!(kind, "ConfigValidationError");
assert_eq!(file, "config.json");
}
#[test]
fn stochastic_phase_with_stochastic_error_returns_inflow_history() {
let err = SddpError::Stochastic(StochasticError::InsufficientData {
context: "only 2 years".to_string(),
});
let (kind, file) = prep_phase_metadata(PrepPhase::Stochastic, &err);
assert_eq!(kind, "StochasticPreparationError");
assert_eq!(file, "scenarios/inflow_history.parquet");
}
#[test]
fn stochastic_phase_with_non_stochastic_error_returns_scenarios_dir() {
let err = SddpError::Validation("something else".to_string());
let (kind, file) = prep_phase_metadata(PrepPhase::Stochastic, &err);
assert_eq!(kind, "StochasticPreparationError");
assert_eq!(file, "scenarios/");
}
#[test]
fn hydro_models_phase_returns_production_models_file() {
let err = SddpError::Validation("model error".to_string());
let (kind, file) = prep_phase_metadata(PrepPhase::HydroModels, &err);
assert_eq!(kind, "HydroModelsPreparationError");
assert_eq!(file, "system/hydro_production_models.json");
}
#[test]
fn all_phases_produce_non_empty_kind_and_file() {
let err = SddpError::Validation("test".to_string());
for phase in [
PrepPhase::Config,
PrepPhase::Stochastic,
PrepPhase::HydroModels,
] {
let (kind, file) = prep_phase_metadata(phase, &err);
assert!(!kind.is_empty(), "kind must not be empty for {phase:?}");
assert!(
!file.is_empty(),
"file_label must not be empty for {phase:?}"
);
}
}
}