1use std::io;
4
5#[derive(Debug)]
7pub enum OutputError {
8 Io(io::Error),
10}
11
12impl std::fmt::Display for OutputError {
13 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 match self {
15 OutputError::Io(error) => write!(formatter, "output error: {error}"),
16 }
17 }
18}
19
20impl std::error::Error for OutputError {}
21
22impl From<io::Error> for OutputError {
23 fn from(error: io::Error) -> Self {
24 OutputError::Io(error)
25 }
26}
27
28pub trait Output: Send + Sync {
30 fn write(&mut self, bytes: &[u8]) -> Result<(), OutputError>;
32
33 fn flush(&mut self) -> Result<(), OutputError> {
35 Ok(())
36 }
37}
38
39#[derive(Clone, Debug, Default)]
42pub struct BytesOutput {
43 bytes: Vec<u8>,
44}
45
46impl BytesOutput {
47 pub fn new() -> Self {
49 Self::default()
50 }
51
52 pub fn bytes(&self) -> &[u8] {
54 &self.bytes
55 }
56
57 pub fn into_bytes(self) -> Vec<u8> {
59 self.bytes
60 }
61}
62
63impl Output for BytesOutput {
64 fn write(&mut self, bytes: &[u8]) -> Result<(), OutputError> {
65 self.bytes.extend_from_slice(bytes);
66 Ok(())
67 }
68}
69
70#[derive(Clone, Copy, Debug, Default)]
72pub struct SinkOutput;
73
74impl SinkOutput {
75 pub fn new() -> Self {
77 Self
78 }
79}
80
81impl Output for SinkOutput {
82 fn write(&mut self, _bytes: &[u8]) -> Result<(), OutputError> {
83 Ok(())
84 }
85}