use anyhow::{anyhow, Result};
use rust_embed::RustEmbed;
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
#[derive(RustEmbed)]
#[folder = "templates/"]
struct Templates;
pub async fn run(project_name: String, target_path: Option<PathBuf>, force: bool) -> Result<()> {
let normalized_name = normalize_project_name(&project_name)?;
let project_title = project_name_to_title(&project_name);
let base_path = target_path.unwrap_or_else(|| std::env::current_dir().unwrap());
let project_path = base_path.join(&normalized_name);
if project_path.exists() {
if !force {
if is_directory_empty(&project_path)? {
println!("📁 Directory exists but is empty, proceeding...");
} else {
return Err(anyhow!(
"Directory '{}' already exists and is not empty. Use --force to overwrite.",
project_path.display()
));
}
} else {
if !is_directory_empty(&project_path)? {
use crate::utils::backup::BackupManager;
let backup_manager = BackupManager::new()?;
backup_manager.backup_directory(&project_path, "project")?;
}
println!(
"⚠️ Overwriting existing directory: {}",
project_path.display()
);
}
}
println!("🚀 Creating new RustF project: {}", project_title);
println!("📁 Project directory: {}", project_path.display());
create_project_structure(&project_path)?;
let variables = create_template_variables(&normalized_name, &project_title);
generate_project_files(&project_path, &variables)?;
println!("✅ Project '{}' created successfully!", project_title);
println!();
println!("📋 Next steps:");
println!(" cd {}", normalized_name);
println!(" cargo run");
println!();
println!("🤖 AI-friendly features included:");
println!(" • Auto-discovery for controllers, models, middleware, and definitions");
println!(" • Definitions system for customizing framework behavior");
println!(" • Comprehensive README files in each directory");
println!(" • Schema-based model generation support");
println!(" • Template engine with layout support");
println!(" • Built-in middleware and security features");
Ok(())
}
fn normalize_project_name(name: &str) -> Result<String> {
if name.trim().is_empty() {
return Err(anyhow!("Project name cannot be empty"));
}
let normalized = name
.chars()
.map(|c| {
if c.is_alphanumeric() {
c.to_ascii_lowercase()
} else if c.is_whitespace() || c == '-' {
'_'
} else {
'_'
}
})
.collect::<String>()
.split('_')
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join("_");
if !normalized
.chars()
.next()
.unwrap_or('_')
.is_ascii_alphabetic()
{
return Err(anyhow!("Project name must start with a letter"));
}
if normalized.len() > 50 {
return Err(anyhow!("Project name is too long (max 50 characters)"));
}
Ok(normalized)
}
fn project_name_to_title(name: &str) -> String {
name.split(|c: char| c.is_whitespace() || c == '_' || c == '-')
.filter(|s| !s.is_empty())
.map(|word| {
let mut chars = word.chars();
match chars.next() {
None => String::new(),
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
}
})
.collect::<Vec<_>>()
.join(" ")
}
fn is_directory_empty(path: &Path) -> Result<bool> {
if !path.exists() {
return Ok(true);
}
if !path.is_dir() {
return Err(anyhow!(
"Path exists but is not a directory: {}",
path.display()
));
}
let entries = fs::read_dir(path)?;
Ok(entries.count() == 0)
}
fn create_project_structure(project_path: &Path) -> Result<()> {
let directories = [
"",
"src",
"src/controllers",
"src/middleware",
"src/modules",
"src/models",
"src/models/base",
"src/definitions",
"views",
"views/layouts",
"public",
"public/css",
"public/js",
"public/images",
"private",
"private/uploads",
"schemas",
];
for dir in &directories {
let dir_path = if dir.is_empty() {
project_path.to_path_buf()
} else {
project_path.join(dir)
};
fs::create_dir_all(&dir_path)?;
if matches!(*dir, "private/uploads" | "src/models/base") {
let gitkeep_path = dir_path.join(".gitkeep");
File::create(gitkeep_path)?.write_all(b"")?;
}
}
Ok(())
}
fn create_template_variables(project_name: &str, project_title: &str) -> HashMap<String, String> {
let mut variables = HashMap::new();
variables.insert("project_name".to_string(), project_name.to_string());
variables.insert("project_title".to_string(), project_title.to_string());
variables.insert(
"rustf_version".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
);
variables
}
fn generate_project_files(project_path: &Path, variables: &HashMap<String, String>) -> Result<()> {
let file_mappings = [
("project/Cargo.toml.template", "Cargo.toml"),
("project/config.toml.template", "config.toml"),
("project/config.dev.toml.template", "config.dev.toml"),
("project/main.rs.template", "src/main.rs"),
("project/README.md.template", "README.md"),
("project/gitignore.template", ".gitignore"),
(
"views/layouts/default.html.template",
"views/layouts/default.html",
),
(
"readmes/controllers_README.md.template",
"src/controllers/README.md",
),
(
"readmes/middleware_README.md.template",
"src/middleware/README.md",
),
(
"readmes/modules_README.md.template",
"src/modules/README.md",
),
("readmes/models_README.md.template", "src/models/README.md"),
(
"readmes/models_base_README.md.template",
"src/models/base/README.md",
),
(
"readmes/definitions_README.md.template",
"src/definitions/README.md",
),
("readmes/views_README.md.template", "views/README.md"),
("readmes/schemas_README.md.template", "schemas/README.md"),
(
"readmes/public_css_README.md.template",
"public/css/README.md",
),
(
"readmes/public_js_README.md.template",
"public/js/README.md",
),
(
"readmes/public_images_README.md.template",
"public/images/README.md",
),
("readmes/uploads_README.md.template", "private/uploads/README.md"),
("schemas/sessions.yaml.template", "schemas/sessions.yaml"),
(
"project/claude_skills/rustf/SKILL.md",
".claude/skills/rustf/SKILL.md",
),
];
for (template_path, output_path) in &file_mappings {
println!("📝 Creating {}", output_path);
let template_content = Templates::get(template_path)
.ok_or_else(|| anyhow!("Template not found: {}", template_path))?;
let content = std::str::from_utf8(template_content.data.as_ref())?;
let processed_content = process_template(content, variables)?;
let output_file_path = project_path.join(output_path);
if let Some(parent) = output_file_path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&output_file_path, processed_content)?;
}
create_sample_controller(project_path, variables)?;
create_sample_definition(project_path, variables)?;
Ok(())
}
fn process_template(content: &str, variables: &HashMap<String, String>) -> Result<String> {
let mut processed = content.to_string();
for (key, value) in variables {
let placeholder = format!("{{{{{}}}}}", key);
processed = processed.replace(&placeholder, value);
}
Ok(processed)
}
fn create_sample_controller(
project_path: &Path,
variables: &HashMap<String, String>,
) -> Result<()> {
let controller_content = format!(
r#"use rustf::prelude::*;
pub fn install() -> Vec<Route> {{
routes![
GET "/" => index,
GET "/about" => about,
]
}}
async fn index(ctx: &mut Context) -> Result<()> {{
let data = json!({{
"title": "Welcome to {}",
"message": "Your RustF application is running successfully!",
"features": [
"🚀 Auto-discovery for controllers, models, and middleware",
"🎨 Total.js template engine with layout support",
"🔐 Built-in session management and security features",
"📊 Schema-based model generation",
"🛡️ Comprehensive middleware system",
"🤖 AI-friendly documentation and patterns"
]
}});
ctx.view("/home/index", data)
}}
async fn about(ctx: &mut Context) -> Result<()> {{
let data = json!({{
"title": "About {}",
"description": "Built with the RustF framework - an AI-friendly MVC framework for Rust."
}});
ctx.view("/home/about", data)
}}
"#,
variables
.get("project_title")
.unwrap_or(&"RustF App".to_string()),
variables
.get("project_title")
.unwrap_or(&"RustF App".to_string())
);
let controller_path = project_path.join("src/controllers/home.rs");
fs::write(controller_path, controller_content)?;
println!("📝 Creating src/controllers/home.rs");
create_sample_views(project_path, variables)?;
Ok(())
}
fn create_sample_views(project_path: &Path, variables: &HashMap<String, String>) -> Result<()> {
let home_views_dir = project_path.join("views/home");
fs::create_dir_all(&home_views_dir)?;
let index_template = Templates::get("views/home/index.html.template")
.ok_or_else(|| anyhow!("Template not found: views/home/index.html.template"))?;
let index_content = std::str::from_utf8(index_template.data.as_ref())?;
let processed_index = process_template(index_content, variables)?;
let index_path = home_views_dir.join("index.html");
fs::write(index_path, processed_index)?;
println!("📝 Creating views/home/index.html");
let about_template = Templates::get("views/home/about.html.template")
.ok_or_else(|| anyhow!("Template not found: views/home/about.html.template"))?;
let about_content = std::str::from_utf8(about_template.data.as_ref())?;
let processed_about = process_template(about_content, variables)?;
let about_path = home_views_dir.join("about.html");
fs::write(about_path, processed_about)?;
println!("📝 Creating views/home/about.html");
Ok(())
}
fn create_sample_definition(
project_path: &Path,
_variables: &HashMap<String, String>,
) -> Result<()> {
let definition_content = format!(
r#"//! Application definitions
//!
//! Customises framework behaviour through the definitions system:
//! template helpers, validators, and the session-storage factory.
//!
//! NOTE: The definitions system covers helpers + validators + a session-
//! storage factory. It does NOT include a generic interceptor/provider
//! system — if you need one, reach for middleware, events, or shared
//! modules instead.
use rustf::definitions::{{Definitions, Helper, Validator}};
use rustf::prelude::*;
use serde_json::Value;
/// Install function called by auto-discovery.
pub fn install(defs: &mut Definitions) {{
register_helpers(defs);
// Uncomment to add more customisations:
// register_validators(defs);
// defs.set_session_storage_factory(my_session_factory);
}}
/// Register template helpers (callable from views).
fn register_helpers(defs: &mut Definitions) {{
// Closure-based helper — goes through HelperRegistry::register_fn,
// which takes (name, description, closure).
defs.helpers.register_fn(
"format_number",
"Format a number with thousands separators (e.g. 1234 -> 1,234)",
|args, _ctx| {{
if let Some(Value::Number(n)) = args.first() {{
if let Some(num) = n.as_u64() {{
let formatted = format!("{{}}", num)
.chars()
.rev()
.enumerate()
.map(|(i, c)| {{
if i > 0 && i % 3 == 0 {{
format!(",{{}}", c)
}} else {{
c.to_string()
}}
}})
.collect::<String>()
.chars()
.rev()
.collect::<String>();
return Ok(Value::String(formatted));
}}
}}
Ok(args.first().cloned().unwrap_or(Value::Null))
}},
);
// App-specific helper.
defs.helpers.register_fn(
"app_version",
"Return the current app version string",
|_args, _ctx| Ok(Value::String("1.0.0".to_string())),
);
}}
// Example: Custom validators (uncomment to use). Same register_fn shape:
// (name, description, closure) — goes through ValidatorRegistry::register_fn.
/*
fn register_validators(defs: &mut Definitions) {{
defs.validators.register_fn(
"strong_password",
"Require 8+ chars with upper, lower, and a digit",
|value, _options| {{
if let Some(password) = value.as_str() {{
if password.len() < 8 {{
return Err(rustf::Error::validation("Password must be at least 8 characters"));
}}
if !password.chars().any(|c| c.is_uppercase()) {{
return Err(rustf::Error::validation("Password must contain at least one uppercase letter"));
}}
if !password.chars().any(|c| c.is_lowercase()) {{
return Err(rustf::Error::validation("Password must contain at least one lowercase letter"));
}}
if !password.chars().any(|c| c.is_numeric()) {{
return Err(rustf::Error::validation("Password must contain at least one number"));
}}
}}
Ok(())
}},
);
}}
*/
"#
);
let definition_path = project_path.join("src/definitions/app.rs");
fs::write(definition_path, definition_content)?;
println!("📝 Creating src/definitions/app.rs");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_normalize_project_name() {
assert_eq!(normalize_project_name("MyApp").unwrap(), "myapp");
assert_eq!(normalize_project_name("my-app").unwrap(), "my_app");
assert_eq!(
normalize_project_name("My Great App").unwrap(),
"my_great_app"
);
assert_eq!(normalize_project_name("my__app").unwrap(), "my_app");
assert!(normalize_project_name("").is_err());
assert!(normalize_project_name("123app").is_err());
}
#[test]
fn test_scaffold_depends_on_the_published_framework() {
let template = Templates::get("project/Cargo.toml.template").unwrap();
let content = std::str::from_utf8(template.data.as_ref()).unwrap();
let mut variables = HashMap::new();
variables.insert("project_name".to_string(), "demo".to_string());
variables.insert(
"rustf_version".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
);
let manifest = process_template(content, &variables).unwrap();
let expected = format!("\nrustf = \"{}\"\n", env!("CARGO_PKG_VERSION"));
assert!(manifest.contains(&expected), "{manifest}");
assert!(!manifest.contains("{{"), "unreplaced variable: {manifest}");
}
#[test]
fn test_scaffold_sessions_schema_parses() {
let template = Templates::get("schemas/sessions.yaml.template")
.expect("sessions schema template is embedded");
let yaml = std::str::from_utf8(template.data.as_ref()).unwrap();
let tables = rustf_schema::parser::SchemaParser::parse_yaml(yaml)
.unwrap_or_else(|e| panic!("scaffold sessions.yaml does not parse: {}", e));
assert!(tables.contains_key("Sessions"));
}
#[test]
fn test_project_name_to_title() {
assert_eq!(project_name_to_title("my_app"), "My App");
assert_eq!(project_name_to_title("my-great-app"), "My Great App");
assert_eq!(project_name_to_title("MyApp"), "MyApp");
}
}