pub struct PromptManager { /* private fields */ }Expand description
Manages prompt templates using minijinja for rendering.
Templates can be added individually via add_template
or loaded in bulk from a directory via load_from_dir.
Implementations§
Source§impl PromptManager
impl PromptManager
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty prompt manager with no templates loaded.
Use add_template or load_from_dir
to populate with templates.
Sourcepub fn with_builtin_templates() -> Result<Self, PromptError>
pub fn with_builtin_templates() -> Result<Self, PromptError>
Creates a new prompt manager pre-loaded with all built-in templates.
Built-in templates are embedded at compile time via include_str! and
are always available, even when installed via cargo install.
§Errors
Returns PromptError::InvalidTemplate if any built-in template has
invalid minijinja syntax.
§Examples
use coda_pm::PromptManager;
let pm = PromptManager::with_builtin_templates().unwrap();
assert!(pm.get_template("init/system").is_some());Sourcepub fn add_template(
&mut self,
template: PromptTemplate,
) -> Result<(), PromptError>
pub fn add_template( &mut self, template: PromptTemplate, ) -> Result<(), PromptError>
Registers a single template with the manager.
§Errors
Returns PromptError::InvalidTemplate if the template content has
invalid minijinja syntax.
Sourcepub fn load_from_dir(&mut self, dir: &Path) -> Result<(), PromptError>
pub fn load_from_dir(&mut self, dir: &Path) -> Result<(), PromptError>
Loads all .j2 templates from a directory recursively.
Template names are derived from relative paths (e.g., init/system
from init/system.j2). Existing templates with the same name are
overwritten.
§Errors
Returns PromptError::IoError if the directory cannot be read, or
PromptError::InvalidTemplate if a template has invalid syntax.
Sourcepub fn render<T: Serialize>(
&self,
name: &str,
ctx: T,
) -> Result<String, PromptError>
pub fn render<T: Serialize>( &self, name: &str, ctx: T, ) -> Result<String, PromptError>
Renders a named template with the given context data.
§Errors
Returns PromptError::TemplateNotFound if no template with the given
name exists, or PromptError::RenderError if rendering fails.
Sourcepub fn get_template(&self, name: &str) -> Option<&PromptTemplate>
pub fn get_template(&self, name: &str) -> Option<&PromptTemplate>
Returns a reference to the template with the given name, if it exists.
Sourcepub fn template_count(&self) -> usize
pub fn template_count(&self) -> usize
Returns the number of templates currently loaded.
§Examples
use coda_pm::{PromptManager, PromptTemplate};
let mut pm = PromptManager::new();
assert_eq!(pm.template_count(), 0);
pm.add_template(PromptTemplate::new("test", "Hello")).unwrap();
assert_eq!(pm.template_count(), 1);Trait Implementations§
Source§impl Clone for PromptManager
impl Clone for PromptManager
Source§fn clone(&self) -> PromptManager
fn clone(&self) -> PromptManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more