From 31a6adc52d4357d8801225084bd993623aad0eb9 Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Sat, 26 Dec 2020 15:23:32 +0200 Subject: [PATCH 1/2] Handle `Ok` and `Err` with separate paths --- src/lib.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b9b3d35..be5c30d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,12 +110,18 @@ impl Connection { self.sender.send(resp.into()).unwrap(); } } - msg => { + Ok(msg) => { return Err(ProtocolError(format!( "expected initialize request, got {:?}", msg ))) } + Err(e) => { + return Err(ProtocolError(format!( + "expected initialize request, got error: {}", + e + ))) + } }; } } @@ -130,10 +136,16 @@ impl Connection { self.sender.send(resp.into()).unwrap(); match &self.receiver.recv() { Ok(Message::Notification(n)) if n.is_initialized() => (), - m => { + Ok(msg) => { + return Err(ProtocolError(format!( + "expected Message::Notification, got: {:?}", + msg, + ))) + }, + Err(e) => { return Err(ProtocolError(format!( - "expected initialized notification, got {:?}", - m + "expected initialized notification, got error: {}", + e, ))) } } @@ -191,7 +203,8 @@ impl Connection { let _ = self.sender.send(resp.into()); match &self.receiver.recv_timeout(std::time::Duration::from_secs(30)) { Ok(Message::Notification(n)) if n.is_exit() => (), - m => return Err(ProtocolError(format!("unexpected message during shutdown: {:?}", m))), + Ok(msg) => return Err(ProtocolError(format!("unexpected message during shutdown: {:?}", msg))), + Err(e) => return Err(ProtocolError(format!("unexpected error during shutdown: {}", e))) } Ok(true) } From b46ba2f3d1faadc9832b5d8c4e1d0eeb9d3b179e Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Sat, 26 Dec 2020 19:20:00 +0200 Subject: [PATCH 2/2] Apply `cargo fmt` fixes --- src/lib.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index be5c30d..3866993 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -141,7 +141,7 @@ impl Connection { "expected Message::Notification, got: {:?}", msg, ))) - }, + } Err(e) => { return Err(ProtocolError(format!( "expected initialized notification, got error: {}", @@ -203,8 +203,12 @@ impl Connection { let _ = self.sender.send(resp.into()); match &self.receiver.recv_timeout(std::time::Duration::from_secs(30)) { Ok(Message::Notification(n)) if n.is_exit() => (), - Ok(msg) => return Err(ProtocolError(format!("unexpected message during shutdown: {:?}", msg))), - Err(e) => return Err(ProtocolError(format!("unexpected error during shutdown: {}", e))) + Ok(msg) => { + return Err(ProtocolError(format!("unexpected message during shutdown: {:?}", msg))) + } + Err(e) => { + return Err(ProtocolError(format!("unexpected error during shutdown: {}", e))) + } } Ok(true) }