use std::borrow::Cow;
pub trait PromptFormatter: Send + Sync {
fn format<'a>(
&'a self,
prompt: &'a str,
feedback: Option<&str>,
iteration: u32,
) -> Cow<'a, str>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct PassthroughFormatter;
impl PromptFormatter for PassthroughFormatter {
#[inline]
fn format<'a>(
&'a self,
prompt: &'a str,
_feedback: Option<&str>,
_iteration: u32,
) -> Cow<'a, str> {
Cow::Borrowed(prompt)
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct FeedbackFormatter;
impl PromptFormatter for FeedbackFormatter {
fn format<'a>(
&'a self,
prompt: &'a str,
feedback: Option<&str>,
_iteration: u32,
) -> Cow<'a, str> {
match feedback {
Some(fb) => Cow::Owned(format!(
"{}\n\n[Feedback from previous attempt: {}]",
prompt, fb
)),
None => Cow::Borrowed(prompt),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_passthrough_formatter() {
let fmt = PassthroughFormatter;
let result = fmt.format("hello world", None, 0);
assert_eq!(result.as_ref(), "hello world");
assert!(matches!(result, Cow::Borrowed(_)));
}
#[test]
fn test_passthrough_with_feedback() {
let fmt = PassthroughFormatter;
let result = fmt.format("prompt", Some("feedback"), 3);
assert_eq!(result.as_ref(), "prompt");
assert!(matches!(result, Cow::Borrowed(_)));
}
#[test]
fn test_feedback_formatter_no_feedback() {
let fmt = FeedbackFormatter;
let result = fmt.format("prompt", None, 0);
assert_eq!(result.as_ref(), "prompt");
assert!(matches!(result, Cow::Borrowed(_)));
}
#[test]
fn test_feedback_formatter_with_feedback() {
let fmt = FeedbackFormatter;
let result = fmt.format("my task", Some("needs improvement"), 1);
assert!(result.contains("my task"));
assert!(result.contains("needs improvement"));
assert!(matches!(result, Cow::Owned(_)));
}
}