use super::TemplateComponent;
pub fn strip_author_component(component: &TemplateComponent) -> Option<TemplateComponent> {
match component {
TemplateComponent::Contributor(c)
if c.contributor == citum_schema::template::ContributorRole::Author =>
{
None
}
TemplateComponent::Group(list) => {
let filtered_items: Vec<TemplateComponent> = list
.group
.iter()
.filter_map(strip_author_component)
.collect();
if filtered_items.is_empty() {
None
} else {
let mut filtered_list = list.clone();
filtered_list.group = filtered_items;
Some(TemplateComponent::Group(filtered_list))
}
}
_ => Some(component.clone()),
}
}
pub fn leading_group_affix(component: &TemplateComponent) -> Option<String> {
let r = component.rendering();
let own_affix = r
.prefix
.clone()
.or_else(|| r.wrap.as_ref().and_then(|w| w.inner_prefix.clone()))
.or_else(|| {
if let TemplateComponent::Group(inner) = component {
inner.group.first().and_then(leading_group_affix)
} else {
None
}
});
own_affix.filter(|value| !value.is_empty())
}
pub fn strip_leading_group_affixes(component: &mut TemplateComponent) {
let r = component.rendering_mut();
r.prefix = None;
if let Some(ref mut wrap_config) = r.wrap {
wrap_config.inner_prefix = None;
}
if let TemplateComponent::Group(inner) = component
&& let Some(first) = inner.group.first_mut()
{
strip_leading_group_affixes(first);
}
}
pub fn find_grouping_component(component: &TemplateComponent) -> Option<&TemplateComponent> {
match component {
TemplateComponent::Contributor(_) | TemplateComponent::Title(_) => Some(component),
TemplateComponent::Group(list) => list.group.iter().find_map(find_grouping_component),
_ => None,
}
}
pub fn has_contributor_component(component: &TemplateComponent) -> bool {
match component {
TemplateComponent::Contributor(_) => true,
TemplateComponent::Group(list) => list.group.iter().any(has_contributor_component),
_ => false,
}
}
pub fn remove_first_contributor_with_role(
component: TemplateComponent,
role: &citum_schema::template::ContributorRole,
) -> (Option<TemplateComponent>, bool) {
match component {
TemplateComponent::Contributor(ref c) if &c.contributor == role => (None, true),
TemplateComponent::Group(mut list) => {
let mut removed = false;
let mut kept = Vec::with_capacity(list.group.len());
for child in list.group.drain(..) {
if removed {
kept.push(child);
continue;
}
let (remaining, child_removed) = remove_first_contributor_with_role(child, role);
removed = child_removed;
if let Some(remaining) = remaining {
kept.push(remaining);
}
}
if kept.is_empty() {
(None, removed)
} else {
list.group = kept;
(Some(TemplateComponent::Group(list)), removed)
}
}
other => (Some(other), false),
}
}