diff options
Diffstat (limited to 'morphology-img/src/template.rs')
| -rw-r--r-- | morphology-img/src/template.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/morphology-img/src/template.rs b/morphology-img/src/template.rs index afa091b..15a47d5 100644 --- a/morphology-img/src/template.rs +++ b/morphology-img/src/template.rs @@ -1 +1,53 @@ // 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 PLACEHOLDER: &str = "%%%"; + +/// 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 the placeholder in the template with the provided markup. +/// +/// # Arguments +/// - `markup`: The LilyPond markup to substitute into the template +/// +/// # Returns +/// The complete .ly file content with substitution applied +/// +/// # Errors +/// Returns `RenderError::Template` if the placeholder is not found +pub fn substitute_template(markup: &str) -> Result<String, RenderError> { + let template = get_template(); + + if !template.contains(PLACEHOLDER) { + return Err(RenderError::Template( + "Template does not contain placeholder '%%%'".to_string() + )); + } + + Ok(template.replace(PLACEHOLDER, markup)) +} |