Skip to main content

ProjectScaffolder

Trait ProjectScaffolder 

Source
pub trait ProjectScaffolder {
    // Required methods
    fn scaffold(
        &self,
        project_dir: &Path,
        project_name: &str,
    ) -> Result<Vec<ScaffoldedFile>>;
    fn next_steps(&self, project_name: &str) -> Vec<String>;
}
Expand description

Language-agnostic trait for scaffolding new Spikard projects.

Implementations define how to create the initial project structure, configuration files, and example handlers for a specific language.

§Design Philosophy

  • Zero-cost abstraction: Trait implementations are compiled inline
  • Composability: Each method is independently callable for flexibility
  • Clarity: Method names and signatures are self-documenting
  • Extensibility: New methods can be added without breaking existing implementations

§Example

use spikard_cli::init::{ProjectScaffolder, ScaffoldedFile};
use std::path::PathBuf;

struct MyScaffolder;

impl ProjectScaffolder for MyScaffolder {
    fn scaffold(
        &self,
        project_dir: &std::path::Path,
        project_name: &str,
    ) -> anyhow::Result<Vec<ScaffoldedFile>> {
        let mut files = vec![];
        files.push(ScaffoldedFile::new(
            PathBuf::from("pyproject.toml"),
            format!("[project]\nname = \"{}\"", project_name),
        ));
        Ok(files)
    }

    fn next_steps(&self, _project_name: &str) -> Vec<String> {
        vec!["cd my_api".to_string()]
    }
}

Required Methods§

Source

fn scaffold( &self, project_dir: &Path, project_name: &str, ) -> Result<Vec<ScaffoldedFile>>

Scaffold a new project with language-idiomatic structure.

This method is responsible for generating all files needed for a new Spikard project in the target language. The returned files will be written to disk by the caller.

§Arguments
  • project_dir: The root directory where the project will be created
  • project_name: The name of the project (used for package names, module names, etc.)
§Returns

A vector of ScaffoldedFile instances representing all files to be created. The order of files is not guaranteed to be preserved on disk.

§Errors

Returns an error if scaffolding fails for any reason (e.g., invalid project name, I/O errors, or validation failures).

§Example
let files = scaffolder.scaffold(Path::new("."), "my_api")?;
// files might contain:
// - pyproject.toml
// - src/main.py
// - examples/basic_handler.py
// - tests/test_handlers.py
// - README.md
Source

fn next_steps(&self, project_name: &str) -> Vec<String>

Return next steps messages for the user after scaffolding completes.

These messages guide the user through initial setup steps like installing dependencies, running tests, or starting the server.

§Arguments
  • project_name: The name of the project that was scaffolded
§Returns

A vector of human-readable instruction strings that should be displayed to the user after successful project creation.

§Example
let steps = scaffolder.next_steps("my_api");
// steps might return:
// [
//   "cd my_api",
//   "python -m venv venv",
//   ". venv/bin/activate",
//   "pip install -e .",
//   "python -m pytest",
// ]

Implementors§