pub struct Bicycle { /* private fields */ }
Implementations§
source§impl Bicycle
impl Bicycle
sourcepub fn new<'helper_name>(
escape_fn: EscapeFn,
helpers: impl IntoIterator<Item = (&'helper_name str, Box<dyn HelperDef + Send + Sync + 'static>)>,
base_data: JsonMap,
) -> Self
pub fn new<'helper_name>( escape_fn: EscapeFn, helpers: impl IntoIterator<Item = (&'helper_name str, Box<dyn HelperDef + Send + Sync + 'static>)>, base_data: JsonMap, ) -> Self
Creates a new Bicycle
instance, using the provided arguments to
configure the underlying handlebars::Handlebars
instance.
For info on helpers
, consult the handlebars
docs.
base_data
is data that will be available for all invocations of all methods on this instance.
§Examples
use cargo_mobile2::bicycle::{
handlebars::{handlebars_helper, HelperDef},
Bicycle, EscapeFn, JsonMap,
};
use std::collections::HashMap;
// An escape function that just replaces spaces with an angry emoji...
fn spaces_make_me_very_mad(raw: &str) -> String {
raw.replace(' ', "😡")
}
// A helper to reverse strings.
handlebars_helper!(reverse: |s: str|
// This doesn't correctly account for graphemes, so
// use a less naïve implementation for real apps.
s.chars().rev().collect::<String>()
);
// You could just as well use a [`Vec`] of tuples, or in this case,
// [`std::iter::once`].
let mut helpers = HashMap::<_, Box<dyn HelperDef + Send + Sync>>::new();
helpers.insert("reverse", Box::new(reverse));
let bike = Bicycle::new(
EscapeFn::Custom(&spaces_make_me_very_mad),
helpers,
JsonMap::default(),
);
sourcepub fn render(
&self,
template: &str,
insert_data: impl FnOnce(&mut JsonMap),
) -> Result<String, RenderingError>
pub fn render( &self, template: &str, insert_data: impl FnOnce(&mut JsonMap), ) -> Result<String, RenderingError>
Renders a template.
Use insert_data
to define any variables needed for the template.
§Examples
use cargo_mobile2::bicycle::Bicycle;
let bike = Bicycle::default();
let rendered = bike.render("Hello {{name}}!", |map| {
map.insert("name", "Shinji");
}).unwrap();
assert_eq!(rendered, "Hello Shinji!");
sourcepub fn process_action(
&self,
action: &Action,
insert_data: impl Fn(&mut JsonMap),
) -> Result<(), ProcessingError>
pub fn process_action( &self, action: &Action, insert_data: impl Fn(&mut JsonMap), ) -> Result<(), ProcessingError>
Executes an Action
.
Action::CreateDirectory
is executed with the same semantics asmkdir -p
: any missing parent directories are also created, and creation succeeds even if the directory already exists. Failure results in a [ProcessingError::DirectoryCreationFailed
].Action::CopyFile
is executed with the same semantics ascp
: if the destination file already exists, it will be overwritted with a copy of the source file. Failure results in a [ProcessingError::FileCopyFailed
].Action::WriteTemplate
is executed by reading the source file, rendering the contents as a template (usinginsert_data
to pass any required values to the underlyingBicycle::render
call), and then finally writing the result to the destination file. The destination file will be overwritten if it already exists. Failure for each step results in [ProcessingError::TemplateReadFailed
], [ProcessingError::TemplateRenderFailed
], and [ProcessingError::TemplateWriteFailed
], respectively.
sourcepub fn process_actions<'iter_item>(
&self,
actions: impl Iterator<Item = &'iter_item Action>,
insert_data: impl Fn(&mut JsonMap),
) -> Result<(), ProcessingError>
pub fn process_actions<'iter_item>( &self, actions: impl Iterator<Item = &'iter_item Action>, insert_data: impl Fn(&mut JsonMap), ) -> Result<(), ProcessingError>
Iterates over actions
, passing each item to Bicycle::process_action
.
sourcepub fn process(
&self,
src: impl AsRef<Path>,
dest: impl AsRef<Path>,
insert_data: impl Fn(&mut JsonMap),
) -> Result<(), ProcessingError>
pub fn process( &self, src: impl AsRef<Path>, dest: impl AsRef<Path>, insert_data: impl Fn(&mut JsonMap), ) -> Result<(), ProcessingError>
A convenience method that calls traverse
and passes the
output to Bicycle::process_actions
. Uses Bicycle::transform_path
as the transform_path
argument and DEFAULT_TEMPLATE_EXT
(“hbs”) as
the template_ext
argument to traverse
.
sourcepub fn filter_and_process(
&self,
src: impl AsRef<Path>,
dest: impl AsRef<Path>,
insert_data: impl Fn(&mut JsonMap),
filter: impl FnMut(&Action) -> bool,
) -> Result<(), ProcessingError>
pub fn filter_and_process( &self, src: impl AsRef<Path>, dest: impl AsRef<Path>, insert_data: impl Fn(&mut JsonMap), filter: impl FnMut(&Action) -> bool, ) -> Result<(), ProcessingError>
A convenience method that does the same work as Bicycle::process
,
but applies a filter predicate to each action prior to processing it.
sourcepub fn transform_path(
&self,
path: &Path,
insert_data: impl FnOnce(&mut JsonMap),
) -> Result<PathBuf, RenderingError>
pub fn transform_path( &self, path: &Path, insert_data: impl FnOnce(&mut JsonMap), ) -> Result<PathBuf, RenderingError>
Renders a path string itself as a template.
Intended to be used as the transform_path
argument to traverse
.