use crate::addon::Addon;
use anyhow::Result;
use std::path::Path;
pub struct ValidationAddon;
impl Addon for ValidationAddon {
fn name(&self) -> &str {
"validation"
}
fn check_prerequisites(&self, project_root: &Path) -> Result<()> {
super::check_romance_project(project_root)
}
fn is_already_installed(&self, project_root: &Path) -> bool {
project_root
.join("backend/src/validation.rs")
.exists()
}
fn install(&self, project_root: &Path) -> Result<()> {
install_validation(project_root)
}
fn uninstall(&self, project_root: &Path) -> Result<()> {
use colored::Colorize;
println!("{}", "Uninstalling validation...".bold());
if super::remove_file_if_exists(&project_root.join("backend/src/validation.rs"))? {
println!(" {} backend/src/validation.rs", "delete".red());
}
super::remove_mod_from_main(project_root, "validation")?;
super::remove_feature_flag(project_root, "validation")?;
crate::ai_context::regenerate(project_root).ok();
println!();
println!("{}", "Validation uninstalled successfully.".green().bold());
Ok(())
}
}
fn install_validation(project_root: &Path) -> Result<()> {
use crate::template::TemplateEngine;
use crate::utils;
use colored::Colorize;
use tera::Context;
println!("{}", "Installing validation...".bold());
let engine = TemplateEngine::new()?;
let ctx = Context::new();
let content = engine.render("addon/validation/validate_middleware.rs.tera", &ctx)?;
utils::write_file(
&project_root.join("backend/src/validation.rs"),
&content,
)?;
println!(" {} backend/src/validation.rs", "create".green());
super::add_mod_to_main(project_root, "validation")?;
crate::generator::auth::insert_cargo_dependency(
&project_root.join("backend/Cargo.toml"),
&[
("validator", r#"{ version = "0.19", features = ["derive"] }"#),
],
)?;
super::update_feature_flag(project_root, "validation", true)?;
println!();
println!("{}", "Validation installed successfully!".green().bold());
println!(" Entity fields now support validation rules: name:string[min=3,max=100]");
Ok(())
}