Struct ructe::Ructe

source ·
pub struct Ructe { /* private fields */ }
Expand description

The main build-time interface of ructe.

Your build script should create an instance of Ructe and use it to compile templates and possibly get access to the static files handler.

Ructe compiles your templates to rust code that should be compiled with your other rust code, so it needs to be called before compiling. Assuming you use cargo, it can be done like this:

First, specify a build script and ructe as a build dependency in Cargo.toml:

build = "src/build.rs"

[build-dependencies]
ructe = "0.6.0"

Then, in build.rs, compile all templates found in the templates directory and put the output where cargo tells it to:

use ructe::{Result, Ructe};

fn main() -> Result<()> {
    Ructe::from_env()?.compile_templates("templates")
}

And finally, include and use the generated code in your code. The file templates.rs will contain mod templates { ... }, so I just include it in my main.rs:

include!(concat!(env!("OUT_DIR"), "/templates.rs"));

When creating a Ructe it will create a file called templates.rs in your $OUT_DIR (which is normally created and specified by cargo). The methods will add content, and when the Ructe goes of of scope, the file will be completed.

Implementations§

source§

impl Ructe

source

pub fn from_env() -> Result<Ructe>

Create a Ructe instance suitable for a cargo-built project.

A file called templates.rs (and a directory called templates containing sub-modules) will be created in the directory that cargo specifies with the OUT_DIR environment variable.

source

pub fn new(outdir: PathBuf) -> Result<Ructe>

Create a ructe instance writing to a given directory.

The out_dir path is assumed to be a directory that exists and is writable. A file called templates.rs (and a directory called templates containing sub-modules) will be created in out_dir.

If you are using Ructe in a project that uses cargo, you should probably use Ructe::from_env instead.

source

pub fn compile_templates<P>(&mut self, indir: P) -> Result<()>where P: AsRef<Path>,

Create a templates module in outdir containing rust code for all templates found in indir.

If indir is a relative path, it should be relative to the main directory of your crate, i.e. the directory containing your Cargo.toml file.

Files with suffix .rs.html, .rs.svg, or .rs.xml are considered templates. A templete file called template.rs.html, template.rs.svg, etc, will result in a callable function named template_html, template_svg, etc. The template_html function will get a template alias for backwards compatibility, but that will be removed in a future release.

source

pub fn statics(&mut self) -> Result<StaticFiles>

Create a StaticFiles handler for this Ructe instance.

This will create a statics module inside the generated templates module.

Examples

This code goes into the build.rs:

let mut ructe = Ructe::from_env()?;
ructe.statics()?.add_files("static")?;
Ok(())

Assuming your project have a directory named static that contains e.g. a file called logo.svg and you have included the generated templates.rs, you can now use templates::statics::logo_png as a StaticFile in your project.

Trait Implementations§

source§

impl Drop for Ructe

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Ructe

§

impl Send for Ructe

§

impl Sync for Ructe

§

impl Unpin for Ructe

§

impl UnwindSafe for Ructe

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.