@@ -10,7 +10,6 @@ use super::error::{Error, Result};
10
10
struct EventData {
11
11
pub event_type : String ,
12
12
pub data : String ,
13
- pub comment : Option < String > ,
14
13
pub id : Option < String > ,
15
14
pub retry : Option < u64 > ,
16
15
}
@@ -45,10 +44,6 @@ impl TryFrom<EventData> for Option<SSE> {
45
44
return Err ( Error :: InvalidEvent ) ;
46
45
}
47
46
48
- if event_data. comment . is_some ( ) {
49
- return Ok ( Some ( SSE :: Comment ( event_data. comment . unwrap ( ) ) ) ) ;
50
- }
51
-
52
47
if event_data. data . is_empty ( ) {
53
48
return Ok ( None ) ;
54
49
}
@@ -217,7 +212,7 @@ impl EventParser {
217
212
. get_or_insert_with ( || EventData :: new ( ) . with_id ( id. clone ( ) ) ) ;
218
213
219
214
if key == "comment" {
220
- event_data . comment = Some ( value. to_string ( ) ) ;
215
+ self . sse . push_back ( SSE :: Comment ( value. to_string ( ) ) ) ;
221
216
} else if key == "event" {
222
217
event_data. event_type = value. to_string ( )
223
218
} else if key == "data" {
@@ -253,9 +248,7 @@ impl EventParser {
253
248
254
249
trace ! (
255
250
"seen empty line, event_data is {:?})" ,
256
- self . event_data
257
- . as_ref( )
258
- . map( |event_data| & event_data. event_type)
251
+ event_data. as_ref( ) . map( |event_data| & event_data. event_type)
259
252
) ;
260
253
261
254
if let Some ( event_data) = event_data {
@@ -520,9 +513,24 @@ mod tests {
520
513
#[ test_case( b":hello\n " ; "with LF" ) ]
521
514
#[ test_case( b":hello\r " ; "with CR" ) ]
522
515
#[ test_case( b":hello\r \n " ; "with CRLF" ) ]
523
- fn test_decode_chunks_comments_are_ignored ( chunk : & ' static [ u8 ] ) {
516
+ fn test_decode_chunks_comments_are_generated ( chunk : & ' static [ u8 ] ) {
524
517
let mut parser = EventParser :: new ( ) ;
525
518
assert ! ( parser. process_bytes( Bytes :: from( chunk) ) . is_ok( ) ) ;
519
+ assert ! ( parser. get_event( ) . is_some( ) ) ;
520
+ }
521
+
522
+ #[ test]
523
+ fn test_comment_is_separate_from_event ( ) {
524
+ let mut parser = EventParser :: new ( ) ;
525
+ let result = parser. process_bytes ( Bytes :: from ( ":comment\n data:hello\n \n " ) ) ;
526
+ assert ! ( result. is_ok( ) ) ;
527
+
528
+ let comment = parser. get_event ( ) ;
529
+ assert ! ( matches!( comment, Some ( SSE :: Comment ( _) ) ) ) ;
530
+
531
+ let event = parser. get_event ( ) ;
532
+ assert ! ( matches!( event, Some ( SSE :: Event ( _) ) ) ) ;
533
+
526
534
assert ! ( parser. get_event( ) . is_none( ) ) ;
527
535
}
528
536
0 commit comments