cargo_mobile2::bicycle

Struct Bicycle

source
pub struct Bicycle { /* private fields */ }

Implementations§

source§

impl Bicycle

source

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(),
);
source

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

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 as mkdir -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 as cp: 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 (using insert_data to pass any required values to the underlying Bicycle::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.
source

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.

source

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.

source

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.

source

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.

Trait Implementations§

source§

impl Debug for Bicycle

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Bicycle

source§

fn default() -> Self

Returns the “default value” for a type. Read more

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

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.