Skip to main content

OutputMessage

Trait OutputMessage 

Source
pub trait OutputMessage {
    // Required methods
    fn format(&self, theme: &Theme) -> String;
    fn required_verbosity(&self) -> VerbosityLevel;
    fn channel(&self) -> Channel;
    fn type_name(&self) -> &'static str;
}
Expand description

Trait for output messages that can be written to user-facing channels

This trait enables extensibility following the Open/Closed Principle. Each message type encapsulates its own:

  • Formatting logic (how it appears to users)
  • Verbosity requirements (when it should be shown)
  • Channel routing (stdout vs stderr)

§Design Philosophy

By implementing this trait, message types become self-contained and can be added without modifying the UserOutput struct. This makes the system extensible - new message types can be defined in external modules.

§Examples

use torrust_tracker_deployer_lib::presentation::cli::views::{OutputMessage, Theme, VerbosityLevel, Channel};

struct CustomMessage {
    text: String,
}

impl OutputMessage for CustomMessage {
    fn format(&self, theme: &Theme) -> String {
        format!("🎉 {}", self.text)
    }

    fn required_verbosity(&self) -> VerbosityLevel {
        VerbosityLevel::Normal
    }

    fn channel(&self) -> Channel {
        Channel::Stderr
    }

    fn type_name(&self) -> &'static str {
        "CustomMessage"
    }
}

Required Methods§

Source

fn format(&self, theme: &Theme) -> String

Format this message using the given theme

This method defines how the message appears to users. It should incorporate theme symbols and any necessary formatting.

§Arguments
  • theme - The theme providing symbols for formatting
§Returns

A formatted string ready for display to users

Source

fn required_verbosity(&self) -> VerbosityLevel

Get the minimum verbosity level required to show this message

Messages are only displayed if the current verbosity level is greater than or equal to the required level.

§Returns

The minimum verbosity level needed to display this message

Source

fn channel(&self) -> Channel

Get the output channel for this message

Determines whether the message goes to stdout or stderr following Unix conventions.

§Returns

The channel (Stdout or Stderr) where this message should be written

Source

fn type_name(&self) -> &'static str

Get the type name of this message

Returns a human-readable type identifier for this message type. This is primarily used by formatter overrides (e.g., JSON formatter) to include type information in the output.

§Returns

A static string representing the message type name

Implementors§