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
impl TemplateService
Sourcepub fn with_template_dir(template_dir: impl Into<PathBuf>) -> Self
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
Sourcepub fn render(
&self,
template_path: &str,
variables: &HashMap<&str, &str>,
) -> Result<String>
pub fn render( &self, template_path: &str, variables: &HashMap<&str, &str>, ) -> Result<String>
Sourcepub async fn render_to_file(
&self,
template_path: &str,
output_path: impl AsRef<Path>,
variables: &HashMap<&str, &str>,
) -> Result<()>
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 directoryoutput_path- Destination file pathvariables- 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
Sourcepub fn template_exists(&self, template_path: &str) -> bool
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
Sourcepub fn create_vars(&self) -> TemplateVars
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_NAMESourcepub async fn generate_node(
&self,
node_name: &str,
node_type: &str,
output_dir: impl AsRef<Path>,
) -> Result<()>
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
Sourcepub async fn generate_driver(
&self,
driver_name: &str,
driver_type: &str,
output_dir: impl AsRef<Path>,
) -> Result<()>
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
Sourcepub async fn generate_type(
&self,
type_name: &str,
output_dir: impl AsRef<Path>,
) -> Result<()>
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
Sourcepub async fn generate_cargo_toml(
&self,
package_name: &str,
package_type: &str,
output_dir: impl AsRef<Path>,
) -> Result<()>
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 packagepackage_type- Type of package (“node”, “driver”, “type”)output_dir- Directory where Cargo.toml will be generated
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TemplateService
impl RefUnwindSafe for TemplateService
impl Send for TemplateService
impl Sync for TemplateService
impl Unpin for TemplateService
impl UnwindSafe for TemplateService
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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