use refix_dictionary::{Dictionary, MemberContext, Tag, dictionary};
use crate::groups::known_tags;
#[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<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>,
pub(crate) tags: Vec<Tag>,
}
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, dictionary::Member::Group(_)))
{
with_groups.push(section);
}
}
Self {
sections,
with_groups,
tags: known_tags(dictionary.header().chain(dictionary.trailer())),
}
}
}