use refix_dictionary::{Dictionary, MemberContext, dictionary};
use crate::flat;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum Section {
Header,
Trailer,
}
impl Section {
pub(crate) fn type_name(self) -> &'static str {
match self {
Section::Header => "Header",
Section::Trailer => "Trailer",
}
}
pub(crate) fn member_name(self) -> &'static str {
match self {
Section::Header => "header",
Section::Trailer => "trailer",
}
}
pub(crate) fn context(self) -> MemberContext {
match self {
Section::Header => MemberContext::Header,
Section::Trailer => MemberContext::Trailer,
}
}
pub(crate) fn members(self, dictionary: &Dictionary) -> Vec<flat::Member<'_>> {
flat::members(self.listed(dictionary).into_iter()).collect()
}
pub(crate) fn listed(self, dictionary: &Dictionary) -> Vec<dictionary::Member<'_>> {
match self {
Section::Header => dictionary.header().collect(),
Section::Trailer => dictionary.trailer().collect(),
}
}
}
pub(crate) struct Envelope {
pub(crate) sections: Vec<Section>,
pub(crate) with_groups: Vec<Section>,
}
impl Envelope {
pub(crate) fn of(dictionary: &Dictionary) -> Self {
let mut sections = Vec::new();
let mut with_groups = Vec::new();
for section in [Section::Header, Section::Trailer] {
let members = section.members(dictionary);
if members.is_empty() {
continue;
}
sections.push(section);
if members
.iter()
.any(|member| matches!(member, flat::Member::Group(_)))
{
with_groups.push(section);
}
}
Self {
sections,
with_groups,
}
}
}