Skip to main content

PromptManager

Struct PromptManager 

Source
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

Source

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.

Source

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());
Source

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.

Source

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.

Source

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.

Source

pub fn get_template(&self, name: &str) -> Option<&PromptTemplate>

Returns a reference to the template with the given name, if it exists.

Source

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

Source§

fn clone(&self) -> PromptManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PromptManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PromptManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.