pub fn apply(template: Option<&str>, instruction: Option<&str>, text: &str) -> String {
match template {
None => text.to_string(),
Some(template) => template
.replace("{instruction}", instruction.unwrap_or_default())
.replace("{text}", text),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn none_returns_text() {
assert_eq!(apply(None, None, "hello"), "hello");
}
#[test]
fn replaces_text_and_instruction() {
let template = "Instruct: {instruction}\nQuery: {text}";
assert_eq!(
apply(Some(template), Some("Retrieve passages"), "what is x"),
"Instruct: Retrieve passages\nQuery: what is x"
);
assert_eq!(apply(Some("query: {text}"), None, "a"), "query: a");
}
#[test]
fn text_placeholders_are_not_reprocessed() {
assert_eq!(
apply(Some("{text}"), Some("i"), "{instruction}"),
"{instruction}"
);
}
}