kxio 3.2.0

Provides injectable Filesystem and Network resources to make code more testable
Documentation
use super::{NullPrint, Print, StandardPrint, TestPrint};

/// A wrapper struct that can contain any implementation of the Print trait
#[derive(Clone)]
pub enum Printer {
    Standard(StandardPrint),
    Null(NullPrint),
    Test(TestPrint),
}

impl Printer {
    /// Creates a new Printer wrapping a StandardPrint implementation
    pub fn standard() -> Self {
        Self::Standard(StandardPrint)
    }

    /// Creates a new Printer wrapping a NullPrint implementation
    pub fn null() -> Self {
        Self::Null(NullPrint)
    }

    /// Creates a new Printer wrapping a TestPrint implementation
    pub fn test() -> Self {
        Self::Test(TestPrint::new())
    }

    /// Returns a reference to the wrapped TestPrint implementation if this Printer contains one
    pub fn as_test(&self) -> Option<&TestPrint> {
        match self {
            Self::Test(test_print) => Some(test_print),
            _ => None,
        }
    }
}

impl Print for Printer {
    fn print_fmt(&self, args: std::fmt::Arguments<'_>) {
        match self {
            Self::Standard(p) => p.print_fmt(args),
            Self::Null(p) => p.print_fmt(args),
            Self::Test(p) => p.print_fmt(args),
        }
    }

    fn println_fmt(&self, args: std::fmt::Arguments<'_>) {
        match self {
            Self::Standard(p) => p.println_fmt(args),
            Self::Null(p) => p.println_fmt(args),
            Self::Test(p) => p.println_fmt(args),
        }
    }

    fn eprint_fmt(&self, args: std::fmt::Arguments<'_>) {
        match self {
            Self::Standard(p) => p.eprint_fmt(args),
            Self::Null(p) => p.eprint_fmt(args),
            Self::Test(p) => p.eprint_fmt(args),
        }
    }

    fn eprintln_fmt(&self, args: std::fmt::Arguments<'_>) {
        match self {
            Self::Standard(p) => p.eprintln_fmt(args),
            Self::Null(p) => p.eprintln_fmt(args),
            Self::Test(p) => p.eprintln_fmt(args),
        }
    }
}