@@ -153,6 +153,14 @@ pub struct Notification {
153
153
pub params : serde_json:: Value ,
154
154
}
155
155
156
+ fn invalid_data ( error : impl Into < Box < dyn std:: error:: Error + Send + Sync > > ) -> io:: Error {
157
+ io:: Error :: new ( io:: ErrorKind :: InvalidData , error)
158
+ }
159
+
160
+ macro_rules! invalid_data {
161
+ ( $( $tt: tt) * ) => ( invalid_data( format!( $( $tt) * ) ) )
162
+ }
163
+
156
164
impl Message {
157
165
pub fn read ( r : & mut impl BufRead ) -> io:: Result < Option < Message > > {
158
166
Message :: _read ( r)
@@ -162,7 +170,14 @@ impl Message {
162
170
None => return Ok ( None ) ,
163
171
Some ( text) => text,
164
172
} ;
165
- let msg = serde_json:: from_str ( & text) ?;
173
+
174
+ let msg = match serde_json:: from_str ( & text) {
175
+ Ok ( msg) => msg,
176
+ Err ( e) => {
177
+ return Err ( invalid_data ! ( "malformed LSP payload: {:?}" , e) ) ;
178
+ }
179
+ } ;
180
+
166
181
Ok ( Some ( msg) )
167
182
}
168
183
pub fn write ( self , w : & mut impl Write ) -> io:: Result < ( ) > {
@@ -240,13 +255,6 @@ impl Notification {
240
255
}
241
256
242
257
fn read_msg_text ( inp : & mut dyn BufRead ) -> io:: Result < Option < String > > {
243
- fn invalid_data ( error : impl Into < Box < dyn std:: error:: Error + Send + Sync > > ) -> io:: Error {
244
- io:: Error :: new ( io:: ErrorKind :: InvalidData , error)
245
- }
246
- macro_rules! invalid_data {
247
- ( $( $tt: tt) * ) => ( invalid_data( format!( $( $tt) * ) ) )
248
- }
249
-
250
258
let mut size = None ;
251
259
let mut buf = String :: new ( ) ;
252
260
loop {
@@ -264,12 +272,12 @@ fn read_msg_text(inp: &mut dyn BufRead) -> io::Result<Option<String>> {
264
272
let mut parts = buf. splitn ( 2 , ": " ) ;
265
273
let header_name = parts. next ( ) . unwrap ( ) ;
266
274
let header_value =
267
- parts. next ( ) . ok_or_else ( || invalid_data ( format ! ( "malformed header: {:?}" , buf) ) ) ?;
275
+ parts. next ( ) . ok_or_else ( || invalid_data ! ( "malformed header: {:?}" , buf) ) ?;
268
276
if header_name. eq_ignore_ascii_case ( "Content-Length" ) {
269
277
size = Some ( header_value. parse :: < usize > ( ) . map_err ( invalid_data) ?) ;
270
278
}
271
279
}
272
- let size: usize = size. ok_or_else ( || invalid_data ( "no Content-Length" . to_owned ( ) ) ) ?;
280
+ let size: usize = size. ok_or_else ( || invalid_data ! ( "no Content-Length" ) ) ?;
273
281
let mut buf = buf. into_bytes ( ) ;
274
282
buf. resize ( size, 0 ) ;
275
283
inp. read_exact ( & mut buf) ?;
0 commit comments