pub struct InitEngine;Expand description
Orchestrates the project initialization workflow.
§Overview
InitEngine is the main entry point for the spikard init command.
It handles:
- Validation: Ensures project name and paths are valid
- Scaffolder Selection: Routes to the correct language scaffolder
- File Creation: Writes scaffolded files to disk
- 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
impl InitEngine
Sourcepub fn execute(request: InitRequest) -> Result<InitResponse>
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: AnInitRequestspecifying 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 languageDirectoryAlreadyExists: If the project directory already existsSchemaPathNotFound: If a schema path was provided but doesn’t existScaffoldingFailed: 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)?;Sourcepub fn validate_project_name(
project_name: &str,
language: TargetLanguage,
) -> Result<()>
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 validatelanguage: The target language whose rules apply
§Returns
Returns Ok(()) if the name is valid, otherwise returns a descriptive error.