use crate::key::Key;
use std::collections::BTreeSet;
#[derive(Eq, Ord, PartialEq, PartialOrd)]
pub(crate) struct Section {
name: String,
keys: BTreeSet<Key>,
children: BTreeSet<Section>,
}
impl Section {
pub(crate) fn new(name: &str) -> Self {
Self {
name: name.into(),
keys: BTreeSet::new(),
children: BTreeSet::new(),
}
}
pub(crate) fn add_key(mut self, key: Key) -> Self {
self.keys.insert(key);
self
}
pub(crate) fn add_child(mut self, child: Section) -> Self {
self.children.insert(child);
self
}
pub(crate) fn generate(self) -> String {
format!(
r"
#[allow(unused)]
pub(crate) mod {section_name} {{
{keys}
{children}
}}",
section_name = self.name,
keys = self
.keys
.into_iter()
.map(|k| k.generate())
.fold(String::new(), |acc, key| format!("{}\n\n{}", acc, key)),
children = self
.children
.into_iter()
.map(|sec| sec.generate())
.fold(String::new(), |acc, sec| format!("{}\n\n{}", acc, sec))
)
}
}