// Template handling and substitution use crate::error::RenderError; /// Template files embedded at compile time const TEMPLATE_LY: &str = include_str!("../static/template.ly"); const FORMAT_ILY: &str = include_str!("../static/format.ily"); const ENTRY_ILY: &str = include_str!("../static/entry.ily"); const ELEMENTS_ILY: &str = include_str!("../static/elements.ily"); const MORPHEME_PLACEHOLDER: &str = "%MORPHEME%"; const RUNGS_PLACEHOLDER: &str = "%RUNGS%"; const LEDGERS_PLACEHOLDER: &str = "%LEDGERS%"; /// Returns the template .ly content pub fn get_template() -> &'static str { TEMPLATE_LY } /// Returns the format.ily content pub fn get_format_ily() -> &'static str { FORMAT_ILY } /// Returns the entry.ily content pub fn get_entry_ily() -> &'static str { ENTRY_ILY } /// Returns the elements.ily content pub fn get_elements_ily() -> &'static str { ELEMENTS_ILY } /// Substitutes placeholders in the template with the provided values. /// /// # Arguments /// - `morpheme`: The LilyPond morpheme markup to substitute /// - `rungs`: The Scheme list string for staff line positions /// - `ledgers`: The Scheme list string for ledger line positions /// /// # Returns /// The complete .ly file content with substitutions applied /// /// # Errors /// Returns `RenderError::Template` if placeholders are not found pub fn substitute_template(morpheme: &str, rungs: &str, ledgers: &str) -> Result { let template = get_template(); if !template.contains(MORPHEME_PLACEHOLDER) { return Err(RenderError::Template( "Template does not contain placeholder '%MORPHEME%'".to_string() )); } if !template.contains(RUNGS_PLACEHOLDER) { return Err(RenderError::Template( "Template does not contain placeholder '%RUNGS%'".to_string() )); } if !template.contains(LEDGERS_PLACEHOLDER) { return Err(RenderError::Template( "Template does not contain placeholder '%LEDGERS%'".to_string() )); } let result = template .replace(MORPHEME_PLACEHOLDER, morpheme) .replace(RUNGS_PLACEHOLDER, rungs) .replace(LEDGERS_PLACEHOLDER, ledgers); Ok(result) }