ckb_resource

Struct Template

Source
pub struct Template(/* private fields */);
Expand description

A simple template which supports spec branches and variables.

The template is designed so that without expanding the template, it is still a valid TOML file.

§Spec Branches

A spec branches block replaces a line with a branch matching the given spec name.

The block starts with the line ending with # {{ (the leading space is required) and ends with a line # }}.

Between the start and end markers, every line is a branch starting with # SPEC => CONTENT, where SPEC is the branch spec name, and CONTENT is the text to be replaced for the spec. A special spec name _ acts as a wildcard which matches any spec name.

The spec name is required to render the template, see Template::new. The block including the whole starting line which ends with # {{ will be replaced by the branch CONTENT which SPEC is _ or equals to the given spec name.

In the CONTENT, variables are expanded and all the escape sequences \n are replaced by new lines.

use ckb_resource::{Template, TemplateContext};

let template = Template::new(
    r#"filter = "debug" # {{
# mainnet => filter = "error"
# _ => filter = "info"
# }}"#
        .to_string(),
);
let mainnet_result = template.render(&TemplateContext::new("mainnet", Vec::new()));
assert_eq!("filter = \"error\"\n", mainnet_result.unwrap());
let testnet_result = template.render(&TemplateContext::new("testnet", Vec::new()));
assert_eq!("filter = \"info\"\n", testnet_result.unwrap());

§Template Variables

Template variables are defined as key value dictionary in TemplateContext via TemplateContext::new or TemplateContext::insert.

Template uses variables by surrounding the variable names with curly brackets.

The variables expansions only happen inside the spec branches in the spec CONTENT. It is a trick to use a wildcard branch as in the following example.

use ckb_resource::{Template, TemplateContext};

let template = Template::new(
    r#"# # {{
# _ => listen_address = "127.0.0.1:{rpc_port}"
# }}"#
        .to_string(),
);
let text = template.render(&TemplateContext::new("dev", vec![("rpc_port", "18114")]));
assert_eq!("listen_address = \"127.0.0.1:18114\"\n", text.unwrap());

Implementations§

Source§

impl Template

Source

pub fn new(content: String) -> Self

Creates the template with the specified content.

Source§

impl Template

Source

pub fn render_to<W: Write>( &self, w: &mut W, context: &TemplateContext<'_>, ) -> Result<()>

Expands the template using the context and writes the result via the writer w.

§Errors

This method returns std::io::Error when it fails to write the chunks to the underlying writer.

Source

pub fn render(&self, context: &TemplateContext<'_>) -> Result<String>

Renders the template and returns the result as a string.

§Errors

This method returns std::io::Error when it fails to write the chunks to the underlying writer or it failed to convert the result text to UTF-8.

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> 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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V