parse_meta/
parse_meta.rs

1// nrbf-parser - A high-performance MS-NRBF binary parser and encoder.
2// Copyright (C) 2026  driedpampas@proton.me
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17use nrbf_parser::Decoder;
18use nrbf_parser::records::Record;
19use std::env;
20use std::fs::File;
21use std::io::{BufReader, Write};
22
23fn main() -> Result<(), Box<dyn std::error::Error>> {
24    let args: Vec<String> = env::args().collect();
25    if args.len() < 2 {
26        eprintln!("Usage: {} <nrbf_file> [output_json]", args[0]);
27        std::process::exit(1);
28    }
29
30    let file = File::open(&args[1])?;
31    let reader = BufReader::new(file);
32    let mut decoder = Decoder::new(reader);
33
34    let mut records = Vec::new();
35    while let Some(record) = decoder.decode_next()? {
36        records.push(record);
37        if let Record::MessageEnd = records.last().unwrap() {
38            break;
39        }
40    }
41
42    let output_path = args.get(2).map(|s| s.as_str()).unwrap_or("output.json");
43    let json = serde_json::to_string_pretty(&records)?;
44
45    let mut out_file = File::create(output_path)?;
46    out_file.write_all(json.as_bytes())?;
47
48    println!(
49        "Successfully parsed {} records and saved to {}",
50        records.len(),
51        output_path
52    );
53
54    Ok(())
55}