Skip to main content

Parser

Struct Parser 

Source
pub struct Parser { /* private fields */ }
Expand description

A configured parser for EDI@Energy messages and interchanges.

Parser is the primary API for parsing with custom ParseConfig. Construct with Parser::new (default config) or Parser::with_config.

The Parser API is the primary API for parsing with custom ParseConfig. Construct with Parser::new (default config) or Parser::with_config. Use Parser directly whenever you need custom segment limits, a reference date, or the more advanced interchange paths.

§Example

use edi_energy::{Parser, ParseConfig};

let config = ParseConfig { max_segments: Some(5_000), ..ParseConfig::default() };
let parser = Parser::with_config(config);

// Single message from bytes
let msg = parser.parse(b"UNH+1+UTILMD:D:11A:UN:S2.1'...")?;

// Single message from a reader
let reader = std::fs::File::open("message.edi")?;
let msg = parser.parse_reader(reader)?;

// Interchange — lazy iterator
let reader = std::fs::File::open("interchange.edi")?;
for result in parser.parse_interchange(reader) {
    let _msg: edi_energy::AnyMessage = result?;
}

// Interchange — envelope first, messages lazily
let reader = std::fs::File::open("interchange.edi")?;
let (header, iter) = parser.parse_interchange_buffered(reader)?;
println!("sender: {}", header.sender_id);
for result in iter { let env = result?; }

// Interchange — fully materialise into ParsedInterchange
let reader = std::fs::File::open("interchange.edi")?;
let ic = parser.parse_interchange_full(reader)?;
assert!(ic.is_structurally_valid());

Implementations§

Source§

impl Parser

Source

pub fn new() -> Self

Create a Parser with the default ParseConfig.

Source

pub fn with_config(config: ParseConfig) -> Self

Create a Parser with the given ParseConfig.

Source

pub fn parse(&self, input: &[u8]) -> Result<AnyMessage, Error>

Parse a single EDI@Energy message from an in-memory byte slice.

§Errors

Returns Err on EDIFACT syntax errors or unknown message type.

Source

pub fn parse_reader(&self, reader: impl Read) -> Result<AnyMessage, Error>

Parse a single EDI@Energy message from a Read source.

Reads the entire source into a segment list, then dispatches to the appropriate typed message variant. For &[u8] inputs, prefer Parser::parse to avoid the buffered read.

§Errors

Returns Err on I/O errors, EDIFACT syntax errors, or unknown message type.

Source

pub fn parse_envelope_only(&self, input: &[u8]) -> Result<LightMessage, Error>

Parse only the UNH/BGM envelope fields from a byte slice, without constructing typed message structs.

Returns a LightMessage that exposes message type, release, message reference, and Prüfidentifikator at minimal cost (~zero allocation beyond the raw segment buffer). Useful for routing and forwarding paths that must inspect envelope fields before deciding whether to run full validation.

Call LightMessage::into_message when full typed access is needed.

§Errors

Returns Err on EDIFACT syntax errors or a missing UNH segment.

Source

pub fn parse_interchange( &self, reader: impl Read, ) -> impl Iterator<Item = Result<AnyMessage, Error>>

Parse all messages from an EDIFACT interchange (lazy iterator).

Returns a lazy iterator yielding one Result<AnyMessage, Error> per UNH…UNT message window. The UNB/UNZ envelope is consumed but not preserved; use Parser::parse_interchange_buffered or Parser::parse_interchange_full when the envelope is needed.

The max_messages_per_interchange from the parser’s ParseConfig (default: 1 000) is enforced. Override via Parser::with_config.

§Errors

Each iterator item is Result<AnyMessage, Error>.

Source

pub fn parse_interchange_buffered( &self, reader: impl Read, ) -> Result<(InterchangeHeader, InterchangeIter), Error>

Parse an EDIFACT interchange, returning the InterchangeHeader eagerly and messages lazily via InterchangeIter.

Segment tokenization is eager — the entire input is tokenized into a Vec<OwnedSegment> before this method returns. Message deserialization is lazy — typed struct construction is deferred to each next() call.

This is the recommended path for AS4 adapters that must inspect the UNB sender/receiver GLN and decide whether to process a message before paying the deserialization cost.

The max_messages_per_interchange from the parser’s ParseConfig (default: 1 000) is enforced during iteration.

§Errors

Returns Err eagerly on I/O errors, syntax errors, or a missing UNB. Per-message errors and Error::TooManyMessages are returned as Err iterator items from the returned InterchangeIter.

Source

pub fn parse_interchange_full( &self, reader: impl Read, ) -> Result<ParsedInterchange, Error>

Fully parse an EDIFACT interchange into a ParsedInterchange, materialising all messages eagerly.

Use this when you need all messages and the UNB/UNZ envelope together. For large interchanges prefer Parser::parse_interchange_buffered to keep memory usage proportional to the number of messages you actually need.

Validates the UNZ control reference and message count before returning.

§Errors

Returns Err on I/O errors, syntax errors, envelope structural errors (missing UNB/UNZ, mismatched control reference or count), or individual message parse errors.

Trait Implementations§

Source§

impl Clone for Parser

Source§

fn clone(&self) -> Parser

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Parser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Parser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.