Skip to main content

PromptManager

Struct PromptManager 

Source
#[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>

Source

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

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")?;
Source

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 under
  • content - 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 }}!")?;
Source

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 render
  • ctx - 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!");
Source

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 render
  • ctx - 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!");
Source

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"));

Trait Implementations§

Source§

impl<'a> Debug for PromptManager<'a>

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§

§

impl<'a> !Freeze for PromptManager<'a>

§

impl<'a> !RefUnwindSafe for PromptManager<'a>

§

impl<'a> Send for PromptManager<'a>

§

impl<'a> Sync for PromptManager<'a>

§

impl<'a> Unpin for PromptManager<'a>

§

impl<'a> !UnwindSafe for PromptManager<'a>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more