pub trait Formatter: Send + Sync {
// Required methods
fn format(&self, result: &TestResult, use_bytes: bool) -> Result<(), Error>;
fn format_list(&self, servers: &[Server]) -> Result<(), Error>;
}Expand description
Trait for output formatting strategies.
Implement this trait to provide custom output formatters. This enables the Open-Closed Principle: new formatters can be added without modifying existing code that uses formatters.
§Example
use netspeed_cli::formatter::{Formatter, OutputFormat};
use netspeed_cli::types::{Server, TestResult};
use netspeed_cli::error::Error;
struct MyFormatter;
impl Formatter for MyFormatter {
fn format(&self, result: &TestResult, use_bytes: bool) -> Result<(), Error> {
println!("Custom: {:?}", result.ping);
Ok(())
}
fn format_list(&self, servers: &[Server]) -> Result<(), Error> {
println!("Servers: {}", servers.len());
Ok(())
}
}Required Methods§
Implementors§
impl Formatter for OutputFormat
Allows using OutputFormat polymorphically through the trait.