Parse a file line by line

I have a particular use case - it's about 2 specific payment protocols: I have researched Rust crates/libraries etc but found no satisfactory solution.
Firstly, I want to process a NACHA file. It comprises of fixed length records, 94 bytes, that I need to parse (break up) into individual fields. How? This is an easy solution in Perl
Secondly, I a dealing with ISO20022 (XML) message format. I need to create a DOM and then reference each field, Solution? Thoughts?

This seems to be a decent write-up of the options of how to handle XML in Rust.

For the NACHA / ACH format, I couldn't find pre-existing solutions in Rust. I guess, either try to implement it yourself or use some existing tool for converting it into a different format first?

Note that I'm unfamiliar with the formats at hand; I only googled what NACHA is, and I haven't looked at ISO20022 at all for this answer.

Steffahn
Parsing a file, line by line, should be easy. For NACHA, it's a simple use case - take a line of data, 94 bytes long, and split it into fields: 1 byte, 2 bytes, 10 bytes, 10 bytes, 12 bytes etc up to 94 bytes.
But I'll be darned if I can find a way/reference to do that

Something like this?

use std::io::BufRead;

fn main() {
    let file = std::fs::File::open("my file").unwrap();
    let reader = std::io::BufReader::new(file);
    for line in reader.lines() {
        let line = line.unwrap();
        let record_type_code = &line[0..1];
        let priority_code = &line[1..3];
        let immediate_destination = &line[3..13];
        let immediate_origin = &line[13..23];
        // etc...
}
1 Like

Great! Thank you

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.