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