pub const BEGIN_PREFIX: &str = "<!-- LIFELOOP:BEGIN managed-section";
pub const END_MARKER: &str = "<!-- LIFELOOP:END managed-section -->";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BeginMeta {
pub template_version: u32,
pub adapter_id: String,
}
pub fn render_begin(adapter_id: &str, template_version: u32) -> String {
format!("{BEGIN_PREFIX} v={template_version} adapter={adapter_id} -->",)
}
pub fn parse_begin(line: &str) -> Option<BeginMeta> {
let trimmed = line.trim();
let rest = trimmed.strip_prefix(BEGIN_PREFIX)?.trim();
let body = rest.strip_suffix("-->")?.trim();
let mut template_version: Option<u32> = None;
let mut adapter_id: Option<String> = None;
for token in body.split_whitespace() {
let (key, value) = token.split_once('=')?;
match key {
"v" => {
template_version = Some(value.parse().ok()?);
}
"adapter" => {
adapter_id = Some(value.to_owned());
}
_ => {}
}
}
Some(BeginMeta {
template_version: template_version?,
adapter_id: adapter_id?,
})
}
pub fn is_end(line: &str) -> bool {
line.trim() == END_MARKER
}