use crate::string::SmartString;
use crate::{Dictionary, Field, LayoutItem, LayoutItemData, LayoutItemKind};
#[derive(Clone, Debug)]
pub struct Component<'a>(pub(crate) &'a Dictionary, pub(crate) &'a ComponentData);
#[derive(Clone, Debug)]
pub(crate) struct ComponentData {
pub(crate) id: usize,
pub(crate) component_type: FixmlComponentAttributes,
pub(crate) layout_items: Vec<LayoutItemData>,
pub(crate) name: SmartString,
}
impl<'a> Component<'a> {
pub fn id(&self) -> u32 {
self.1.id as u32
}
pub fn name(&self) -> &str {
self.1.name.as_str()
}
pub fn is_group(&self) -> bool {
match self.1.component_type {
FixmlComponentAttributes::Block { is_repeating, .. } => is_repeating,
_ => false,
}
}
pub fn items(&self) -> impl Iterator<Item = LayoutItem<'_>> {
self.1
.layout_items
.iter()
.map(move |data| LayoutItem(self.0, data))
}
pub fn contains_field(&self, field: &Field) -> bool {
self.items().any(|layout_item| {
if let LayoutItemKind::Field(f) = layout_item.kind() {
f.tag() == field.tag()
} else {
false
}
})
}
}
#[derive(Clone, Debug, PartialEq)]
#[allow(dead_code)]
pub enum FixmlComponentAttributes {
Xml,
Block {
is_repeating: bool,
is_implicit: bool,
is_optimized: bool,
},
Message,
}