Skip to main content

Validator

Trait Validator 

Source
pub trait Validator: Send + Sync {
    // Required method
    fn validate_batch(
        &self,
        segments: &[Segment<'_>],
        report: &mut ValidationReport,
        context: &ValidationRuleContext<'_>,
    );

    // Provided methods
    fn validate_group_batch(
        &self,
        _root: &SegmentGroupIndexed,
        _all_segments: &[Segment<'_>],
        _report: &mut ValidationReport,
        _context: &ValidationRuleContext<'_>,
    ) { ... }
    fn has_group_rules(&self) -> bool { ... }
    fn set_message_type(&mut self, _message_type: Option<&str>) { ... }
    fn fork(&self) -> Option<Box<dyn Validator + Send + Sync>> { ... }
}
Expand description

Pluggable validator for parsed EDIFACT segments.

The primary contract is validate_batch, which processes an entire segment sequence and appends issues to a ValidationReport.

Required Methods§

Source

fn validate_batch( &self, segments: &[Segment<'_>], report: &mut ValidationReport, context: &ValidationRuleContext<'_>, )

Validate a full segment set and append issues to report.

Provided Methods§

Source

fn validate_group_batch( &self, _root: &SegmentGroupIndexed, _all_segments: &[Segment<'_>], _report: &mut ValidationReport, _context: &ValidationRuleContext<'_>, )

Validate a segment-group tree and append issues to report.

Called by ValidationContext::validate_lenient_grouped in addition to validate_batch. Validators that only perform flat segment checks (e.g. EnvelopeValidator) can leave this as the default no-op; only validators with group-scoped rules (typically ProfileRulePack with at least one group rule) need to override it.

The default implementation is a no-op so that adding this method to the trait is not a breaking change for external Validator implementors.

Source

fn has_group_rules(&self) -> bool

Returns true if this validator has any group-scoped rules.

Used by crate::ValidationContext to short-circuit the group-tree walk when no validator in the context has group rules, avoiding the cost of allocating a borrowed slice for nothing.

The default implementation returns false.

Source

fn set_message_type(&mut self, _message_type: Option<&str>)

Configure message-type metadata for validators that support explicit scoping.

Source

fn fork(&self) -> Option<Box<dyn Validator + Send + Sync>>

Create a Box<dyn Validator> clone of this validator for context forking.

Return Some(boxed_clone) for validators that support cheap forking (e.g. those backed by Arc data, like ProfileRulePack).

Return None for validators that cannot be forked (e.g. stateful validators without Clone). Returning None causes the validator to be silently excluded from forked contexts — forking is used by crate::ValidationContext::validate_lenient_grouped to validate each group in isolation, so omitting a non-forkable validator from the forked context is safer than panicking.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Validator for Arc<ProfileRulePack>

Arc<ProfileRulePack> can be plugged directly into a super::context::ValidationContext.

Forking (for fork_with_message_ref) only increments the reference count — no deep copy of the rule vec is performed. This is the zero-allocation path for downstream code that caches packs in a LazyLock or OnceLock.

§Example

use std::sync::{Arc, LazyLock};
use edifact_rs::{ProfileRulePack, ValidationContext};

static ORDERS_PACK: LazyLock<Arc<ProfileRulePack>> = LazyLock::new(|| {
    Arc::new(
        ProfileRulePack::new("ORDERS-MIG")
            .for_message_type("ORDERS")
            .require_segment("BGM", "MIG-BGM-M"),
    )
});

let ctx = ValidationContext::builder()
    .with_profile_pack_arc(Arc::clone(&ORDERS_PACK))
    .build();
Source§

fn validate_batch( &self, segments: &[Segment<'_>], report: &mut ValidationReport, context: &ValidationRuleContext<'_>, )

Source§

fn validate_group_batch( &self, root: &SegmentGroupIndexed, all_segments: &[Segment<'_>], report: &mut ValidationReport, context: &ValidationRuleContext<'_>, )

Source§

fn has_group_rules(&self) -> bool

Source§

fn fork(&self) -> Option<Box<dyn Validator + Send + Sync>>

Implementors§