Skip to main content

FormatterOverride

Trait FormatterOverride 

Source
pub trait FormatterOverride: Send + Sync {
    // Required method
    fn transform(&self, formatted: &str, message: &dyn OutputMessage) -> String;
}
Expand description

Optional trait for post-processing message output

This allows transforming the standard message format without modifying individual message types. Use sparingly - prefer extending the message trait or using themes for most cases.

§When to Use

  • Machine-readable formats: JSON, XML, structured logs
  • Additional decoration: ANSI colors, markup codes
  • Output wrapping: Adding metadata, timestamps, process info

§When NOT to Use

  • Symbol changes: Use Theme instead
  • New message types: Implement OutputMessage trait instead
  • Channel routing changes: Define in message type’s channel() method

§Examples

use torrust_tracker_deployer_lib::presentation::cli::views::{FormatterOverride, OutputMessage};

struct JsonFormatter;

impl FormatterOverride for JsonFormatter {
    fn transform(&self, formatted: &str, message: &dyn OutputMessage) -> String {
        // Transform to JSON representation
        format!(r#"{{"content": "{}"}}"#, formatted.trim())
    }
}

Required Methods§

Source

fn transform(&self, formatted: &str, message: &dyn OutputMessage) -> String

Transform formatted message output

This method receives the already-formatted message (with theme applied) and the original message object for context. It should return the transformed output.

§Arguments
  • formatted - The message already formatted with theme
  • message - The original message object (for metadata/context)
§Returns

The transformed message string

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§