|
| 1 | +//! Create Schema Command Controller (Presentation Layer) |
| 2 | +//! |
| 3 | +//! Handles the presentation layer concerns for JSON Schema generation, |
| 4 | +//! including user output and progress reporting. |
| 5 | +
|
| 6 | +use std::cell::RefCell; |
| 7 | +use std::path::PathBuf; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use parking_lot::ReentrantMutex; |
| 11 | + |
| 12 | +use crate::application::command_handlers::create::schema::CreateSchemaCommandHandler; |
| 13 | +use crate::presentation::views::progress::ProgressReporter; |
| 14 | +use crate::presentation::views::UserOutput; |
| 15 | + |
| 16 | +use super::errors::CreateSchemaCommandError; |
| 17 | + |
| 18 | +/// Steps for schema generation workflow |
| 19 | +enum CreateSchemaStep { |
| 20 | + GenerateSchema, |
| 21 | +} |
| 22 | + |
| 23 | +impl CreateSchemaStep { |
| 24 | + fn description(&self) -> &str { |
| 25 | + match self { |
| 26 | + Self::GenerateSchema => "Generating JSON Schema", |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + fn count() -> usize { |
| 31 | + 1 |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Controller for create schema command |
| 36 | +/// |
| 37 | +/// Handles the presentation layer for JSON Schema generation, |
| 38 | +/// coordinating between the command handler and user output. |
| 39 | +pub struct CreateSchemaCommandController { |
| 40 | + progress: ProgressReporter, |
| 41 | +} |
| 42 | + |
| 43 | +impl CreateSchemaCommandController { |
| 44 | + /// Create a new schema generation command controller |
| 45 | + pub fn new(user_output: &Arc<ReentrantMutex<RefCell<UserOutput>>>) -> Self { |
| 46 | + let progress = ProgressReporter::new(user_output.clone(), CreateSchemaStep::count()); |
| 47 | + |
| 48 | + Self { progress } |
| 49 | + } |
| 50 | + |
| 51 | + /// Execute the schema generation command |
| 52 | + /// |
| 53 | + /// Generates JSON Schema and either writes to file or outputs to stdout. |
| 54 | + /// |
| 55 | + /// # Arguments |
| 56 | + /// |
| 57 | + /// * `output_path` - Optional path to write schema file. If `None`, outputs to stdout. |
| 58 | + /// |
| 59 | + /// # Returns |
| 60 | + /// |
| 61 | + /// Returns `Ok(())` on success, or error if generation or output fails. |
| 62 | + /// |
| 63 | + /// # Errors |
| 64 | + /// |
| 65 | + /// Returns error if: |
| 66 | + /// - Schema generation fails |
| 67 | + /// - File write fails (when path provided) |
| 68 | + /// - Stdout write fails (when no path provided) |
| 69 | + pub fn execute( |
| 70 | + &mut self, |
| 71 | + output_path: Option<&PathBuf>, |
| 72 | + ) -> Result<(), CreateSchemaCommandError> { |
| 73 | + self.progress |
| 74 | + .start_step(CreateSchemaStep::GenerateSchema.description())?; |
| 75 | + |
| 76 | + // Generate schema using application layer handler |
| 77 | + let schema = CreateSchemaCommandHandler::execute(output_path.cloned()) |
| 78 | + .map_err(|source| CreateSchemaCommandError::CommandFailed { source })?; |
| 79 | + |
| 80 | + // Handle output |
| 81 | + if output_path.is_some() { |
| 82 | + self.progress |
| 83 | + .complete_step(Some("Schema written to file successfully"))?; |
| 84 | + } else { |
| 85 | + // Output to stdout using ProgressReporter abstraction |
| 86 | + self.progress.complete_step(Some("Schema generated"))?; |
| 87 | + |
| 88 | + // Write schema to stdout (result data goes to stdout, not stderr) |
| 89 | + self.progress.result(&schema)?; |
| 90 | + } |
| 91 | + |
| 92 | + self.progress |
| 93 | + .complete("Schema generation completed successfully")?; |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use super::*; |
| 102 | + use crate::presentation::views::testing::test_user_output::TestUserOutput; |
| 103 | + use crate::presentation::views::VerbosityLevel; |
| 104 | + use tempfile::TempDir; |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn it_should_generate_schema_to_file_when_path_provided() { |
| 108 | + let temp_dir = TempDir::new().unwrap(); |
| 109 | + let schema_path = temp_dir.path().join("schema.json"); |
| 110 | + |
| 111 | + let (user_output, _capture, _capture_stderr) = |
| 112 | + TestUserOutput::new(VerbosityLevel::Normal).into_reentrant_wrapped(); |
| 113 | + let mut controller = CreateSchemaCommandController::new(&user_output); |
| 114 | + |
| 115 | + let result = controller.execute(Some(&schema_path)); |
| 116 | + assert!(result.is_ok()); |
| 117 | + |
| 118 | + // Verify file was created |
| 119 | + assert!(schema_path.exists()); |
| 120 | + |
| 121 | + // Verify file contains valid JSON schema |
| 122 | + let content = std::fs::read_to_string(&schema_path).unwrap(); |
| 123 | + assert!(content.contains("\"$schema\"")); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn it_should_complete_progress_when_generating_schema() { |
| 128 | + let (user_output, _capture, _capture_stderr) = |
| 129 | + TestUserOutput::new(VerbosityLevel::Normal).into_reentrant_wrapped(); |
| 130 | + let mut controller = CreateSchemaCommandController::new(&user_output); |
| 131 | + |
| 132 | + let temp_dir = TempDir::new().unwrap(); |
| 133 | + let schema_path = temp_dir.path().join("test.json"); |
| 134 | + |
| 135 | + let result = controller.execute(Some(&schema_path)); |
| 136 | + assert!(result.is_ok()); |
| 137 | + } |
| 138 | +} |
0 commit comments