ifc_lite_landxml/
source.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Every supported LandXML family from one read of the bytes (C9a-10, #7784,
6//! EDGE-LXPARSE-8).
7//!
8//! Terrain, plan, alignments and pipe networks used to be four parses of the
9//! same buffer (three for `parse_landxml_document`, whose pipe pass was
10//! recognised by matching an error message). They are handlers on one
11//! [`Driver`](crate::xml::Driver), so the bytes are decoded, scanned and
12//! parsed once and each family sees every event.
13
14use crate::{
15    alignment::{
16        parser::AlignmentHandler, LandXmlAlignmentDocument, LandXmlAlignmentLimits,
17    },
18    parser::TinHandler,
19    pipe_parser::PipeHandler,
20    plan::{parser::PlanHandler, LandXmlDocument, LandXmlPlanLimits},
21    xml::{drive, error, normalize_encoding, FamilyHandler, Meter},
22    LandXmlCancellation, LandXmlDiagnosticCode as Code, LandXmlError, LandXmlLimits,
23};
24
25/// Bounds for [`parse_landxml_source_with_cancel`].
26///
27/// `xml` bounds the one read of the document and wins over the `xml` field
28/// nested in `plan` and `alignment`, which is ignored here: the families share
29/// one read, so they cannot be given different XML limits.
30#[derive(Clone, Debug, Default)]
31pub struct LandXmlSourceLimits {
32    pub xml: LandXmlLimits,
33    pub plan: LandXmlPlanLimits,
34    pub alignment: LandXmlAlignmentLimits,
35}
36
37/// Terrain, plan and pipe networks (the [`LandXmlDocument`]) and the horizontal
38/// alignments of one LandXML source.
39#[derive(Clone, Debug, PartialEq)]
40pub struct LandXmlSource {
41    pub document: LandXmlDocument,
42    pub alignments: LandXmlAlignmentDocument,
43}
44
45/// Parse terrain, plan, pipe networks and alignments with default limits.
46pub fn parse_landxml_source(input: &[u8]) -> Result<LandXmlSource, LandXmlError> {
47    parse_landxml_source_with_cancel(input, &LandXmlSourceLimits::default(), None)
48}
49
50/// Parse every supported family in one pass, with host limits and
51/// cancellation. A terrain-only or alignment-only source returns an honest
52/// empty sibling collection.
53pub fn parse_landxml_source_with_cancel(
54    input: &[u8],
55    limits: &LandXmlSourceLimits,
56    cancelled: Option<&dyn LandXmlCancellation>,
57) -> Result<LandXmlSource, LandXmlError> {
58    let (document, alignments) = parse_families(input, limits, cancelled, true)?;
59    Ok(LandXmlSource {
60        document,
61        alignments: alignments.expect("alignments were requested"),
62    })
63}
64
65/// The one pass. `alignments: false` leaves the alignment family out, so a
66/// document whose alignments are malformed still yields its terrain, plan
67/// and pipes (what `parse_landxml_document` has always done).
68pub(crate) fn parse_families(
69    input: &[u8],
70    limits: &LandXmlSourceLimits,
71    cancelled: Option<&dyn LandXmlCancellation>,
72    alignments: bool,
73) -> Result<(LandXmlDocument, Option<LandXmlAlignmentDocument>), LandXmlError> {
74    if input.len() > limits.xml.max_bytes {
75        return Err(error(Code::InputTooLarge, "input exceeds byte limit"));
76    }
77    // #5175: refuse a bad assumed-unit override before any XML is read.
78    let assumed_units = crate::semantics::resolve_assumed_units(&limits.xml)?;
79    let input = normalize_encoding(input, &limits.xml, cancelled)?;
80    let meter = Meter::new(limits.xml.max_work, cancelled);
81    let plan_limits = LandXmlPlanLimits { xml: limits.xml.clone(), ..limits.plan.clone() };
82    let alignment_limits = LandXmlAlignmentLimits { xml: limits.xml.clone(), ..limits.alignment.clone() };
83    let mut tin = TinHandler::new(&limits.xml, meter.clone(), cancelled, assumed_units.clone());
84    let mut plan = PlanHandler::new(&plan_limits, assumed_units.clone());
85    let mut pipe = PipeHandler::new(&limits.xml, meter.clone(), false);
86    let mut alignment = AlignmentHandler::new(&alignment_limits, false, assumed_units);
87    {
88        let mut handlers: Vec<&mut dyn FamilyHandler> = vec![&mut tin, &mut plan, &mut pipe];
89        if alignments {
90            handlers.push(&mut alignment);
91        }
92        drive(&input, &limits.xml, cancelled, meter, &mut handlers)?;
93    }
94    let mut terrain = tin.finish()?;
95    let (pipes, _, has_pipe_networks) = pipe.finish_stream_with_preflight()?;
96    terrain.pipe_networks = has_pipe_networks.then_some(pipes);
97    let document = LandXmlDocument { terrain, plan: plan.document() };
98    let alignments = alignments.then(|| alignment.finish()).transpose()?;
99    Ok((document, alignments))
100}