#[non_exhaustive]pub struct PromptManager<'a> { /* private fields */ }Expand description
Prompt manager for loading and rendering Jinja templates.
The manager uses MiniJinja as the template engine and supports loading templates from directories or adding them programmatically.
Implementations§
Source§impl<'a> PromptManager<'a>
impl<'a> PromptManager<'a>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new prompt manager with an empty template environment.
§Example
use gba_pm::PromptManager;
let manager = PromptManager::new();
assert!(manager.names().is_empty());Sourcepub fn load_dir(&mut self, path: impl AsRef<Path>) -> Result<&mut Self>
pub fn load_dir(&mut self, path: impl AsRef<Path>) -> Result<&mut Self>
Load templates from a directory.
This method recursively scans the given directory for template files
with extensions .j2, .jinja, or .jinja2. Template names are derived
from the relative path with the extension stripped.
§Arguments
path- Path to the directory containing templates
§Returns
Returns &mut Self to allow method chaining.
§Errors
Returns an error if:
- The directory cannot be read
- A template file cannot be read
- A template has invalid syntax
§Example
use gba_pm::PromptManager;
let mut manager = PromptManager::new();
manager.load_dir("./prompts")?;Sourcepub fn add(&mut self, name: &str, content: &str) -> Result<&mut Self>
pub fn add(&mut self, name: &str, content: &str) -> Result<&mut Self>
Add a template from a string.
§Arguments
name- The name to register the template undercontent- The template content
§Returns
Returns &mut Self to allow method chaining.
§Errors
Returns an error if the template has invalid syntax.
§Example
use gba_pm::PromptManager;
let mut manager = PromptManager::new();
manager.add("hello", "Hello, {{ name }}!")?;Sourcepub fn render(&self, name: &str, ctx: impl Serialize) -> Result<String>
pub fn render(&self, name: &str, ctx: impl Serialize) -> Result<String>
Render a template with the given context.
§Arguments
name- The name of the template to renderctx- The context data to pass to the template
§Returns
Returns the rendered template as a string.
§Errors
Returns an error if:
- The template is not found
- The template cannot be rendered with the given context
§Example
use gba_pm::PromptManager;
use serde_json::json;
let mut manager = PromptManager::new();
manager.add("greeting", "Hello, {{ name }}!")?;
let result = manager.render("greeting", json!({"name": "World"}))?;
assert_eq!(result, "Hello, World!");Sourcepub fn render_str(&self, template: &str, ctx: impl Serialize) -> Result<String>
pub fn render_str(&self, template: &str, ctx: impl Serialize) -> Result<String>
Render a string template directly without registering it.
This is useful for one-off template rendering where you don’t need to store the template for later use.
§Arguments
template- The template string to renderctx- The context data to pass to the template
§Returns
Returns the rendered template as a string.
§Errors
Returns an error if the template cannot be parsed or rendered.
§Example
use gba_pm::PromptManager;
use serde_json::json;
let manager = PromptManager::new();
let result = manager.render_str("Hello, {{ name }}!", json!({"name": "World"}))?;
assert_eq!(result, "Hello, World!");Sourcepub fn names(&self) -> Vec<&str>
pub fn names(&self) -> Vec<&str>
List all registered template names.
§Returns
Returns a vector of template names.
§Example
use gba_pm::PromptManager;
let mut manager = PromptManager::new();
manager.add("hello", "Hello!")?;
manager.add("goodbye", "Goodbye!")?;
let names = manager.names();
assert!(names.contains(&"hello"));
assert!(names.contains(&"goodbye"));