summaryrefslogtreecommitdiff
path: root/morphology-img/src/template.rs
blob: e552843a92e05494d89180f9916042c11bd9828d (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
54
55
56
57
58
59
60
61
62
63
64
65
// 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%";

/// 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
///
/// # 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) -> Result<String, RenderError> {
    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()
        ));
    }

    let result = template
        .replace(MORPHEME_PLACEHOLDER, morpheme)
        .replace(RUNGS_PLACEHOLDER, rungs);

    Ok(result)
}