kxio/src/print/printer.rs
Paul Campbell b6a236ab74
All checks were successful
Test / build (map[name:nightly]) (push) Successful in 4m6s
Test / build (map[name:stable]) (push) Successful in 6m1s
Release Please / Release-plz (push) Successful in 31s
feat(print): add print module
2024-12-12 09:02:21 +00:00

68 lines
1.9 KiB
Rust

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),
}
}
}