@@ -6,7 +6,7 @@ use pin_project::pin_project;
6
6
7
7
use super :: error:: { Error , Result } ;
8
8
9
- #[ derive( Default , PartialEq ) ]
9
+ #[ derive( Default , PartialEq , Debug ) ]
10
10
struct EventData {
11
11
pub event_type : String ,
12
12
pub data : String ,
@@ -83,17 +83,19 @@ pub struct Event {
83
83
pub retry : Option < u64 > ,
84
84
}
85
85
86
- const LOGIFY_MAX_CHARS : usize = 100 ;
86
+ // const LOGIFY_MAX_CHARS: usize = 100;
87
87
fn logify ( bytes : & [ u8 ] ) -> & str {
88
88
let stringified = from_utf8 ( bytes) . unwrap_or ( "<bad utf8>" ) ;
89
- if stringified. len ( ) <= LOGIFY_MAX_CHARS {
90
- stringified
91
- } else {
92
- & stringified[ ..LOGIFY_MAX_CHARS - 1 ]
93
- }
89
+ stringified
90
+ // if stringified.len() <= LOGIFY_MAX_CHARS {
91
+ // stringified
92
+ // } else {
93
+ // &stringified[..LOGIFY_MAX_CHARS - 1]
94
+ // }
94
95
}
95
96
96
97
fn parse_field ( line : & [ u8 ] ) -> Result < Option < ( & str , & str ) > > {
98
+ trace ! ( "Parse field {:?}" , logify( line) ) ;
97
99
if line. is_empty ( ) {
98
100
return Err ( Error :: InvalidLine (
99
101
"should never try to parse an empty line (probably a bug)" . into ( ) ,
@@ -125,10 +127,12 @@ fn parse_field(line: &[u8]) -> Result<Option<(&str, &str)>> {
125
127
}
126
128
127
129
fn parse_key ( key : & [ u8 ] ) -> Result < & str > {
130
+ trace ! ( "Parse key {:?}" , logify( key) ) ;
128
131
from_utf8 ( key) . map_err ( |e| Error :: InvalidLine ( format ! ( "malformed key: {:?}" , e) ) )
129
132
}
130
133
131
134
fn parse_value ( value : & [ u8 ] ) -> Result < & str > {
135
+ trace ! ( "Parse value {:?}" , logify( value) ) ;
132
136
from_utf8 ( value) . map_err ( |e| Error :: InvalidLine ( format ! ( "malformed value: {:?}" , e) ) )
133
137
}
134
138
@@ -199,10 +203,18 @@ impl EventParser {
199
203
//
200
204
// Returns the event for dispatch if it is complete.
201
205
fn parse_complete_lines_into_event ( & mut self ) -> Result < ( ) > {
206
+ trace ! (
207
+ "Preparing to parse_complete_lines_into_event {:?} {:?}" ,
208
+ self . event_data,
209
+ self . last_event_id
210
+ ) ;
211
+
202
212
loop {
213
+ trace ! ( "loop" ) ;
203
214
let mut seen_empty_line = false ;
204
215
205
216
while let Some ( line) = self . complete_lines . pop_front ( ) {
217
+ trace ! ( "line {:?}" , line) ;
206
218
if line. is_empty ( ) && self . event_data . is_some ( ) {
207
219
seen_empty_line = true ;
208
220
break ;
@@ -211,6 +223,8 @@ impl EventParser {
211
223
}
212
224
213
225
if let Some ( ( key, value) ) = parse_field ( & line) ? {
226
+ trace ! ( "key {:}, value {:?}" , key, value) ;
227
+
214
228
let id = & self . last_event_id ;
215
229
let event_data = self
216
230
. event_data
@@ -259,6 +273,7 @@ impl EventParser {
259
273
) ;
260
274
261
275
if let Some ( event_data) = event_data {
276
+ trace ! ( "event_data {:?}" , event_data) ;
262
277
match Option :: < SSE > :: try_from ( event_data) {
263
278
Err ( e) => return Err ( e) ,
264
279
Ok ( None ) => ( ) ,
@@ -281,13 +296,15 @@ impl EventParser {
281
296
// incomplete lines from previous chunks.
282
297
fn decode_and_buffer_lines ( & mut self , chunk : Bytes ) {
283
298
let mut lines = chunk. split_inclusive ( |& b| b == b'\n' || b == b'\r' ) ;
299
+ trace ! ( "decode_and_buffer_lines {:?}" , lines) ;
284
300
// The first and last elements in this split are special. The spec requires lines to be
285
301
// terminated. But lines may span chunks, so:
286
302
// * the last line, if non-empty (i.e. if chunk didn't end with a line terminator),
287
303
// should be buffered as an incomplete line
288
304
// * the first line should be appended to the incomplete line, if any
289
305
290
306
if let Some ( incomplete_line) = self . incomplete_line . as_mut ( ) {
307
+ trace ! ( "incomplete_line {:?}" , incomplete_line) ;
291
308
if let Some ( line) = lines. next ( ) {
292
309
trace ! (
293
310
"extending line from previous chunk: {:?}+{:?}" ,
@@ -301,24 +318,31 @@ impl EventParser {
301
318
// terminator, but also where the entire line is a terminator.
302
319
match line. last ( ) . unwrap ( ) {
303
320
b'\r' => {
321
+ trace ! ( "\\ r branch" ) ;
304
322
incomplete_line. extend_from_slice ( & line[ ..line. len ( ) - 1 ] ) ;
305
323
let il = self . incomplete_line . take ( ) ;
306
324
self . complete_lines . push_back ( il. unwrap ( ) ) ;
307
325
self . last_char_was_cr = true ;
308
326
}
309
327
b'\n' => {
328
+ trace ! ( "\\ n branch" ) ;
310
329
incomplete_line. extend_from_slice ( & line[ ..line. len ( ) - 1 ] ) ;
311
330
let il = self . incomplete_line . take ( ) ;
312
331
self . complete_lines . push_back ( il. unwrap ( ) ) ;
313
332
}
314
- _ => incomplete_line. extend_from_slice ( line) ,
333
+ _ => {
334
+ trace ! ( "other branch" ) ;
335
+ incomplete_line. extend_from_slice ( line) ;
336
+ }
315
337
} ;
316
338
}
317
339
}
318
340
}
319
341
320
342
let mut lines = lines. peekable ( ) ;
343
+ trace ! ( "lines {:?}" , lines) ;
321
344
while let Some ( line) = lines. next ( ) {
345
+ trace ! ( "line {:?}" , line) ;
322
346
if let Some ( actually_complete_line) = self . incomplete_line . take ( ) {
323
347
// we saw the next line, so the previous one must have been complete after all
324
348
trace ! (
0 commit comments