use clap::Subcommand;
use crate::{TuskResult, parse, serialize};
use std::fs;
use std::path::Path;
#[derive(Subcommand)]
pub enum DevCommand {
Serve { port: Option<u16> },
Compile { file: String },
Optimize { file: String },
}
pub fn run(cmd: DevCommand) -> TuskResult<()> {
match cmd {
DevCommand::Serve { port } => {
println!("[serve {:?}] stub", port);
Ok(())
}
DevCommand::Compile { file } => {
dev_compile(&file)?;
Ok(())
}
DevCommand::Optimize { file } => {
println!("[optimize {}] stub", file);
Ok(())
}
}
}
fn dev_compile(file: &str) -> TuskResult<()> {
let content = fs::read_to_string(file)
.map_err(|e| {
eprintln!("❌ File not found: {}", file);
std::process::exit(3); })?;
match parse(&content) {
Ok(config) => {
let optimized = serialize(&config)?;
let input_path = Path::new(file);
let stem = input_path.file_stem().unwrap_or_default();
let output_file = format!("{}.compiled.tsk", stem.to_string_lossy());
fs::write(&output_file, optimized)
.map_err(|e| {
eprintln!("❌ Failed to write output file: {}", e);
std::process::exit(4); })?;
println!("✅ Successfully compiled '{}' to '{}'", file, output_file);
Ok(())
}
Err(e) => {
eprintln!("❌ Compilation failed: {}", e);
std::process::exit(1); }
}
}