@@ -10,7 +10,6 @@ use super::error::{Error, Result};
1010struct EventData {
1111 pub event_type : String ,
1212 pub data : String ,
13- pub comment : Option < String > ,
1413 pub id : Option < String > ,
1514 pub retry : Option < u64 > ,
1615}
@@ -45,10 +44,6 @@ impl TryFrom<EventData> for Option<SSE> {
4544 return Err ( Error :: InvalidEvent ) ;
4645 }
4746
48- if event_data. comment . is_some ( ) {
49- return Ok ( Some ( SSE :: Comment ( event_data. comment . unwrap ( ) ) ) ) ;
50- }
51-
5247 if event_data. data . is_empty ( ) {
5348 return Ok ( None ) ;
5449 }
@@ -217,7 +212,7 @@ impl EventParser {
217212 . get_or_insert_with ( || EventData :: new ( ) . with_id ( id. clone ( ) ) ) ;
218213
219214 if key == "comment" {
220- event_data . comment = Some ( value. to_string ( ) ) ;
215+ self . sse . push_back ( SSE :: Comment ( value. to_string ( ) ) ) ;
221216 } else if key == "event" {
222217 event_data. event_type = value. to_string ( )
223218 } else if key == "data" {
@@ -253,9 +248,7 @@ impl EventParser {
253248
254249 trace ! (
255250 "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)
259252 ) ;
260253
261254 if let Some ( event_data) = event_data {
@@ -520,9 +513,24 @@ mod tests {
520513 #[ test_case( b":hello\n " ; "with LF" ) ]
521514 #[ test_case( b":hello\r " ; "with CR" ) ]
522515 #[ 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 ] ) {
524517 let mut parser = EventParser :: new ( ) ;
525518 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+
526534 assert ! ( parser. get_event( ) . is_none( ) ) ;
527535 }
528536
0 commit comments