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§
Sourcefn required_verbosity(&self) -> VerbosityLevel
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
Sourcefn channel(&self) -> Channel
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
Sourcefn type_name(&self) -> &'static str
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
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".