This document provides a detailed, unambiguous reference for all public API elements of the snapfire crate, suitable for developers or automated systems with no prior knowledge of the library.
-
Core Concept:
snapfireis a library that integrates theteratemplating engine with theactix-webframework. Its primary goal is to provide a simple API for rendering templates and to offer an optional, automatic live-reload feature for development. -
Primary Handle: The central struct for all operations is
snapfire::TeraWeb. An instance of this struct is created at application startup, holds all rendering configuration, and is shared with all Actix handlers. -
Configuration Entry Point: All configuration and initialization is performed through the
snapfire::TeraWebBuilder, which is created viasnapfire::TeraWeb::builder(). -
Rendering Pattern: The library uses a "renderable struct" pattern. Calling
TeraWeb::render()does not perform the render immediately. Instead, it returns an instance ofsnapfire::Template. ThisTemplatestruct is what you return from an Actix handler. Actix then usessnapfire's implementation of theRespondertrait onTemplateto perform the actual rendering asynchronously. -
Pervasive Types:
snapfire::Result<T>: All fallible operations in this library (likebuild()) return thisResulttype, which is an alias forstd::result::Result<T, snapfire::SnapFireError>.tera::Context: When rendering, users must provide a context object of this type, which comes from theteracrate.
The primary application state for SnapFire.
Public Methods
-
builder- Signature:
pub fn builder(templates_glob: &str) -> TeraWebBuilder - Description: Creates a new
TeraWebBuilderto configure and build aTeraWebinstance. This is the main entry point for using the library. - Parameters:
templates_glob:&str– A glob pattern used byterato discover template files. Example:"templates/**/*.html".
- Signature:
-
render- Signature:
pub fn render(&self, tpl: &str, context: tera::Context) -> Template - Description: Prepares a template for rendering by returning a
Templatestruct. This method is synchronous. - Parameters:
tpl:&str– The name of the template file to render, relative to the templates directory. Example:"pages/index.html".context:tera::Context– Thetera::Contextobject containing the variables for this specific render.
- Signature:
-
configure_routes- Availability: Only available when the
develfeature is enabled. - Signature:
#[cfg(feature = "devel")] pub fn configure_routes(&self, cfg: &mut actix_web::ServiceConfig) - Description: Configures Actix application routes required for
snapfire's development features (specifically, the live-reload WebSocket). In release builds (without thedevelfeature), this method is a no-op. - Parameters:
cfg:&mut actix_web::ServiceConfig– The mutable Actix service configuration that the WebSocket route will be added to.
- Availability: Only available when the
A builder used to configure and create a TeraWeb instance.
Public Methods
-
add_global- Signature:
pub fn add_global<S: Into<String>, T: serde::Serialize>(mut self, key: S, value: T) -> Self - Description: Adds a variable to the global context, making it available to all templates rendered by this instance.
- Parameters:
key:SwhereS: Into<String>– The name of the variable as it will be used in templates.value:TwhereT: serde::Serialize– Any value that implements theserde::Serializetrait.
- Signature:
-
configure_tera- Signature:
pub fn configure_tera<F>(mut self, configurator: F) -> Self where F: FnOnce(&mut tera::Tera) + 'static - Description: Provides a closure for advanced, direct manipulation of the
tera::Terainstance before it is finalized. Use this to register custom filters, functions, etc. - Parameters:
configurator:FwhereF: FnOnce(&mut tera::Tera) + 'static– A closure that receives a mutable reference to the newly createdtera::Terainstance.
- Signature:
-
watch_static- Availability: Only available when the
develfeature is enabled. - Signature:
#[cfg(feature = "devel")] pub fn watch_static(mut self, path: &str) -> Self - Description: Adds a static asset directory path for the live-reload watcher to monitor for changes.
- Parameters:
path:&str– The path to a directory to watch. Example:"static/css".
- Availability: Only available when the
-
ws_path- Availability: Only available when the
develfeature is enabled. - Signature:
#[cfg(feature = "devel")] pub fn ws_path(mut self, path: &str) -> Self - Description: Customizes the URL path for the live-reload WebSocket endpoint.
- Parameters:
path:&str– The URL path. Defaults to"/_snapfire/ws".
- Availability: Only available when the
-
auto_inject_script- Availability: Only available when the
develfeature is enabled. - Signature:
#[cfg(feature = "devel")] pub fn auto_inject_script(mut self, enabled: bool) -> Self - Description: Controls whether the live-reload JavaScript is automatically injected into HTML responses.
- Parameters:
enabled:bool– Set tofalseto disable injection. Defaults totrue.
- Availability: Only available when the
-
build- Signature:
pub fn build(self) -> Result<TeraWeb> - Description: Consumes the builder and attempts to create the final
TeraWebinstance. This can fail if the template glob is invalid or if the watcher fails to initialize.
- Signature:
A struct representing a render operation. It has no public fields or methods. Its primary interface is its implementation of actix_web::Responder.
An Actix middleware. It has no public fields or methods. It is instantiated via InjectSnapFireScript::default() and used with actix_web::App::wrap().
- Definition:
pub type Result<T, E = SnapFireError> = std::result::Result<T, E>; - Description: The standard
Resulttype used throughout thesnapfirecrate.
The unified error enum for all fallible operations in the library.
Enum Variants
Tera(tera::Error): Wraps an error from the underlyingteracrate.Io(std::io::Error): Wraps a standard I/O error.Serialization(String): An error occurred during context serialization.Watcher(notify::Error): (Only available when thedevelfeature is enabled). Wraps an error from thenotifyfile watcher crate.