TemplateService

Struct TemplateService 

Source
pub struct TemplateService { /* private fields */ }
Expand description

Template service for code generation

§Examples

use mecha10_cli::services::TemplateService;
use std::collections::HashMap;

// Create service with default templates
let template_service = TemplateService::new();

// Render a template
let mut vars = HashMap::new();
vars.insert("name", "camera_driver");
let rendered = template_service.render("drivers/sensor.rs.template", &vars)?;

// Render to file
template_service.render_to_file(
    "drivers/sensor.rs.template",
    "output/camera_driver.rs",
    &vars
).await?;

Implementations§

Source§

impl TemplateService

Source

pub fn new() -> Self

Create a new template service with default templates

Source

pub fn with_template_dir(template_dir: impl Into<PathBuf>) -> Self

Create a template service with a custom template directory

§Arguments
  • template_dir - Path to directory containing templates
Source

pub fn render( &self, template_path: &str, variables: &HashMap<&str, &str>, ) -> Result<String>

Render a template with variables

§Arguments
  • template_path - Path to template file relative to template directory
  • variables - HashMap of variable names to values
§Errors

Returns an error if:

  • The template file doesn’t exist
  • The template file cannot be read
  • Variable substitution fails
Source

pub async fn render_to_file( &self, template_path: &str, output_path: impl AsRef<Path>, variables: &HashMap<&str, &str>, ) -> Result<()>

Render a template and write it to a file

§Arguments
  • template_path - Path to template file relative to template directory
  • output_path - Destination file path
  • variables - HashMap of variable names to values
§Errors

Returns an error if:

  • The template file doesn’t exist
  • The template file cannot be read
  • The output file cannot be written
Source

pub fn list_templates(&self, category: &str) -> Result<Vec<String>>

List available templates in a category

§Arguments
  • category - Template category (e.g., “drivers”, “nodes”, “types”)
§Returns

Vector of template names without the .template extension

Source

pub fn template_exists(&self, template_path: &str) -> bool

Check if a template exists

§Arguments
  • template_path - Path to template file relative to template directory
Source

pub fn create_vars(&self) -> TemplateVars

Create a template variables builder

Helper method to create a TemplateVars instance for building variable mappings with name transformations.

§Examples
let service = TemplateService::new();
let mut vars = service.create_vars();
vars.add_name("camera_driver");
// Now vars contains: name, PascalName, snake_name, UPPER_NAME
Source

pub async fn generate_node( &self, node_name: &str, node_type: &str, output_dir: impl AsRef<Path>, ) -> Result<()>

Generate a node file from template

Convenience method for generating node files with standard naming.

§Arguments
  • node_name - Name of the node (snake_case)
  • node_type - Type of node (“sensor”, “actuator”, “controller”, etc.)
  • output_dir - Directory where the node will be generated
Source

pub async fn generate_driver( &self, driver_name: &str, driver_type: &str, output_dir: impl AsRef<Path>, ) -> Result<()>

Generate a driver file from template

Convenience method for generating driver files with standard naming.

§Arguments
  • driver_name - Name of the driver (snake_case)
  • driver_type - Type of driver (“camera”, “motor”, “sensor”, etc.)
  • output_dir - Directory where the driver will be generated
Source

pub async fn generate_type( &self, type_name: &str, output_dir: impl AsRef<Path>, ) -> Result<()>

Generate a custom type file from template

Convenience method for generating type files with standard naming.

§Arguments
  • type_name - Name of the type (PascalCase or snake_case)
  • output_dir - Directory where the type will be generated
Source

pub async fn generate_cargo_toml( &self, package_name: &str, package_type: &str, output_dir: impl AsRef<Path>, ) -> Result<()>

Generate a Cargo.toml file for a package

§Arguments
  • package_name - Name of the package
  • package_type - Type of package (“node”, “driver”, “type”)
  • output_dir - Directory where Cargo.toml will be generated
Source

pub fn render_string( &self, template_content: &str, variables: &HashMap<&str, &str>, ) -> Result<String>

Render a string template directly (not from file)

§Arguments
  • template_content - Template content as string
  • variables - HashMap of variable names to values

Trait Implementations§

Source§

impl Default for TemplateService

Source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more