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
impl Parser
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a Parser with the default ParseConfig.
Sourcepub fn with_config(config: ParseConfig) -> Self
pub fn with_config(config: ParseConfig) -> Self
Create a Parser with the given ParseConfig.
Sourcepub fn parse(&self, input: &[u8]) -> Result<AnyMessage, Error>
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.
Sourcepub fn parse_reader(&self, reader: impl Read) -> Result<AnyMessage, Error>
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.
Sourcepub fn parse_envelope_only(&self, input: &[u8]) -> Result<LightMessage, Error>
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.
Sourcepub fn parse_interchange(
&self,
reader: impl Read,
) -> impl Iterator<Item = Result<AnyMessage, Error>>
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>.
Error::Parse— I/O or EDIFACT syntax error.Error::TooManyMessages— interchange exceeds the configured message limit (ParseConfig::max_messages_per_interchange).
Sourcepub fn parse_interchange_buffered(
&self,
reader: impl Read,
) -> Result<(InterchangeHeader, InterchangeIter), Error>
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.
Sourcepub fn parse_interchange_full(
&self,
reader: impl Read,
) -> Result<ParsedInterchange, Error>
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.