@@ -693,40 +693,65 @@ impl Iterator for TsGoLintMessageStream {
693693 }
694694}
695695
696+ enum TsGoLintMessageParseError {
697+ IncompleteData ,
698+ InvalidMessageType ( InvalidMessageType ) ,
699+ InvalidErrorPayload ( serde_json:: Error ) ,
700+ InvalidDiagnosticPayload ( serde_json:: Error ) ,
701+ }
702+
703+ impl std:: fmt:: Display for TsGoLintMessageParseError {
704+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
705+ match self {
706+ TsGoLintMessageParseError :: IncompleteData => write ! ( f, "Incomplete data" ) ,
707+ TsGoLintMessageParseError :: InvalidMessageType ( e) => write ! ( f, "{e}" ) ,
708+ TsGoLintMessageParseError :: InvalidErrorPayload ( e) => {
709+ write ! ( f, "Failed to parse tsgolint error payload: {e}" )
710+ }
711+ TsGoLintMessageParseError :: InvalidDiagnosticPayload ( e) => {
712+ write ! ( f, "Failed to parse tsgolint diagnostic payload: {e}" )
713+ }
714+ }
715+ }
716+ }
717+
696718/// Parses a single message from the binary tsgolint output.
697719// Messages are encoded as follows:
698720// | Payload Size (uint32 LE) - 4 bytes | Message Type (uint8) - 1 byte | Payload |
699- fn parse_single_message ( cursor : & mut std:: io:: Cursor < & [ u8 ] > ) -> Result < TsGoLintMessage , String > {
721+ fn parse_single_message (
722+ cursor : & mut std:: io:: Cursor < & [ u8 ] > ,
723+ ) -> Result < TsGoLintMessage , TsGoLintMessageParseError > {
700724 let mut size_bytes = [ 0u8 ; 4 ] ;
701725 if cursor. read_exact ( & mut size_bytes) . is_err ( ) {
702- return Err ( "Failed to read size bytes" . to_string ( ) ) ;
726+ return Err ( TsGoLintMessageParseError :: IncompleteData ) ;
703727 }
704728 let size = u32:: from_le_bytes ( size_bytes) as usize ;
705729
706730 let mut message_type_byte = [ 0u8 ; 1 ] ;
707731 if cursor. read_exact ( & mut message_type_byte) . is_err ( ) {
708- return Err ( "Failed to read message type byte" . to_string ( ) ) ;
732+ return Err ( TsGoLintMessageParseError :: IncompleteData ) ;
709733 }
734+
710735 let message_type = MessageType :: try_from ( message_type_byte[ 0 ] )
711- . map_err ( |_| "Invalid message type byte" . to_string ( ) ) ?;
736+ . map_err ( TsGoLintMessageParseError :: InvalidMessageType ) ?;
712737
713738 let mut payload_bytes = vec ! [ 0u8 ; size] ;
714739 if cursor. read_exact ( & mut payload_bytes) . is_err ( ) {
715- return Err ( "Failed to read payload bytes" . to_string ( ) ) ;
740+ return Err ( TsGoLintMessageParseError :: IncompleteData ) ;
716741 }
717742 let payload_str = String :: from_utf8_lossy ( & payload_bytes) ;
718743
719744 match message_type {
720745 MessageType :: Error => {
721746 let error_payload = serde_json:: from_str :: < TsGoLintErrorPayload > ( & payload_str)
722- . map_err ( |e| format ! ( "Failed to parse tsgolint error payload: {e}" ) ) ?;
747+ . map_err ( TsGoLintMessageParseError :: InvalidErrorPayload ) ?;
723748
724749 Ok ( TsGoLintMessage :: Error ( TsGoLintError { error : error_payload. error } ) )
725750 }
726751 MessageType :: Diagnostic => {
727752 let diagnostic_payload =
728753 serde_json:: from_str :: < TsGoLintDiagnosticPayload > ( & payload_str)
729- . map_err ( |e| format ! ( "Failed to parse tsgolint diagnostic payload: {e}" ) ) ?;
754+ . map_err ( TsGoLintMessageParseError :: InvalidDiagnosticPayload ) ?;
730755
731756 Ok ( TsGoLintMessage :: Diagnostic ( TsGoLintDiagnostic {
732757 range : diagnostic_payload. range ,
0 commit comments