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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::time::Duration;

use crate::io::{AsyncCacheRead, AsyncReadCacheTimeoutExt, AsyncReadTimeoutExt, PipeError};
use ascii::AsciiString;
use tokio::io::{AsyncRead, AsyncReadExt};

pub trait PipeRead: AsyncRead + AsyncCacheRead {
    fn get_timeout(&self) -> Duration;
    fn set_timeout(&mut self, timeout: Duration);
    fn get_block_size(&self) -> usize;
    fn set_block_size(&mut self, block_size: usize);
}

impl<R: PipeRead> PipeReadExt for R {}

pub trait PipeReadExt: PipeRead {
    async fn recv(&mut self) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut data = vec![0u8; self.get_block_size()];
        let _ = self.read_timeout(&mut data, self.get_timeout()).await?;
        Ok(data)
    }

    async fn recv_all(&mut self) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut data = vec![];
        let _ = self.read_to_end(&mut data).await?;
        Ok(data)
    }

    async fn recv_all_timeout(
        &mut self,
        timeout: Duration,
        keep_data: bool,
    ) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut data = vec![];
        let _ = self
            .read_to_end_timeout(&mut data, timeout, !keep_data)
            .await?;
        Ok(data)
    }

    async fn recvn(&mut self, len: usize) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut data = vec![0u8; len];
        let _ = self.read_timeout(&mut data, self.get_timeout()).await?;
        Ok(data)
    }

    async fn recvn_fill(&mut self, len: usize) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut data = vec![0u8; len];
        let _ = self
            .read_fill_timeout(&mut data, self.get_timeout())
            .await?;
        Ok(data)
    }

    async fn recvn_exact(&mut self, len: usize) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut data = vec![0u8; len];
        let _ = self
            .read_exact_timeout(&mut data, self.get_timeout())
            .await?;
        Ok(data)
    }

    async fn recv_until<T: AsRef<[u8]>>(
        &mut self,
        delim: T,
        drop: bool,
    ) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut buf = Vec::new();
        let delim_len = delim.as_ref().len();
        self.read_until_timeout(delim, &mut buf, self.get_timeout())
            .await?;
        if drop {
            buf.drain(buf.len() - delim_len..);
        }
        Ok(buf)
    }

    async fn recv_until_regex(&mut self, pattern: &str, drop: bool) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut buf = Vec::new();
        let (_, match_len) = self
            .read_until_regex_timeout(pattern, &mut buf, self.get_timeout())?
            .await?;
        if drop {
            buf.drain(buf.len() - match_len..);
        }
        Ok(buf)
    }

    async fn recv_regex(&mut self, pattern: &str) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        let mut buf = Vec::new();
        let (_, match_len) = self
            .read_until_regex_timeout(pattern, &mut buf, self.get_timeout())?
            .await?;
        buf.drain(..buf.len() - match_len);
        Ok(buf)
    }

    async fn recv_until_regex_split(
        &mut self,
        pattern: &str,
    ) -> Result<(Vec<u8>, Vec<u8>), PipeError>
    where
        Self: Unpin,
    {
        let mut buf = Vec::new();
        let (_, match_len) = self
            .read_until_regex_timeout(pattern, &mut buf, self.get_timeout())?
            .await?;
        let (data, mch) = buf.split_at(buf.len() - match_len);
        Ok((data.to_vec(), mch.to_vec()))
    }

    async fn recv_line(&mut self) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        self.recv_until(b"\n", true).await
    }

    async fn recv_line_crlf(&mut self) -> Result<Vec<u8>, PipeError>
    where
        Self: Unpin,
    {
        self.recv_until(b"\r\n", true).await
    }

    async fn recv_utf8(&mut self) -> Result<String, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv().await?;
        Ok(String::from_utf8(data)?)
    }

    async fn recv_until_utf8<T: AsRef<[u8]>>(
        &mut self,
        delim: T,
        drop: bool,
    ) -> Result<String, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_until(delim, drop).await?;
        Ok(String::from_utf8(data)?)
    }

    async fn recv_until_regex_utf8(
        &mut self,
        pattern: &str,
        drop: bool,
    ) -> Result<String, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_until_regex(pattern, drop).await?;
        Ok(String::from_utf8(data)?)
    }

    async fn recv_regex_utf8(&mut self, pattern: &str) -> Result<String, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_regex(pattern).await?;
        Ok(String::from_utf8(data)?)
    }

    async fn recv_line_utf8(&mut self) -> Result<String, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_line().await?;
        Ok(String::from_utf8(data)?)
    }

    async fn recv_line_crlf_utf8(&mut self) -> Result<String, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_line_crlf().await?;
        Ok(String::from_utf8(data)?)
    }

    async fn recv_ascii(&mut self) -> Result<AsciiString, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv().await?;
        Ok(AsciiString::from_ascii(data)?)
    }

    async fn recv_until_ascii<T: AsRef<[u8]>>(
        &mut self,
        delim: T,
        drop: bool,
    ) -> Result<AsciiString, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_until(delim, drop).await?;
        Ok(AsciiString::from_ascii(data)?)
    }

    async fn recv_until_regex_ascii(
        &mut self,
        pattern: &str,
        drop: bool,
    ) -> Result<AsciiString, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_until_regex(pattern, drop).await?;
        Ok(AsciiString::from_ascii(data)?)
    }

    async fn recv_regex_ascii(&mut self, pattern: &str) -> Result<AsciiString, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_regex(pattern).await?;
        Ok(AsciiString::from_ascii(data)?)
    }

    async fn recv_line_ascii(&mut self) -> Result<AsciiString, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_line().await?;
        Ok(AsciiString::from_ascii(data)?)
    }

    async fn recv_line_crlf_ascii(&mut self) -> Result<AsciiString, PipeError>
    where
        Self: Unpin,
    {
        let data = self.recv_line_crlf().await?;
        Ok(AsciiString::from_ascii(data)?)
    }
}