edifact_mapper/
evaluator_factory.rs1use automapper_validation::{
4 ConditionEvaluator, ConditionExprEvaluator, ConditionResult, EvaluationContext,
5};
6use mig_bo4e::pid_requirements::PidRequirements;
7use mig_bo4e::pid_validation::PidValidationError;
8use serde_json::Value;
9
10pub fn validate_with_boxed_evaluator(
14 evaluator: &dyn ConditionEvaluator,
15 json: &Value,
16 requirements: &PidRequirements,
17 pid: &str,
18 segments: &[mig_types::segment::OwnedSegment],
19) -> Vec<PidValidationError> {
20 struct EvalRef<'a>(&'a dyn ConditionEvaluator);
23
24 impl ConditionEvaluator for EvalRef<'_> {
25 fn evaluate(&self, condition: u32, ctx: &EvaluationContext) -> ConditionResult {
26 self.0.evaluate(condition, ctx)
27 }
28
29 fn is_external(&self, condition: u32) -> bool {
30 self.0.is_external(condition)
31 }
32
33 fn is_known(&self, condition: u32) -> bool {
34 self.0.is_known(condition)
35 }
36
37 fn message_type(&self) -> &str {
38 self.0.message_type()
39 }
40
41 fn format_version(&self) -> &str {
42 self.0.format_version()
43 }
44 }
45
46 let wrapper = EvalRef(evaluator);
47 let external = automapper_validation::MapExternalProvider::new(Default::default());
48 let ctx = EvaluationContext::new(pid, &external, segments);
49 let expr_eval = ConditionExprEvaluator::new(&wrapper);
50 crate::conditional_validation::validate_with_conditions(json, requirements, &expr_eval, &ctx)
51}
52
53pub fn create_evaluator(variant: &str, fv: &str) -> Option<Box<dyn ConditionEvaluator>> {
58 automapper_validation::evaluator_for(variant, fv)
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_known_evaluators_exist() {
67 assert!(create_evaluator("UTILMD_Strom", "FV2504").is_some());
68 assert!(create_evaluator("MSCONS", "FV2504").is_some());
69 assert!(create_evaluator("ORDERS", "FV2510").is_some());
70 assert!(create_evaluator("UTILMD_Gas", "FV2604").is_some());
71 }
72
73 #[test]
74 fn test_unknown_evaluator_returns_none() {
75 assert!(create_evaluator("NONEXISTENT", "FV2504").is_none());
76 assert!(create_evaluator("UTILMD_Strom", "FV9999").is_none());
77 }
78}