#![allow(dead_code)]
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptTemplate {
pub name: String,
pub template: String,
pub variables: Vec<String>,
pub description: String,
}
impl PromptTemplate {
pub fn render(&self, variables: &HashMap<String, String>) -> String {
let mut result = self.template.clone();
for (key, value) in variables {
let placeholder = format!("{{{{{}}}}}", key);
result = result.replace(&placeholder, value);
}
result
}
}
pub struct PromptLibrary;
impl PromptLibrary {
pub fn explain_code() -> PromptTemplate {
PromptTemplate {
name: "explain_code".to_string(),
description: "Explain a code snippet in the developer's style".to_string(),
variables: vec!["code".to_string(), "language".to_string()],
template: r#"You are analyzing code written by a developer. Explain the following {language} code:
```{language}
{code}
```
Provide:
1. What this code does (high-level purpose)
2. Key patterns and techniques used
3. Any notable design decisions
4. Potential improvements or considerations
Keep your explanation clear and concise."#.to_string(),
}
}
pub fn suggest_code() -> PromptTemplate {
PromptTemplate {
name: "suggest_code".to_string(),
description: "Generate code following the developer's patterns".to_string(),
variables: vec!["description".to_string(), "language".to_string(), "patterns".to_string()],
template: r#"Generate {language} code for the following:
Description: {description}
The developer's typical patterns:
{patterns}
Please generate clean, well-documented code that follows these patterns and best practices."#.to_string(),
}
}
pub fn review_code() -> PromptTemplate {
PromptTemplate {
name: "review_code".to_string(),
description: "Review code and suggest improvements".to_string(),
variables: vec!["code".to_string(), "language".to_string()],
template: r#"Review the following {language} code and suggest improvements:
```{language}
{code}
```
Focus on:
1. Performance optimizations
2. Code readability and maintainability
3. Error handling
4. Best practices for {language}
5. Potential bugs or edge cases
Provide specific, actionable suggestions."#.to_string(),
}
}
pub fn summarize_work() -> PromptTemplate {
PromptTemplate {
name: "summarize_work".to_string(),
description: "Summarize recent development work".to_string(),
variables: vec!["commits".to_string(), "period".to_string()],
template: r#"Summarize the following development work from {period}:
{commits}
Provide a concise summary highlighting:
1. Main areas of focus
2. Key achievements or milestones
3. Technologies and patterns used
4. Overall progress and momentum
Keep it to 3-5 bullet points."#.to_string(),
}
}
pub fn answer_pattern_question() -> PromptTemplate {
PromptTemplate {
name: "answer_pattern_question".to_string(),
description: "Answer questions about the developer's coding patterns".to_string(),
variables: vec!["question".to_string(), "patterns".to_string()],
template: r#"Based on the following coding patterns:
{patterns}
Answer this question: {question}
Provide a specific, helpful answer based on the patterns shown above."#.to_string(),
}
}
pub fn generate_commit_message() -> PromptTemplate {
PromptTemplate {
name: "generate_commit_message".to_string(),
description: "Generate a commit message based on changes".to_string(),
variables: vec!["diff".to_string(), "style".to_string()],
template: r#"Generate a commit message for the following changes:
{diff}
The developer's typical commit message style: {style}
Generate a concise, informative commit message following this style."#.to_string(),
}
}
pub fn all_templates() -> Vec<PromptTemplate> {
vec![
Self::explain_code(),
Self::suggest_code(),
Self::review_code(),
Self::summarize_work(),
Self::answer_pattern_question(),
Self::generate_commit_message(),
]
}
}
pub struct PromptBuilder {
sections: Vec<String>,
}
impl PromptBuilder {
pub fn new() -> Self {
Self {
sections: Vec::new(),
}
}
pub fn add_section(mut self, title: &str, content: &str) -> Self {
self.sections.push(format!("## {}\n\n{}", title, content));
self
}
pub fn add_code_example(mut self, language: &str, code: &str) -> Self {
self.sections.push(format!("```{}\n{}\n```", language, code));
self
}
pub fn add_instruction(mut self, instruction: &str) -> Self {
self.sections.push(format!("**Instruction:** {}", instruction));
self
}
pub fn build(self) -> String {
self.sections.join("\n\n")
}
}
impl Default for PromptBuilder {
fn default() -> Self {
Self::new()
}
}