Skip to main content

Formatter

Trait Formatter 

Source
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§

Source

fn format(&self, result: &TestResult, use_bytes: bool) -> Result<(), Error>

Format a test result for output.

§Errors

Returns an error if output fails.

Source

fn format_list(&self, servers: &[Server]) -> Result<(), Error>

Format a list of servers for output.

§Errors

Returns an error if output fails.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl Formatter for OutputFormat

Allows using OutputFormat polymorphically through the trait.