Skip to main content

InitEngine

Struct InitEngine 

Source
pub struct InitEngine;
Expand description

Orchestrates the project initialization workflow.

§Overview

InitEngine is the main entry point for the spikard init command. It handles:

  1. Validation: Ensures project name and paths are valid
  2. Scaffolder Selection: Routes to the correct language scaffolder
  3. File Creation: Writes scaffolded files to disk
  4. Guidance: Returns user-friendly next steps

§Validation Rules

  • Project Name: Must be a valid identifier in the target language
  • Directory: The project directory must not already exist
  • Schema Path: If provided, must exist and be readable

§Architecture

The engine does not generate code itself; instead, it delegates to language-specific ProjectScaffolder implementations. This keeps the engine lightweight and allows independent evolution of language support.

§Example

use spikard_cli::init::{InitEngine, InitRequest};
use spikard_cli::codegen::TargetLanguage;
use std::path::PathBuf;

let request = InitRequest {
    project_name: "my_api".to_string(),
    language: TargetLanguage::Python,
    project_dir: PathBuf::from("."),
    schema_path: None,
};

match InitEngine::execute(request) {
    Ok(response) => {
        println!("Successfully created {} files", response.files_created.len());
        for step in response.next_steps {
            println!("  → {}", step);
        }
    }
    Err(e) => eprintln!("Initialization failed: {}", e),
}

Implementations§

Source§

impl InitEngine

Source

pub fn execute(request: InitRequest) -> Result<InitResponse>

Execute the project initialization workflow.

This method is the primary entry point for initializing a new Spikard project. It validates the request, selects the appropriate scaffolder, generates files, writes them to disk, and returns guidance for next steps.

§Arguments
  • request: An InitRequest specifying project name, language, and location
§Returns

On success, returns an InitResponse with created file paths and next steps. On failure, returns an error detailing what went wrong.

§Errors
  • InvalidProjectName: If the project name is not valid for the target language
  • DirectoryAlreadyExists: If the project directory already exists
  • SchemaPathNotFound: If a schema path was provided but doesn’t exist
  • ScaffoldingFailed: If file creation or writing fails
§Side Effects

This method creates the project directory and all scaffolded files on disk. If any error occurs after directory creation, the directory is left as-is for the user to clean up (to avoid accidental data loss).

§Example
let request = InitRequest {
    project_name: "my_api".to_string(),
    language: TargetLanguage::Python,
    project_dir: PathBuf::from("."),
    schema_path: None,
};

let response = InitEngine::execute(request)?;
Source

pub fn validate_project_name( project_name: &str, language: TargetLanguage, ) -> Result<()>

Validate that a project name is appropriate for the target language.

Naming rules vary by language:

  • Python: Lowercase, alphanumeric + underscore, no leading digit
  • TypeScript: Must be valid npm package name (lowercase, hyphen OK)
  • Ruby: Snake_case, no leading digit
  • Rust: Snake_case, alphanumeric + underscore, no leading digit
  • PHP: Alphanumeric + underscore, no leading digit
§Arguments
  • project_name: The name to validate
  • language: The target language whose rules apply
§Returns

Returns Ok(()) if the name is valid, otherwise returns a descriptive error.

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> 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
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<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