Skip to main content

parse_with

Function parse_with 

Source
pub fn parse_with(
    input: &[u8],
    options: ParseOptions,
) -> Result<ParseOutcome, StepError>
Expand description

Parses a physical file under explicit ParseOptions.

With OnMalformed::Skip an unparsable data record is reported as a Diagnostic and the parser resynchronizes on the next record or section end, so a consumer can load a damaged file and still show exactly what was dropped. Header structure stays strict under every policy: mandatory header records are file-level invariants, not recoverable payload.

ยงErrors

Returns StepError for the wrong physical-file marker, for any header or section-structure defect, and for data defects when the policy is OnMalformed::Abort.

Examples found in repository?
examples/recovery_probe.rs (line 12)
3fn main() {
4    let path = std::env::args().nth(1).expect("path");
5    let bytes = std::fs::read(&path).expect("read");
6
7    match openbim_step::parse(&bytes) {
8        Ok(_) => println!("strict: OK (unexpected)"),
9        Err(error) => println!("strict: {error}"),
10    }
11
12    let outcome = openbim_step::parse_with(&bytes, openbim_step::ParseOptions::lenient())
13        .expect("lenient parse");
14    println!(
15        "lenient: {} records, {} diagnostics",
16        outcome.exchange.data.records.len(),
17        outcome.diagnostics.len()
18    );
19    for diagnostic in &outcome.diagnostics {
20        println!("  {diagnostic}");
21        let span = diagnostic.span();
22        println!(
23            "  dropped bytes: {:?}",
24            String::from_utf8_lossy(&bytes[span.start..span.end])
25        );
26    }
27    let boundaries = outcome
28        .exchange
29        .data
30        .records
31        .iter()
32        .filter(|record| {
33            record
34                .as_simple()
35                .is_some_and(|simple| simple.name == "IFCRELSPACEBOUNDARY")
36        })
37        .count();
38    println!("IfcRelSpaceBoundary: {boundaries}");
39    println!("schema: {:?}", outcome.exchange.header.standard().schema);
40}