openbim_ilcd_epd/
model.rsuse std::io::{self, Write};
use openbim_epd::InformationModule;
use crate::{parser, ParseError, ParseOptions};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FormatVersion {
V1_3,
}
impl FormatVersion {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::V1_3 => "1.3",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MultilingualName {
language: String,
text: String,
}
impl MultilingualName {
pub(crate) fn new(language: String, text: String) -> Self {
Self { language, text }
}
#[must_use]
pub fn language(&self) -> &str {
&self.language
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessDataset {
version: FormatVersion,
uuid: String,
names: Vec<MultilingualName>,
modules: Vec<InformationModule>,
}
impl ProcessDataset {
pub(crate) fn new(
version: FormatVersion,
uuid: String,
names: Vec<MultilingualName>,
modules: Vec<InformationModule>,
) -> Self {
Self {
version,
uuid,
names,
modules,
}
}
#[must_use]
pub const fn version(&self) -> FormatVersion {
self.version
}
#[must_use]
pub fn uuid(&self) -> &str {
&self.uuid
}
#[must_use]
pub fn names(&self) -> &[MultilingualName] {
&self.names
}
#[must_use]
pub fn name(&self, language: &str) -> Option<&str> {
self.names
.iter()
.find(|name| name.language == language)
.map(MultilingualName::text)
}
#[must_use]
pub fn modules(&self) -> &[InformationModule] {
&self.modules
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Document {
original: Box<[u8]>,
process: ProcessDataset,
}
impl Document {
pub(crate) fn parsed(original: &[u8], process: ProcessDataset) -> Self {
Self {
original: original.into(),
process,
}
}
pub fn parse(input: &[u8]) -> Result<Self, ParseError> {
Self::parse_with_options(input, ParseOptions::default())
}
pub fn parse_with_options(input: &[u8], options: ParseOptions) -> Result<Self, ParseError> {
parser::parse(input, options)
}
#[must_use]
pub const fn process(&self) -> &ProcessDataset {
&self.process
}
#[must_use]
pub const fn format_version(&self) -> FormatVersion {
self.process.version()
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.original
}
pub fn write_original(&self, writer: &mut impl Write) -> io::Result<()> {
writer.write_all(&self.original)
}
#[must_use]
pub fn into_bytes(self) -> Box<[u8]> {
self.original
}
}