1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::fmt::{Error, Write};

use super::Context;

const PREALLOC_BUFFER: usize = 199;

impl Context {
    /// Start capturing printed content in a buffer.
    pub fn capture(&mut self) {
        self.out = Some(String::with_capacity(PREALLOC_BUFFER));
    }

    /// Capture `display` and `write` statement output in a buffer.
    pub fn capturing(mut self) -> Self {
        self.capture();
        self
    }

    /// Get the captured side-effect output.
    pub fn get_output(&mut self) -> Option<String> {
        self.out.take()
    }
}

impl Write for Context {
    fn write_str(&mut self, s: &str) -> Result<(), Error> {
        if let Some(ref mut st) = &mut self.out {
            write!(st, "{}", s)
        } else {
            print!("{}", s);
            Ok(())
        }
    }
}