mini_lang/
printer.rs

1use std::fmt;
2
3/// The printer to print evaluated value.
4pub trait Printer {
5    /// The error type that printer will provide.
6    type Err: std::error::Error + 'static;
7    /// Print integer value.
8    fn print(&mut self, v: i32) -> Result<(), Self::Err>;
9}
10
11/// The default printer implementation which prints to stdout.
12#[derive(Copy, Clone, Debug, PartialEq, Eq)]
13pub struct StdPrinter;
14
15#[derive(Copy, Clone, Debug, PartialEq, Eq)]
16pub struct EmptyError;
17
18impl fmt::Display for EmptyError {
19    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20        write!(f, "EmptyError")
21    }
22}
23
24impl std::error::Error for EmptyError {}
25
26impl Printer for StdPrinter {
27    type Err = EmptyError;
28    fn print(&mut self, v: i32) -> Result<(), Self::Err> {
29        println!("{}", v);
30        Ok(())
31    }
32}