microcad_lang/eval/
output.rs1pub trait Output {
6 fn print(&mut self, what: String) -> std::io::Result<()>;
8 fn output(&self) -> Option<String>;
10}
11
12pub struct Stdout;
14
15impl Stdout {
16 pub fn new() -> Box<Self> {
18 Box::new(Self)
19 }
20}
21
22impl Output for Stdout {
23 fn print(&mut self, what: String) -> std::io::Result<()> {
25 println!("{what}");
26 Ok(())
27 }
28 fn output(&self) -> Option<String> {
29 None
30 }
31}
32
33pub struct Capture {
35 buf: std::io::BufWriter<Vec<u8>>,
36}
37
38impl Capture {
39 pub fn new() -> Box<Self> {
41 Box::new(Self {
42 buf: std::io::BufWriter::new(Vec::new()),
43 })
44 }
45}
46
47impl Output for Capture {
48 fn print(&mut self, what: String) -> std::io::Result<()> {
49 use std::io::Write;
50 writeln!(self.buf, "{what}")
51 }
52 fn output(&self) -> Option<String> {
53 String::from_utf8(self.buf.buffer().into()).ok()
54 }
55}