hedl_xml/from_xml/config.rs
1// Dweve HEDL - Hierarchical Entity Data Language
2//
3// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
4//
5// SPDX-License-Identifier: Apache-2.0
6//
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License in the LICENSE file at the
10// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Configuration for XML to HEDL conversion
19
20/// Policy for handling XML entities and DTDs
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
22pub enum EntityPolicy {
23 /// Reject XML with DOCTYPE declarations (strictest, recommended)
24 RejectDtd,
25 /// Allow DOCTYPE but never resolve external entities (default)
26 #[default]
27 AllowDtdNoExternal,
28 /// Log warnings when DTDs or entity references detected
29 WarnOnEntities,
30}
31
32/// Configuration for XML import
33#[derive(Debug, Clone)]
34pub struct FromXmlConfig {
35 /// Default type name for list items without metadata
36 pub default_type_name: String,
37 /// HEDL version to use
38 pub version: (u32, u32),
39 /// Try to infer list structures from repeated elements
40 pub infer_lists: bool,
41
42 /// Entity handling policy (XXE prevention)
43 pub entity_policy: EntityPolicy,
44
45 /// Enable security event logging
46 pub log_security_events: bool,
47}
48
49impl Default for FromXmlConfig {
50 fn default() -> Self {
51 Self {
52 default_type_name: "Item".to_string(),
53 version: (2, 0),
54 infer_lists: true,
55 entity_policy: EntityPolicy::default(),
56 log_security_events: false,
57 }
58 }
59}
60
61impl FromXmlConfig {
62 /// Create a config with strict security (reject DTDs entirely)
63 pub fn strict_security() -> Self {
64 Self {
65 entity_policy: EntityPolicy::RejectDtd,
66 log_security_events: true,
67 ..Default::default()
68 }
69 }
70}
71
72impl hedl_core::convert::ImportConfig for FromXmlConfig {
73 fn default_type_name(&self) -> &str {
74 &self.default_type_name
75 }
76
77 fn version(&self) -> (u32, u32) {
78 self.version
79 }
80}