|
94 | 94 | //! pub fn validate(&self) -> Result<(), AgentConfigError> { |
95 | 95 | //! match self { |
96 | 96 | //! AgentConfig::MyNewAgent { param1, param2 } => { |
97 | | -//! // Validate parameters |
98 | | -//! if *param1 < 0.0 { |
99 | | -//! return Err(AgentConfigError::ValidationError( |
100 | | -//! "param1 must be non-negative".to_string() |
101 | | -//! )); |
| 97 | +//! // Validate parameters via a domain-specific error |
| 98 | +//! // variant; add a new variant to AgentConfigError if |
| 99 | +//! // none of the existing typed variants fits. |
| 100 | +//! if *param1 < 0.0 || *param1 > 1.0 { |
| 101 | +//! return Err(AgentConfigError::InvalidProbability(*param1)); |
102 | 102 | //! } |
103 | 103 | //! } |
104 | 104 | //! // ... other variants ... |
@@ -153,8 +153,9 @@ use crate::arena::agent::{ |
153 | 153 | }; |
154 | 154 | use crate::arena::cfr::{ |
155 | 155 | ActionGenerator, BasicCFRActionGenerator, CFRAgentBuilder, CFRState, CfrDepthConfig, |
156 | | - ConfigurableActionConfig, ConfigurableActionGenerator, PreflopChartActionConfig, |
157 | | - PreflopChartActionGenerator, PreflopChartConfig, SimpleActionGenerator, TraversalSet, |
| 156 | + ConfigurableActionConfig, ConfigurableActionConfigError, ConfigurableActionGenerator, |
| 157 | + PreflopChartActionConfig, PreflopChartActionGenerator, PreflopChartConfig, |
| 158 | + PreflopChartConfigError, SimpleActionGenerator, TraversalSet, |
158 | 159 | }; |
159 | 160 | use crate::arena::{Agent, GameState}; |
160 | 161 | use rand::SeedableRng; |
@@ -295,12 +296,9 @@ impl PreflopChartConfigOption { |
295 | 296 | /// Preset names are not currently supported - use inline configuration. |
296 | 297 | pub fn resolve(&self) -> Result<PreflopChartConfig, AgentConfigError> { |
297 | 298 | match self { |
298 | | - PreflopChartConfigOption::Preset(name) => { |
299 | | - Err(AgentConfigError::ValidationError(format!( |
300 | | - "Preset charts are not available. Use inline configuration instead of preset '{}'. See examples/configs/preflop_6max_rfi.json for an example.", |
301 | | - name |
302 | | - ))) |
303 | | - } |
| 299 | + PreflopChartConfigOption::Preset(name) => Err( |
| 300 | + AgentConfigError::PreflopChartPresetUnavailable(name.clone()), |
| 301 | + ), |
304 | 302 | PreflopChartConfigOption::Inline(config) => Ok(config.clone()), |
305 | 303 | } |
306 | 304 | } |
@@ -333,9 +331,21 @@ pub enum AgentConfigError { |
333 | 331 | #[error("File I/O error: {0}")] |
334 | 332 | IoError(#[from] std::io::Error), |
335 | 333 |
|
336 | | - /// Generic validation error |
337 | | - #[error("Validation error: {0}")] |
338 | | - ValidationError(String), |
| 334 | + /// Failed to validate a configurable action generator config. |
| 335 | + #[error("invalid configurable action config: {0}")] |
| 336 | + ConfigurableActionConfig(#[from] ConfigurableActionConfigError), |
| 337 | + |
| 338 | + /// Failed to validate a preflop chart config. |
| 339 | + #[error("invalid preflop chart config: {0}")] |
| 340 | + PreflopChartConfig(#[from] PreflopChartConfigError), |
| 341 | + |
| 342 | + /// A preflop chart preset name was supplied, but presets are not yet |
| 343 | + /// supported. Use an inline configuration instead. |
| 344 | + #[error( |
| 345 | + "preflop chart preset '{0}' is not available; use inline configuration instead. \ |
| 346 | + See examples/configs/preflop_6max_rfi.json for an example" |
| 347 | + )] |
| 348 | + PreflopChartPresetUnavailable(String), |
339 | 349 | } |
340 | 350 |
|
341 | 351 | impl AgentConfig { |
@@ -369,15 +379,11 @@ impl AgentConfig { |
369 | 379 | validate_probabilities(percent_call)?; |
370 | 380 | } |
371 | 381 | AgentConfig::CfrConfigurable { action_config, .. } => { |
372 | | - action_config |
373 | | - .validate() |
374 | | - .map_err(AgentConfigError::ValidationError)?; |
| 382 | + action_config.validate()?; |
375 | 383 | } |
376 | 384 | AgentConfig::CfrPreflopChart { preflop_config, .. } => { |
377 | 385 | let resolved = preflop_config.resolve()?; |
378 | | - resolved |
379 | | - .validate() |
380 | | - .map_err(AgentConfigError::ValidationError)?; |
| 386 | + resolved.validate()?; |
381 | 387 | } |
382 | 388 | _ => {} |
383 | 389 | } |
|
0 commit comments