summaryrefslogtreecommitdiff
path: root/morphology-img/src/template.rs
blob: 15a47d56f10bee6641382361c3a93de640fc2cf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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))
}