artichoke_backend/state/
output.rs

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::fmt;
use std::io::{self, Write};

use bstr::BString;

#[cfg(all(not(feature = "output-strategy-capture"), not(feature = "output-strategy-null")))]
pub type Strategy = Process;
#[cfg(all(feature = "output-strategy-capture", not(feature = "output-strategy-null")))]
pub type Strategy = Captured;
#[cfg(all(feature = "output-strategy-capture", feature = "output-strategy-null"))]
pub type Strategy = Null;

pub trait Output: Send + Sync + fmt::Debug {
    fn write_stdout<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()>;

    fn write_stderr<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()>;

    fn print<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        self.write_stdout(bytes)?;
        Ok(())
    }

    fn puts<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        self.write_stdout(bytes)?;
        self.write_stdout(b"\n")?;
        Ok(())
    }
}

#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Process {
    _private: (),
}

impl Process {
    /// Constructs a new, default `Process` output strategy.
    #[must_use]
    pub const fn new() -> Self {
        Self { _private: () }
    }
}

impl Output for Process {
    fn write_stdout<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        io::stdout().write_all(bytes.as_ref())
    }

    fn write_stderr<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        io::stderr().write_all(bytes.as_ref())
    }
}

#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Captured {
    stdout: BString,
    stderr: BString,
}

impl Captured {
    /// Constructs a new, default `Captured` output strategy.
    // This method cannot be const because of:
    // https://github.com/BurntSushi/bstr/issues/73
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    pub fn clear(&mut self) {
        self.stdout.clear();
        self.stderr.clear();
    }

    #[must_use]
    pub fn stdout(&self) -> &[u8] {
        self.stdout.as_slice()
    }

    #[must_use]
    pub fn stderr(&self) -> &[u8] {
        self.stderr.as_slice()
    }
}

impl Output for Captured {
    fn write_stdout<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        self.stdout.extend_from_slice(bytes.as_ref());
        Ok(())
    }

    fn write_stderr<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        self.stderr.extend_from_slice(bytes.as_ref());
        Ok(())
    }
}

impl<'a> Output for &'a mut Captured {
    fn write_stdout<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        self.stdout.extend_from_slice(bytes.as_ref());
        Ok(())
    }

    fn write_stderr<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        self.stderr.extend_from_slice(bytes.as_ref());
        Ok(())
    }
}

#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Null {
    _private: (),
}

impl Null {
    /// Constructs a new, default `Null` output strategy.
    #[must_use]
    pub const fn new() -> Self {
        Self { _private: () }
    }
}

impl Output for Null {
    fn write_stdout<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        drop(bytes);
        Ok(())
    }

    fn write_stderr<T: AsRef<[u8]>>(&mut self, bytes: T) -> io::Result<()> {
        drop(bytes);
        Ok(())
    }
}