openbim_ilcd_epd/
model.rs1use std::io::{self, Write};
2
3use openbim_epd::InformationModule;
4
5use crate::{parser, ParseError, ParseOptions};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub enum FormatVersion {
10 V1_3,
12}
13
14impl FormatVersion {
15 #[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#[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 #[must_use]
38 pub fn language(&self) -> &str {
39 &self.language
40 }
41
42 #[must_use]
44 pub fn text(&self) -> &str {
45 &self.text
46 }
47}
48
49#[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 #[must_use]
77 pub const fn version(&self) -> FormatVersion {
78 self.version
79 }
80
81 #[must_use]
83 pub fn uuid(&self) -> &str {
84 &self.uuid
85 }
86
87 #[must_use]
89 pub fn names(&self) -> &[MultilingualName] {
90 &self.names
91 }
92
93 #[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 #[must_use]
104 pub fn modules(&self) -> &[InformationModule] {
105 &self.modules
106 }
107}
108
109#[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 pub fn parse(input: &[u8]) -> Result<Self, ParseError> {
126 Self::parse_with_options(input, ParseOptions::default())
127 }
128
129 pub fn parse_with_options(input: &[u8], options: ParseOptions) -> Result<Self, ParseError> {
131 parser::parse(input, options)
132 }
133
134 #[must_use]
136 pub const fn process(&self) -> &ProcessDataset {
137 &self.process
138 }
139
140 #[must_use]
142 pub const fn format_version(&self) -> FormatVersion {
143 self.process.version()
144 }
145
146 #[must_use]
148 pub fn as_bytes(&self) -> &[u8] {
149 &self.original
150 }
151
152 pub fn write_original(&self, writer: &mut impl Write) -> io::Result<()> {
154 writer.write_all(&self.original)
155 }
156
157 #[must_use]
159 pub fn into_bytes(self) -> Box<[u8]> {
160 self.original
161 }
162}