use super::Command;
use crate::error::{CliError, CliResult};
use crate::output::OutputStyle;
use std::fs;
use std::path::{Path, PathBuf};
pub struct GenCommand {
pub spec_file: String,
}
impl GenCommand {
pub fn new(spec_file: String) -> Self {
Self { spec_file }
}
fn validate_spec(&self) -> CliResult<PathBuf> {
let spec_path = Path::new(&self.spec_file);
if !spec_path.exists() {
return Err(CliError::InvalidArgument {
message: format!("Spec file not found: {}", self.spec_file),
});
}
if !spec_path.is_file() {
return Err(CliError::InvalidArgument {
message: format!("Spec path is not a file: {}", self.spec_file),
});
}
match fs::metadata(spec_path) {
Ok(_) => Ok(spec_path.to_path_buf()),
Err(e) => Err(CliError::Io(e)),
}
}
fn load_spec(&self, spec_path: &Path) -> CliResult<String> {
match fs::read_to_string(spec_path) {
Ok(content) => {
if content.trim().is_empty() {
return Err(CliError::InvalidArgument {
message: "Spec file is empty".to_string(),
});
}
Ok(content)
}
Err(e) => Err(CliError::Io(e)),
}
}
fn generate_code(&self, _spec_content: &str) -> CliResult<()> {
Ok(())
}
}
impl Command for GenCommand {
fn execute(&self) -> CliResult<()> {
let style = OutputStyle::default();
let spec_path = self.validate_spec()?;
println!(
"{}",
style.info(&format!("Loading spec: {}", self.spec_file))
);
let spec_content = self.load_spec(&spec_path)?;
println!("{}", style.success("Spec loaded successfully"));
self.generate_code(&spec_content)?;
println!("{}", style.success("Code generation complete"));
Ok(())
}
}