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
use byteorder::{BigEndian, ReadBytesExt};
use std::fmt::{Display, Formatter};
use std::io::ErrorKind::UnexpectedEof;
use std::io::{Read};
use std::string::FromUtf8Error;
use time::OffsetDateTime;
use crate::client::shared::split_log_container_timestamp;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StreamLine {
pub kind: StreamKind,
pub text: String
}
impl StreamLine {
pub(crate) fn read<R: Read>(mut reader: R) -> Result<Option<StreamLine>, StreamLineReadError> {
let stream_type = match reader.read_u8() {
Err(other) => {
return match other.kind() {
UnexpectedEof => Ok(None),
_ => Err(other.into())
}
}
Ok(value) => {
value
}
};
reader.read_u8()?;
reader.read_u8()?;
reader.read_u8()?;
let text_length = reader.read_u32::<BigEndian>()?;
let mut text_bytes = vec![0u8; text_length as usize];
reader.read_exact(&mut text_bytes)?;
let line = StreamLine {
kind: StreamKind::from(stream_type),
text: String::from_utf8(text_bytes)?
};
Ok(Some(line))
}
pub(crate) fn read_all<R: Read>(mut reader: R) -> Result<Vec<StreamLine>, StreamLineReadError> {
let mut result: Vec<StreamLine> = Vec::new();
while let Some(line) = Self::read(&mut reader)? {
result.push(line);
}
Ok(result)
}
}
impl Display for StreamLine {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.text)
}
}
#[derive(Debug)]
pub enum StreamLineReadError {
Io(std::io::Error),
Utf8Conversion(FromUtf8Error)
}
impl StreamLineReadError {
pub fn error_message(&self) -> String {
match self {
Self::Io(e) => format!("Stream read IO error: {:?}", e),
Self::Utf8Conversion(e) => format!("Stream read UTF-8 conversion error: {:?}", e),
}
}
}
impl Display for StreamLineReadError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.error_message())
}
}
impl From<std::io::Error> for StreamLineReadError {
fn from(other: std::io::Error) -> Self {
StreamLineReadError::Io(other)
}
}
impl From<FromUtf8Error> for StreamLineReadError {
fn from(other: FromUtf8Error) -> Self {
StreamLineReadError::Utf8Conversion(other)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StreamKind {
StdIn,
StdOut,
StdErr,
Other(u8)
}
impl Display for StreamKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let message = match self {
Self::StdIn => "stdin".to_string(),
Self::StdOut => "stdout".to_string(),
Self::StdErr => "stderr".to_string(),
Self::Other(value) => format!("stream {}", value)
};
write!(f, "{}", message)
}
}
impl From<u8> for StreamKind {
fn from(value: u8) -> Self {
match value {
0 => Self::StdIn,
1 => Self::StdOut,
2 => Self::StdErr,
_ => Self::Other(value)
}
}
}
pub struct TsStreamLine {
pub timestamp: OffsetDateTime,
pub kind: StreamKind,
pub text: String,
}
impl TryFrom<&StreamLine> for TsStreamLine {
type Error = String;
fn try_from(value: &StreamLine) -> Result<TsStreamLine, Self::Error> {
let parts = split_log_container_timestamp(&value.text)?;
Ok(Self {
timestamp: parts.timestamp,
kind: value.kind,
text: parts.text.to_string()
})
}
}
#[cfg(test)]
mod test_stream_kind {
mod fixtures {
use std::fs::{File};
use std::io::Read;
use std::path::PathBuf;
pub fn path() -> PathBuf {
PathBuf::from(file!())
.parent()
.unwrap()
.join("test_fixtures")
}
pub fn binary<S: Into<String>>(name: S) -> Vec<u8> {
let file_name = path().join(name.into());
let mut result: Vec<u8> = Vec::new();
let mut f = File::open(file_name).unwrap();
f.read_to_end(&mut result).unwrap();
result
}
}
mod parses {
use std::io::Cursor;
use crate::model::{StreamKind, StreamLine};
#[test]
fn hello_world_line() {
let content: Vec<u8> = super::fixtures::binary("hello-world.bin");
let mut cursor = Cursor::new(content);
let line = StreamLine::read(&mut cursor)
.unwrap()
.unwrap();
assert_eq!(StreamKind::StdOut, line.kind);
assert_eq!("Hello, world.\n".to_string(), line.text);
let next_line = StreamLine::read(&mut cursor)
.unwrap();
assert!(!next_line.is_some());
}
#[test]
fn hello_world_lines() {
let content: Vec<u8> = super::fixtures::binary("hello-world.bin");
let mut cursor = Cursor::new(content);
let lines = StreamLine::read_all(&mut cursor)
.unwrap();
assert_eq!(1, lines.len());
let line0 = lines.get(0).unwrap();
assert_eq!(StreamKind::StdOut, line0.kind);
assert_eq!("Hello, world.\n".to_string(), line0.text);
}
}
}