ifc_template_catalog/
xml.rs1mod common;
4mod node;
5mod property;
6mod psd;
7mod qto;
8
9use thiserror::Error;
10
11use crate::definition::{ApplicabilityError, SetTemplate};
12use node::Node;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct ImportLimits {
17 pub max_bytes: usize,
19 pub max_nodes: usize,
21 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
35pub fn parse_template(xml: &str) -> Result<SetTemplate, XmlImportError> {
37 parse_template_with_limits(xml, ImportLimits::default())
38}
39
40pub 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]
61pub enum XmlImportError {
63 #[error("XML {kind} limit exceeded ({limit})")]
64 LimitExceeded {
66 kind: &'static str,
68 limit: usize,
70 },
71 #[error("XML has no root element")]
72 MissingRoot,
74 #[error("XML has multiple root elements")]
75 MultipleRoots,
77 #[error("invalid XML: {0}")]
78 Xml(String),
80 #[error("unsupported catalog root `{0}`")]
81 UnsupportedRoot(String),
83 #[error("missing `{field}` at `{path}`")]
84 MissingField {
86 path: String,
88 field: String,
90 },
91 #[error("property `{path}` has zero or multiple property type elements")]
92 AmbiguousPropertyType {
94 path: String,
96 },
97 #[error("unsupported property type `{element}` for `{set}.{property}`")]
98 UnsupportedPropertyType {
100 set: String,
102 property: String,
104 element: String,
106 },
107 #[error("unsupported quantity type `{value}` for `{set}.{quantity}`")]
108 UnsupportedQuantityType {
110 set: String,
112 quantity: String,
114 value: String,
116 },
117 #[error("unsupported property-set template type `{value}` for `{set}`")]
118 UnsupportedSetType {
120 set: String,
122 value: String,
124 },
125 #[error(transparent)]
126 Applicability(#[from] ApplicabilityError),
128}