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
use tinytemplate::{error::Error, format_unescaped};

/// Format a string as uppercase
///
/// # Errors
///
/// Errors if the value isn't string or null
pub fn format_upper(value: &serde_json::Value, output: &mut String) -> Result<(), Error> {
    let mut string_value = String::new();
    format_escape(value, &mut string_value)?;

    output.push_str(&string_value.to_uppercase());

    Ok(())
}

/// Escape special markdown sequences
///
/// # Errors
///
/// Errors if the value isn't string or null
pub fn format_escape(value: &serde_json::Value, output: &mut String) -> Result<(), Error> {
    let mut string_value = String::new();
    format_unescaped(value, &mut string_value)?;

    output.push_str(
        &string_value
            .replace("[", "\\[")
            .replace("]", "\\]")
            .replace("(", "\\(")
            .replace(")", "\\)")
            .replace("`", "\\`")
            .replace("<", "\\<")
            .replace(">", "\\>"),
    );

    Ok(())
}