From 07561131e9ec3d1f3aef99a8df2e3b9b7282156e Mon Sep 17 00:00:00 2001 From: Max Audron Date: Fri, 17 Jan 2020 12:30:27 +0100 Subject: add error handling --- src/net.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/net.rs') diff --git a/src/net.rs b/src/net.rs index f20dd39..f1f7027 100644 --- a/src/net.rs +++ b/src/net.rs @@ -10,6 +10,7 @@ extern crate log; use crate::protocol::message; +use crate::protocol::error::ErrorKind; pub struct Client { tcp_stream: TcpStream, @@ -24,10 +25,10 @@ impl Client { use crate::protocol::message::handshake::{HandshakeDeserialize, HandshakeSerialize, HandshakeQRead, VariantMap}; use crate::protocol::message::handshake::{ClientInit, ClientInitAck}; - self.tcp_stream.write(&client.serialize()).unwrap(); + self.tcp_stream.write(&client.serialize().unwrap()).unwrap(); let mut buf: Vec = [0; 2048].to_vec(); - let len = VariantMap::read(&mut self.tcp_stream, &mut buf); + let len = VariantMap::read(&mut self.tcp_stream, &mut buf).unwrap(); buf.truncate(len); let res = ClientInitAck::parse(&buf); @@ -67,18 +68,18 @@ pub fn connect(address: &'static str, port: u32, tls: bool, compression: bool) - } impl Deserialize for ConnAck { - fn parse(b: &[u8]) -> (usize, Self) { - let (flen, flags) = u8::parse(b); - let (elen, extra) = i16::parse(&b[flen..]); - let (vlen, version) = i8::parse(&b[(flen+elen)..]); + fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + let (flen, flags) = u8::parse(b)?; + let (elen, extra) = i16::parse(&b[flen..])?; + let (vlen, version) = i8::parse(&b[(flen+elen)..])?; - return (flen+elen+vlen, Self {flags, extra, version}); + return Ok((flen+elen+vlen, Self {flags, extra, version})); } } let mut buf = [0; 4]; s.read_exact(&mut buf)?; - let (_, val) = ConnAck::parse(&buf); + let (_, val) = ConnAck::parse(&buf).unwrap(); println!("Received: {:?}", val); let server: Client = Client { -- cgit v1.2.3 d>
Commit message (Collapse)AuthorLines
2022-02-19remove jsonnet lock fileMax Audron-36/+0
2022-02-19fix deploy to work with gitlab agentMax Audron-4/+4
2021-10-22write tons of documentation and reorganize some modulesMax Audron-65/+300
2021-10-22remove wolfram alpha url shorteningMax Audron-1/+2
2021-10-20remove failing wolfram alpha test casesMax Audron-105/+55
this is due to the url shorterner dying randomly and also just generally bad idea to call external services during unit tests.
2021-10-20bump version to 1.6.2Max Audron-3/+2
2021-10-20prepare for release on crates.ioMax Audron-39/+65
2021-10-20add async docs to macro crate and bump versionMax Audron-9/+10
2021-10-20change hook errors to be logged as warningsMax Audron-3/+3
they in nearly all cases aren't critical enough to warrant an actual error messages
2021-10-20fix configuration not loading correctly on release buildsMax Audron-8/+23
2021-10-19replace sedregex crate8-rework-sedMax Audron-20/+358
This replaces the sedregex crate with our own implementation for multiple reasons: 1. We required to access the parsed regex, this required a patch to the sedregex crate which did not get merged due to an inactive dev, blocking us from publishing on crates.Io 2. We wanted to highlight the changes done in bold 3. We want to add execution of multiple chained sed commands in the future which would require more modification
2021-10-19add formatting trait for irc codesMax Audron-0/+129
add an impl off the formatting trait on String to format Strings with the typical irc formatting codes for bold, italic etc
2021-10-17fix links in readmeMax Audron-2/+2