Skip to main content

ifc_template_catalog/
xml.rs

1//! PSD and QTO XML import.
2
3mod common;
4mod node;
5mod property;
6mod psd;
7mod qto;
8
9use thiserror::Error;
10
11use crate::definition::{ApplicabilityError, SetTemplate};
12use node::Node;
13
14/// Resource limits for untrusted catalog XML.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct ImportLimits {
17    /// Maximum accepted document size in bytes.
18    pub max_bytes: usize,
19    /// Maximum number of parsed elements.
20    pub max_nodes: usize,
21    /// Maximum element nesting depth.
22    pub max_depth: usize,
23}
24
25impl Default for ImportLimits {
26    fn default() -> Self {
27        Self {
28            max_bytes: 4 * 1024 * 1024,
29            max_nodes: 100_000,
30            max_depth: 128,
31        }
32    }
33}
34
35/// Parse one PSD `PropertySetDef` or QTO `QtoSetDef` document.
36pub fn parse_template(xml: &str) -> Result<SetTemplate, XmlImportError> {
37    parse_template_with_limits(xml, ImportLimits::default())
38}
39
40/// Parse one template while bounding input bytes, nodes, and nesting.
41pub fn parse_template_with_limits(
42    xml: &str,
43    limits: ImportLimits,
44) -> Result<SetTemplate, XmlImportError> {
45    if xml.len() > limits.max_bytes {
46        return Err(XmlImportError::LimitExceeded {
47            kind: "bytes",
48            limit: limits.max_bytes,
49        });
50    }
51    let root = node::parse(xml, limits)?;
52    match root.name.as_str() {
53        "PropertySetDef" => psd::parse(&root),
54        "QtoSetDef" => qto::parse(&root),
55        value => Err(XmlImportError::UnsupportedRoot(value.to_owned())),
56    }
57}
58
59#[derive(Debug, Error)]
60#[non_exhaustive]
61/// Why a PSD or QTO document could not be imported.
62pub enum XmlImportError {
63    #[error("XML {kind} limit exceeded ({limit})")]
64    /// A declared resource limit was exceeded before parsing completed.
65    LimitExceeded {
66        /// Which limit was hit: bytes, nodes, or depth.
67        kind: &'static str,
68        /// The configured maximum for that limit.
69        limit: usize,
70    },
71    #[error("XML has no root element")]
72    /// The document contained no root element.
73    MissingRoot,
74    #[error("XML has multiple root elements")]
75    /// The document declared more than one root element.
76    MultipleRoots,
77    #[error("invalid XML: {0}")]
78    /// The document was not well-formed XML.
79    Xml(String),
80    #[error("unsupported catalog root `{0}`")]
81    /// The root element is neither PropertySetDef nor QtoSetDef.
82    UnsupportedRoot(String),
83    #[error("missing `{field}` at `{path}`")]
84    /// A required child element or attribute was absent.
85    MissingField {
86        /// Element path where the value was expected.
87        path: String,
88        /// Name of the absent field.
89        field: String,
90    },
91    #[error("property `{path}` has zero or multiple property type elements")]
92    /// A property declared zero or several property-type elements.
93    AmbiguousPropertyType {
94        /// Element path of the offending property.
95        path: String,
96    },
97    #[error("unsupported property type `{element}` for `{set}.{property}`")]
98    /// The property-type element is not one this importer maps.
99    UnsupportedPropertyType {
100        /// Name of the property set.
101        set: String,
102        /// Name of the property.
103        property: String,
104        /// The unmapped property-type element.
105        element: String,
106    },
107    #[error("unsupported quantity type `{value}` for `{set}.{quantity}`")]
108    /// The quantity-type token is not one this importer maps.
109    UnsupportedQuantityType {
110        /// Name of the quantity set.
111        set: String,
112        /// Name of the quantity.
113        quantity: String,
114        /// The unmapped quantity-type token.
115        value: String,
116    },
117    #[error("unsupported property-set template type `{value}` for `{set}`")]
118    /// The property-set template type token is not recognised.
119    UnsupportedSetType {
120        /// Name of the property set.
121        set: String,
122        /// The unrecognised template-type token.
123        value: String,
124    },
125    #[error(transparent)]
126    /// The parsed applicability expression was itself invalid.
127    Applicability(#[from] ApplicabilityError),
128}