Skip to main content

openbim_ilcd_epd/
model.rs

1use std::io::{self, Write};
2
3use openbim_epd::InformationModule;
4
5use crate::{parser, ParseError, ParseOptions};
6
7/// Explicitly supported ILCD+EPD wire-format revision.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub enum FormatVersion {
10    /// InData ILCD+EPD v1.3 (`epd2019:epd-version="1.3"`).
11    V1_3,
12}
13
14impl FormatVersion {
15    /// Exact wire value.
16    #[must_use]
17    pub const fn as_str(self) -> &'static str {
18        match self {
19            Self::V1_3 => "1.3",
20        }
21    }
22}
23
24/// One ILCD multilingual `baseName` value.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct MultilingualName {
27    language: String,
28    text: String,
29}
30
31impl MultilingualName {
32    pub(crate) fn new(language: String, text: String) -> Self {
33        Self { language, text }
34    }
35
36    /// BCP 47-style language tag carried by `xml:lang`.
37    #[must_use]
38    pub fn language(&self) -> &str {
39        &self.language
40    }
41
42    /// Name text after XML entity decoding.
43    #[must_use]
44    pub fn text(&self) -> &str {
45        &self.text
46    }
47}
48
49/// Typed, adapter-specific view of one ILCD process dataset.
50///
51/// Fields not represented here remain available in [`Document::as_bytes`].
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub struct ProcessDataset {
54    version: FormatVersion,
55    uuid: String,
56    names: Vec<MultilingualName>,
57    modules: Vec<InformationModule>,
58}
59
60impl ProcessDataset {
61    pub(crate) fn new(
62        version: FormatVersion,
63        uuid: String,
64        names: Vec<MultilingualName>,
65        modules: Vec<InformationModule>,
66    ) -> Self {
67        Self {
68            version,
69            uuid,
70            names,
71            modules,
72        }
73    }
74
75    /// ILCD+EPD wire-format revision.
76    #[must_use]
77    pub const fn version(&self) -> FormatVersion {
78        self.version
79    }
80
81    /// ILCD process UUID exactly as represented in XML text.
82    #[must_use]
83    pub fn uuid(&self) -> &str {
84        &self.uuid
85    }
86
87    /// Every non-empty multilingual base name in document order.
88    #[must_use]
89    pub fn names(&self) -> &[MultilingualName] {
90        &self.names
91    }
92
93    /// First base name matching `xml:lang` exactly.
94    #[must_use]
95    pub fn name(&self, language: &str) -> Option<&str> {
96        self.names
97            .iter()
98            .find(|name| name.language == language)
99            .map(MultilingualName::text)
100    }
101
102    /// Unique declared information modules in ISO 22057 normative order.
103    #[must_use]
104    pub fn modules(&self) -> &[InformationModule] {
105        &self.modules
106    }
107}
108
109/// Parsed ILCD+EPD document retaining the exact original UTF-8 bytes.
110#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct Document {
112    original: Box<[u8]>,
113    process: ProcessDataset,
114}
115
116impl Document {
117    pub(crate) fn parsed(original: &[u8], process: ProcessDataset) -> Self {
118        Self {
119            original: original.into(),
120            process,
121        }
122    }
123
124    /// Parses an ILCD+EPD v1.3 process dataset with default resource limits.
125    pub fn parse(input: &[u8]) -> Result<Self, ParseError> {
126        Self::parse_with_options(input, ParseOptions::default())
127    }
128
129    /// Parses with explicit resource limits.
130    pub fn parse_with_options(input: &[u8], options: ParseOptions) -> Result<Self, ParseError> {
131        parser::parse(input, options)
132    }
133
134    /// Typed process-dataset view.
135    #[must_use]
136    pub const fn process(&self) -> &ProcessDataset {
137        &self.process
138    }
139
140    /// Explicit ILCD+EPD wire-format revision.
141    #[must_use]
142    pub const fn format_version(&self) -> FormatVersion {
143        self.process.version()
144    }
145
146    /// Exact original XML bytes, including unknown content and formatting.
147    #[must_use]
148    pub fn as_bytes(&self) -> &[u8] {
149        &self.original
150    }
151
152    /// Writes the exact original XML bytes without reconstruction.
153    pub fn write_original(&self, writer: &mut impl Write) -> io::Result<()> {
154        writer.write_all(&self.original)
155    }
156
157    /// Consumes the document and returns the exact original XML bytes.
158    #[must_use]
159    pub fn into_bytes(self) -> Box<[u8]> {
160        self.original
161    }
162}