#x12 #stream #edi #ansi #parser #delimiter

x12-stream-parser

Low-level, iterator-based X12 EDI stream parser

1 unstable release

new 0.1.0 Apr 3, 2025

#10 in #x12

Download history 92/week @ 2025-03-30

92 downloads per month

MIT/Apache

21KB
295 lines

x12-stream-parser

CI Crates.io Documentation

A high-performance, low-level, iterator-based X12 EDI stream parser for Rust.

Features

  • Stream-based: Parse X12 data without loading the entire file into memory
  • Zero-copy: Uses byte slices to minimize memory allocation
  • Iterator-based: Iterate through segments as they're parsed
  • Lightweight: Minimal dependencies and small code footprint
  • Customizable: Support for custom delimiters and separator configurations
  • Fast: Optimized for performance in parsing large X12 files

Installation

Add this to your Cargo.toml:

[dependencies]
x12-stream-parser = "0.1.0"

Usage

use x12_stream_parser::{Parser, Delimiters};

fn main() {
    // Example X12 data with standard delimiters
    let data = b"ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *210101*1030*^*00501*000000001*0*P*>~GS*PO*SENDER*RECEIVER*20210101*1030*1*X*005010~ST*850*0001~BEG*00*SA*123456**20210101~N1*BY*BUYER NAME~N1*ST*SHIP TO NAME~CTT*1~SE*6*0001~GE*1*1~IEA*1*000000001~";

    // Create a parser with standard X12 delimiters
    let delimiters = Delimiters::new(b'~', b'*', b':');
    let parser = Parser::new(data, delimiters);

    // Iterate through the segments
    for result in parser {
        match result {
            Ok(segment) => {
                println!("Segment ID: {}", String::from_utf8_lossy(segment.id()));
                
                // Print each element in the segment
                for (i, element) in segment.elements().enumerate() {
                    println!("  Element {}: {}", i, String::from_utf8_lossy(element));
                }
            },
            Err(e) => {
                eprintln!("Error parsing segment: {:?}", e);
                break;
            }
        }
    }
}

Performance

The library is designed with performance in mind:

  • Minimizes memory allocations
  • Uses byte slices instead of strings where possible
  • Leverages Rust's zero-cost abstractions
  • Provides iterators for efficient streaming processing

Customization

You can customize the parser with different delimiters:

// Using non-standard delimiters
let custom_delimiters = Delimiters::new(b'|', b'^', b'&');
let parser = Parser::new(data, custom_delimiters);

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~20KB