From 0e5aa5c00498399fc748736f85050128324c1da5 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Mon, 21 Oct 2019 16:13:39 +0200 Subject: [PATCH] feat: expose read_greeting (#144) This is sometimes needed when manually using `Client::new()`. We enforce that the greeting is read at most once. --- src/client.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index 1059d9e..5bcfa6c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -78,6 +78,9 @@ pub struct Connection { /// Enable debug mode for this connection so that all client-server interactions are printed to /// `STDERR`. pub debug: bool, + + /// Tracks if we have read a greeting. + pub greeting_read: bool, } // `Deref` instances are so we can make use of the same underlying primitives in `Client` and @@ -203,6 +206,7 @@ impl Client { stream: BufStream::new(stream), tag: INITIAL_TAG, debug: false, + greeting_read: false, }, } } @@ -1099,10 +1103,17 @@ impl Session { } impl Connection { - fn read_greeting(&mut self) -> Result<()> { + /// Read the greeting from the connection. Needs to be done after `connect`ing. + /// + /// Panics if called more than once on the same `Connection`. + pub fn read_greeting(&mut self) -> Result> { + assert!(!self.greeting_read, "Greeting can only be read once"); + let mut v = Vec::new(); self.readline(&mut v)?; - Ok(()) + self.greeting_read = true; + + Ok(v) } fn run_command_and_check_ok(&mut self, command: &str) -> Result<()> {