-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson.rs
More file actions
44 lines (40 loc) · 1.52 KB
/
Copy pathjson.rs
File metadata and controls
44 lines (40 loc) · 1.52 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
//! JSON formatter implementation
use super::super::{FormatterOverride, OutputMessage};
// ============================================================================
// Formatter Override Implementations
// ============================================================================
/// JSON formatter for machine-readable output
///
/// Transforms messages into JSON objects with metadata including:
/// - Message type (for programmatic filtering)
/// - Channel (stdout/stderr)
/// - Content (the formatted message)
/// - Timestamp (ISO 8601 format)
///
/// # Examples
///
/// ```rust,ignore
/// use torrust_tracker_deployer_lib::presentation::user_output::{JsonFormatter, UserOutput, VerbosityLevel};
///
/// let formatter = JsonFormatter;
/// let mut output = UserOutput::with_formatter_override(
/// VerbosityLevel::Normal,
/// Box::new(formatter)
/// );
///
/// output.progress("Starting process");
/// // Output: {"type":"ProgressMessage","channel":"Stderr","content":"⏳ Starting process","timestamp":"2025-11-04T12:34:56Z"}
/// ```
pub struct JsonFormatter;
impl FormatterOverride for JsonFormatter {
fn transform(&self, formatted: &str, message: &dyn OutputMessage) -> String {
let json = serde_json::json!({
"type": message.type_name(),
"channel": format!("{:?}", message.channel()),
"content": formatted.trim(), // Remove trailing newlines for cleaner JSON
"timestamp": chrono::Utc::now().to_rfc3339(),
})
.to_string();
format!("{json}\n")
}
}