use anyhow::{bail, Result};
use colored::*;
use std::fs;
use std::path::Path;
use crate::common::{self, ProjectType};
const GUARDRAILS_TEMPLATE: &str = include_str!("../templates/guardrails_template.rs");
const SECURE_REQUIREMENTS_TEMPLATE: &str = include_str!("../templates/secure_requirements.txt");
pub fn run_init(language: &str) -> Result<()> {
match language {
"rust" => init_rust(),
"python" => init_python(),
"auto" => {
println!("{}", "Detecting project type...".cyan());
match common::detect_project_type() {
ProjectType::Rust => init_rust(),
ProjectType::Python => init_python(),
ProjectType::Unknown => bail!("Could not auto-detect project type. Please specify 'rust' or 'python'."),
}
}
_ => bail!(
"Unknown language: '{}'. Supported: rust, python, auto",
language
),
}
}
fn init_rust() -> Result<()> {
let target_path = "src/guardrails.rs";
if Path::new(target_path).exists() {
println!(
"{} '{}' already exists. Skipping to avoid overwriting.",
"Warning:".yellow().bold(),
target_path
);
return Ok(());
}
if !Path::new("src").exists() {
fs::create_dir_all("src")?;
}
fs::write(target_path, GUARDRAILS_TEMPLATE)?;
println!("{} Generated '{}'", "â".green().bold(), target_path);
println!("");
println!(" {} Add 'regex = \"1.10\"' to your Cargo.toml", "Next steps:".cyan().bold());
println!(" Then use it in your code: 'mod guardrails; use guardrails::validate_input;'");
Ok(())
}
fn init_python() -> Result<()> {
let target_path = "secure_requirements.txt";
if Path::new(target_path).exists() {
println!(
"{} '{}' already exists. Skipping to avoid overwriting.",
"Warning:".yellow().bold(),
target_path
);
return Ok(());
}
fs::write(target_path, SECURE_REQUIREMENTS_TEMPLATE)?;
println!("{} Generated '{}'", "â".green().bold(), target_path);
println!("");
println!(" {} Append to requirements.txt:", "Next steps:".cyan().bold());
println!(" 'cat secure_requirements.txt >> requirements.txt && pip install -r requirements.txt'");
Ok(())
}