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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! ITM output.

use crate::cli;
use anyhow::Result;
use std::{
    cell::RefCell,
    fs::{File, OpenOptions},
    io::{self, Stdout, Write},
};

/// Opened output.
pub struct Output<'cli> {
    /// Stimulus ports.
    pub ports: &'cli [u8],
    /// Output stream.
    pub output: RefCell<Stream>,
}

/// Output stream.
pub enum Stream {
    /// Standard output.
    Stdout(Stdout),
    /// File output.
    File(File),
}

impl<'cli> Output<'cli> {
    /// Opens all output streams.
    pub fn open_all(outputs: &'cli [cli::Output]) -> io::Result<Vec<Output<'cli>>> {
        outputs
            .iter()
            .map(|cli::Output { ports, path }| {
                match path {
                    Some(path) => OpenOptions::new().write(true).open(path).map(Stream::File),
                    None => Ok(Stream::Stdout(io::stdout())),
                }
                .map(|output| Self { ports, output: RefCell::new(output) })
            })
            .collect()
    }
}

impl Stream {
    /// Writes to the output stream.
    pub fn write(&mut self, data: &[u8]) -> Result<()> {
        match self {
            Self::Stdout(stdout) => write_stream(stdout, data),
            Self::File(file) => write_stream(file, data),
        }
    }
}

fn write_stream<T: Write>(stream: &mut T, data: &[u8]) -> Result<()> {
    stream.write_all(data)?;
    stream.flush()?;
    Ok(())
}