ricecoder_cli/commands/
gen.rs1use super::Command;
5use crate::error::{CliError, CliResult};
6use crate::output::OutputStyle;
7use std::fs;
8use std::path::{Path, PathBuf};
9
10pub struct GenCommand {
12 pub spec_file: String,
13}
14
15impl GenCommand {
16 pub fn new(spec_file: String) -> Self {
17 Self { spec_file }
18 }
19
20 fn validate_spec(&self) -> CliResult<PathBuf> {
22 let spec_path = Path::new(&self.spec_file);
23
24 if !spec_path.exists() {
26 return Err(CliError::InvalidArgument {
27 message: format!("Spec file not found: {}", self.spec_file),
28 });
29 }
30
31 if !spec_path.is_file() {
33 return Err(CliError::InvalidArgument {
34 message: format!("Spec path is not a file: {}", self.spec_file),
35 });
36 }
37
38 match fs::metadata(spec_path) {
40 Ok(_) => Ok(spec_path.to_path_buf()),
41 Err(e) => Err(CliError::Io(e)),
42 }
43 }
44
45 fn load_spec(&self, spec_path: &Path) -> CliResult<String> {
47 match fs::read_to_string(spec_path) {
48 Ok(content) => {
49 if content.trim().is_empty() {
51 return Err(CliError::InvalidArgument {
52 message: "Spec file is empty".to_string(),
53 });
54 }
55 Ok(content)
56 }
57 Err(e) => Err(CliError::Io(e)),
58 }
59 }
60
61 fn generate_code(&self, _spec_content: &str) -> CliResult<()> {
63 Ok(())
66 }
67}
68
69impl Command for GenCommand {
70 fn execute(&self) -> CliResult<()> {
71 let style = OutputStyle::default();
72
73 let spec_path = self.validate_spec()?;
75 println!(
76 "{}",
77 style.info(&format!("Loading spec: {}", self.spec_file))
78 );
79
80 let spec_content = self.load_spec(&spec_path)?;
82 println!("{}", style.success("Spec loaded successfully"));
83
84 self.generate_code(&spec_content)?;
86 println!("{}", style.success("Code generation complete"));
87
88 Ok(())
89 }
90}