From abf0b9c47613f17fcc9b5b9dbb4c186621f05bd1 Mon Sep 17 00:00:00 2001 From: Max Verevkin Date: Mon, 25 Apr 2022 00:51:49 +0300 Subject: throw an error if protocol version is unsupported --- src/i3bar_protocol.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src/i3bar_protocol.rs') diff --git a/src/i3bar_protocol.rs b/src/i3bar_protocol.rs index ef0a933..54ebd93 100644 --- a/src/i3bar_protocol.rs +++ b/src/i3bar_protocol.rs @@ -94,16 +94,20 @@ impl Protocol { /// Extract new data from `bytes`, return unused bytes. pub fn process_new_bytes<'a>(&mut self, bytes: &'a [u8]) -> Result<&'a [u8]> { match self { - Self::Unknown => { - if let Ok((Some(header), rem)) = de_first_json::(bytes) { - if header.version == 1 { - *self = Self::JsonNotStarted(header); - return self.process_new_bytes(rem); - } + Self::Unknown => match de_first_json::(bytes) { + Ok((Some(header), rem)) if header.version == 1 => { + *self = Self::JsonNotStarted(header); + self.process_new_bytes(rem) } - *self = Self::PlainText { line: None }; - self.process_new_bytes(bytes) - } + Ok((Some(header), _)) => Err(Error::new( + ErrorKind::Other, + format!("Protocol version {} is not supported", header.version), + )), + _ => { + *self = Self::PlainText { line: None }; + self.process_new_bytes(bytes) + } + }, Self::PlainText { line } => { if let Some((new_line, rem)) = last_line(bytes) { *line = Some(String::from_utf8_lossy(new_line).into()); -- cgit v1.3.1