use crate::generated::model::StepModel;
use crate::parser::{Attribute, RawEntity, SchemaId};
#[derive(Debug, Clone, Default)]
pub struct FileHeader {
pub description: String,
pub file_name: String,
pub time_stamp: String,
pub authors: Vec<String>,
pub organizations: Vec<String>,
pub preprocessor_version: String,
pub originating_system: String,
pub authorisation: String,
pub schema: SchemaId,
}
impl StepModel {
#[must_use]
pub fn header(&self) -> &FileHeader {
&self.header
}
}
pub(crate) fn from_raw(raw: &[RawEntity], schema: SchemaId) -> FileHeader {
let mut h = FileHeader {
schema,
..FileHeader::default()
};
for ent in raw {
let RawEntity::Simple {
name, attributes, ..
} = ent
else {
continue;
};
let at = |i: usize| attributes.get(i);
match name.as_str() {
"FILE_NAME" => {
h.file_name = attr_str(at(0));
h.time_stamp = attr_str(at(1));
h.authors = attr_list(at(2));
h.organizations = attr_list(at(3));
h.preprocessor_version = attr_str(at(4));
h.originating_system = attr_str(at(5));
h.authorisation = attr_str(at(6));
}
"FILE_DESCRIPTION" => {
h.description = attr_list(at(0)).join(", ");
}
_ => {}
}
}
h
}
fn attr_str(a: Option<&Attribute>) -> String {
match a {
Some(Attribute::String(s)) => s.clone(),
_ => String::new(),
}
}
fn attr_list(a: Option<&Attribute>) -> Vec<String> {
match a {
Some(Attribute::List(items)) => items
.iter()
.filter_map(|it| match it {
Attribute::String(s) if !s.is_empty() => Some(s.clone()),
_ => None,
})
.collect(),
_ => Vec::new(),
}
}