diff options
Diffstat (limited to 'src/protocol')
| -rw-r--r-- | src/protocol/error/mod.rs | 49 | ||||
| -rw-r--r-- | src/protocol/message/handshake.rs | 122 | ||||
| -rw-r--r-- | src/protocol/message/handshake/types.rs | 20 | ||||
| -rw-r--r-- | src/protocol/primitive/basic.rs | 58 | ||||
| -rw-r--r-- | src/protocol/primitive/mod.rs | 19 | ||||
| -rw-r--r-- | src/protocol/primitive/variant.rs | 31 |
6 files changed, 146 insertions, 153 deletions
diff --git a/src/protocol/error/mod.rs b/src/protocol/error/mod.rs index 488ae0d..72a9e59 100644 --- a/src/protocol/error/mod.rs +++ b/src/protocol/error/mod.rs @@ -1,28 +1,37 @@ -#[derive(Debug)] -pub enum ErrorKind { + #[derive(Debug, Fail)] +pub enum ProtocolError { + #[fail(display = "message has wrong type")] WrongMsgType, + #[fail(display = "bool value is neither 0 nor 1")] BoolOutOfRange, + #[fail(display = "QVariant is not known")] UnknownVariant, + #[fail(display = "wrong variant has been given")] WrongVariant, + #[fail(display = "io error")] IOError(std::io::Error), + #[fail(display = "could not convert from int")] TryFromIntError(std::num::TryFromIntError), + #[fail(display = "utf8 error")] Utf8Error(std::string::FromUtf8Error), -} + } -impl std::convert::From<std::io::Error> for ErrorKind { - fn from(error: std::io::Error) -> Self { - ErrorKind::IOError(error) - } -} - -impl std::convert::From<std::num::TryFromIntError> for ErrorKind { - fn from(error: std::num::TryFromIntError) -> Self { - ErrorKind::TryFromIntError(error) - } -} - -impl std::convert::From<std::string::FromUtf8Error> for ErrorKind { - fn from(error: std::string::FromUtf8Error) -> Self { - ErrorKind::Utf8Error(error) - } -} +// impl std::error::Error for ErrorKind {} +// +// impl std::convert::From<std::io::Error> for ErrorKind { +// fn from(error: std::io::Error) -> Self { +// ErrorKind::IOError(error) +// } +// } +// +// impl std::convert::From<std::num::TryFromIntError> for ErrorKind { +// fn from(error: std::num::TryFromIntError) -> Self { +// ErrorKind::TryFromIntError(error) +// } +// } +// +// impl std::convert::From<std::string::FromUtf8Error> for ErrorKind { +// fn from(error: std::string::FromUtf8Error) -> Self { +// ErrorKind::Utf8Error(error) +// } +// } diff --git a/src/protocol/message/handshake.rs b/src/protocol/message/handshake.rs index 44c1073..b38d03f 100644 --- a/src/protocol/message/handshake.rs +++ b/src/protocol/message/handshake.rs @@ -1,12 +1,34 @@ use std::result::Result; +use failure::Error; -use crate::protocol::error::ErrorKind; +use crate::protocol::error::ProtocolError; use crate::protocol::primitive::{String, StringList, Variant, VariantList}; +use crate::util::get_msg_type; mod types; pub use types::{VariantMap, HandshakeDeserialize, HandshakeSerialize, HandshakeQRead}; use crate::match_variant; + + +#[derive(Debug)] +pub struct ConnAck { + flags: u8, + extra: i16, + version: i8 +} + +impl crate::protocol::primitive::deserialize::Deserialize for ConnAck { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (flen, flags) = u8::parse(b)?; + let (elen, extra) = i16::parse(&b[flen..])?; + let (vlen, version) = i8::parse(&b[(flen+elen)..])?; + + return Ok((flen+elen+vlen, Self {flags, extra, version})); + } +} + + #[derive(Debug)] pub struct ClientInit { pub client_version: String, // Version of the client @@ -16,7 +38,7 @@ pub struct ClientInit { } impl HandshakeSerialize for ClientInit { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let mut values: VariantMap = VariantMap::with_capacity(5); values.insert("MsgType".to_string(), Variant::String("ClientInit".to_string())); values.insert("ClientVersion".to_string(), Variant::String(self.client_version.clone())); @@ -28,16 +50,10 @@ impl HandshakeSerialize for ClientInit { } impl HandshakeDeserialize for ClientInit { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtypev = &values["MsgType"]; - let msgtype; - match msgtypev { - Variant::String(x) => msgtype = x, - Variant::StringUTF8(x) => msgtype = x, - _ => return Err(ErrorKind::WrongVariant) - }; + let msgtype = get_msg_type(&values["MsgType"])?; if msgtype == "ClientInit" { return Ok((len, Self { @@ -47,7 +63,7 @@ impl HandshakeDeserialize for ClientInit { client_features: match_variant!(values, Variant::u32, "Features") })); } else { - return Err(ErrorKind::WrongMsgType); + bail!(ProtocolError::WrongMsgType); } } } @@ -58,7 +74,7 @@ pub struct ClientInitReject { } impl HandshakeSerialize for ClientInitReject { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let mut values: VariantMap = VariantMap::with_capacity(2); values.insert("MsgType".to_string(), Variant::String("ClientInitReject".to_string())); values.insert("ErrorString".to_string(), Variant::String(self.error_string.clone())); @@ -67,23 +83,17 @@ impl HandshakeSerialize for ClientInitReject { } impl HandshakeDeserialize for ClientInitReject { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtypev = &values["MsgType"]; - let msgtype; - match msgtypev { - Variant::String(x) => msgtype = x, - Variant::StringUTF8(x) => msgtype = x, - _ => return Err(ErrorKind::WrongVariant) - }; + let msgtype = get_msg_type(&values["MsgType"])?; if msgtype == "ClientInitReject" { return Ok((len, Self { error_string: match_variant!(values, Variant::String, "ErrorString") })); } else { - return Err(ErrorKind::WrongMsgType); + bail!(ProtocolError::WrongMsgType); } } } @@ -98,7 +108,7 @@ pub struct ClientInitAck { } impl HandshakeSerialize for ClientInitAck { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let mut values: VariantMap = VariantMap::with_capacity(6); values.insert("MsgType".to_string(), Variant::String("ClientInitAck".to_string())); values.insert("CoreFeatures".to_string(), Variant::u32(self.core_features)); @@ -111,16 +121,10 @@ impl HandshakeSerialize for ClientInitAck { } impl HandshakeDeserialize for ClientInitAck { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtypev = &values["MsgType"]; - let msgtype; - match msgtypev { - Variant::String(x) => msgtype = x, - Variant::StringUTF8(x) => msgtype = x, - _ => return Err(ErrorKind::WrongVariant) - }; + let msgtype = get_msg_type(&values["MsgType"])?; if msgtype == "ClientInitAck" { return Ok((len, Self { @@ -131,7 +135,7 @@ impl HandshakeDeserialize for ClientInitAck { feature_list: match_variant!(values, Variant::StringList, "FeatureList") })); } else { - return Err(ErrorKind::WrongMsgType); + bail!(ProtocolError::WrongMsgType); } } } @@ -143,7 +147,7 @@ pub struct ClientLogin { } impl HandshakeSerialize for ClientLogin { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let mut values: VariantMap = VariantMap::new(); values.insert("MsgType".to_string(), Variant::String("ClientLogin".to_string())); values.insert("User".to_string(), Variant::String(self.user.clone())); @@ -153,16 +157,10 @@ impl HandshakeSerialize for ClientLogin { } impl HandshakeDeserialize for ClientLogin { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtypev = &values["MsgType"]; - let msgtype; - match msgtypev { - Variant::String(x) => msgtype = x, - Variant::StringUTF8(x) => msgtype = x, - _ => return Err(ErrorKind::WrongVariant) - }; + let msgtype = get_msg_type(&values["MsgType"])?; if msgtype == "ClientLogin" { return Ok((len, Self { @@ -170,7 +168,7 @@ impl HandshakeDeserialize for ClientLogin { password: match_variant!(values, Variant::String, "Password") })); } else { - return Err(ErrorKind::WrongMsgType); + bail!(ProtocolError::WrongMsgType); } } } @@ -179,7 +177,7 @@ impl HandshakeDeserialize for ClientLogin { pub struct ClientLoginAck; impl HandshakeSerialize for ClientLoginAck { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let mut values: VariantMap = VariantMap::with_capacity(1); values.insert("MsgType".to_string(), Variant::String("ClientLoginAck".to_string())); return HandshakeSerialize::serialize(&values); @@ -187,21 +185,15 @@ impl HandshakeSerialize for ClientLoginAck { } impl HandshakeDeserialize for ClientLoginAck { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtypev = &values["MsgType"]; - let msgtype; - match msgtypev { - Variant::String(x) => msgtype = x, - Variant::StringUTF8(x) => msgtype = x, - _ => return Err(ErrorKind::WrongVariant) - }; + let msgtype = get_msg_type(&values["MsgType"])?; if msgtype == "ClientLogin" { return Ok((len, Self {})); } else { - return Err(ErrorKind::WrongMsgType); + bail!(ProtocolError::WrongMsgType); } } } @@ -212,7 +204,7 @@ pub struct ClientLoginReject { } impl HandshakeSerialize for ClientLoginReject { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let mut values: VariantMap = VariantMap::with_capacity(1); values.insert("MsgType".to_string(), Variant::String("ClientLoginReject".to_string())); values.insert("ErrorString".to_string(), Variant::String(self.error.clone())); @@ -221,21 +213,15 @@ impl HandshakeSerialize for ClientLoginReject { } impl HandshakeDeserialize for ClientLoginReject { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtypev = &values["MsgType"]; - let msgtype; - match msgtypev { - Variant::String(x) => msgtype = x, - Variant::StringUTF8(x) => msgtype = x, - _ => return Err(ErrorKind::WrongVariant) - }; + let msgtype = get_msg_type(&values["MsgType"])?; if msgtype == "ClientLogin" { return Ok((len, Self { error: match_variant!(values, Variant::String, "ErrorString")})); } else { - return Err(ErrorKind::WrongMsgType); + bail!(ProtocolError::WrongMsgType); } } } @@ -248,7 +234,7 @@ pub struct SessionInit { } impl HandshakeSerialize for SessionInit { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let mut values: VariantMap = VariantMap::with_capacity(1); values.insert("MsgType".to_string(), Variant::String("SessionInit".to_string())); values.insert("Identities".to_string(), Variant::VariantList(self.identities.clone())); @@ -259,16 +245,10 @@ impl HandshakeSerialize for SessionInit { } impl HandshakeDeserialize for SessionInit { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - let msgtypev = &values["MsgType"]; - let msgtype; - match msgtypev { - Variant::String(x) => msgtype = x, - Variant::StringUTF8(x) => msgtype = x, - _ => return Err(ErrorKind::WrongVariant) - }; + let msgtype = get_msg_type(&values["MsgType"])?; if msgtype == "ClientLogin" { return Ok((len, Self { @@ -277,7 +257,7 @@ impl HandshakeDeserialize for SessionInit { network_ids: match_variant!(values, Variant::VariantList, "NetworkIds") })); } else { - return Err(ErrorKind::WrongMsgType); + bail!(ProtocolError::WrongMsgType); } } } diff --git a/src/protocol/message/handshake/types.rs b/src/protocol/message/handshake/types.rs index 3c5d019..643b376 100644 --- a/src/protocol/message/handshake/types.rs +++ b/src/protocol/message/handshake/types.rs @@ -4,29 +4,31 @@ use std::result::Result; use std::convert::TryInto; use std::collections::HashMap; +use failure::Error; + use crate::util; use crate::protocol::primitive::{String, Variant}; use crate::protocol::primitive::serialize::Serialize; use crate::protocol::primitive::deserialize::Deserialize; use crate::protocol::primitive::qread::QRead; -use crate::protocol::error::ErrorKind; +use crate::protocol::error::ProtocolError; pub trait HandshakeSerialize { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind>; + fn serialize(&self) -> Result<Vec<u8>, Error>; } pub trait HandshakeDeserialize { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> where Self: std::marker::Sized ; + fn parse(b: &[u8]) -> Result<(usize, Self), Error> where Self: std::marker::Sized ; } pub trait HandshakeQRead { - fn read<T: Read>(stream: &mut T, buf: &mut [u8]) -> Result<usize, ErrorKind>; + fn read<T: Read>(stream: &mut T, buf: &mut [u8]) -> Result<usize, Error>; } pub type VariantMap = HashMap<String, Variant>; impl HandshakeSerialize for VariantMap { - fn serialize<'a>(&'a self) -> Result<Vec<u8>, ErrorKind> { + fn serialize<'a>(&'a self) -> Result<Vec<u8>, Error> { let mut res: Vec<u8> = Vec::new(); for (k, v) in self { @@ -37,7 +39,7 @@ impl HandshakeSerialize for VariantMap { util::insert_bytes(0, &mut res, &mut [0, 0, 0, 10]); - let len: i32 = res.len().try_into()?; + let len: i32 = res.len().try_into().unwrap(); util::insert_bytes(0, &mut res, &mut ((len).to_be_bytes())); return Ok(res); @@ -45,7 +47,7 @@ impl HandshakeSerialize for VariantMap { } impl HandshakeDeserialize for VariantMap { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, len) = i32::parse(&b[0..4])?; let mut pos: usize = 8; @@ -62,7 +64,7 @@ impl HandshakeDeserialize for VariantMap { match name { Variant::String(x) => map.insert(x, value), Variant::StringUTF8(x) => map.insert(x, value), - _ => return Err(ErrorKind::WrongVariant) + _ => bail!(ProtocolError::WrongVariant) }; } @@ -71,7 +73,7 @@ impl HandshakeDeserialize for VariantMap { } impl HandshakeQRead for VariantMap { - fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { s.read(&mut b[0..4])?; let (_, len) = i32::parse(&b[0..4])?; let ulen = len as usize; diff --git a/src/protocol/primitive/basic.rs b/src/protocol/primitive/basic.rs index 15b712f..c9f462d 100644 --- a/src/protocol/primitive/basic.rs +++ b/src/protocol/primitive/basic.rs @@ -35,126 +35,128 @@ use std::vec::Vec; use std::result::Result; use std::convert::TryInto; +use failure::Error; + use crate::util; -use crate::protocol::error::ErrorKind; +use crate::protocol::error::ProtocolError; use crate::protocol::primitive::{deserialize, serialize, qread}; impl deserialize::Deserialize for bool { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { if b[0] == 0 { return Ok((1, false)) } else if b[0] == 1 { return Ok((1, true)) } else { - return Err(ErrorKind::BoolOutOfRange); + bail!(ProtocolError::BoolOutOfRange); }; } } impl qread::QRead for bool { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { Ok(s.read(&mut b[0..1])?) } } impl deserialize::Deserialize for u64 { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..8]); return Ok((8, rdr.read_u64::<BigEndian>()?)); } } impl qread::QRead for u64 { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { Ok(s.read(&mut b[0..8])?) } } impl deserialize::Deserialize for u32 { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..4]); return Ok((4, rdr.read_u32::<BigEndian>()?)); } } impl qread::QRead for u32 { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { Ok(s.read(&mut b[0..4])?) } } impl deserialize::Deserialize for u16 { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..2]); return Ok((2, rdr.read_u16::<BigEndian>()?)); } } impl qread::QRead for u16 { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { Ok(s.read(&mut b[0..2])?) } } impl deserialize::Deserialize for u8 { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { return Ok((1, b[0])); } } impl qread::QRead for u8 { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { Ok(s.read(&mut [b[0]])?) } } impl deserialize::Deserialize for i64 { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..8]); return Ok((8, rdr.read_i64::<BigEndian>()?)); } } impl qread::QRead for i64 { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { Ok(s.read(&mut b[0..8])?) } } impl deserialize::Deserialize for i32 { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..4]); return Ok((4, rdr.read_i32::<BigEndian>()?)); } } impl qread::QRead for i32 { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { Ok(s.read(&mut b[0..4])?) } } impl deserialize::Deserialize for i16 { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..2]); return Ok((2, rdr.read_i16::<BigEndian>()?)); } } impl qread::QRead for i16 { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { Ok(s.read(&mut b[0..2])?) } } impl deserialize::Deserialize for i8 { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { return Ok((1, b[0].try_into()?)); } } impl qread::QRead for i8 { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { return Ok(s.read(&mut [b[0]])?) } } @@ -163,7 +165,7 @@ impl qread::QRead for i8 { pub type String = std::string::String; impl serialize::Serialize for String { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let mut res: Vec<u8> = Vec::new(); let utf16: Vec<u16> = self.encode_utf16().collect(); @@ -177,7 +179,7 @@ impl serialize::Serialize for String { } impl serialize::SerializeUTF8 for String { - fn serialize_utf8(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize_utf8(&self) -> Result<Vec<u8>, Error> { let mut res: Vec<u8> = Vec::new(); res.extend(self.clone().into_bytes()); util::prepend_byte_len(&mut res); @@ -186,7 +188,7 @@ impl serialize::SerializeUTF8 for String { } impl deserialize::Deserialize for String { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, len) = i32::parse(&b[0..4])?; let ulen = len as usize; @@ -205,7 +207,7 @@ impl deserialize::Deserialize for String { } impl deserialize::DeserializeUTF8 for String { - fn parse_utf8(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> { use crate::protocol::primitive::deserialize::Deserialize; let (_, len) = i32::parse(&b[0..4])?; @@ -217,7 +219,7 @@ impl deserialize::DeserializeUTF8 for String { } impl qread::QRead for String { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { use crate::protocol::primitive::deserialize::Deserialize; s.read(&mut b[0..4])?; @@ -232,7 +234,7 @@ impl qread::QRead for String { pub type StringList = Vec<String>; impl serialize::Serialize for StringList { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let len: i32 = self.len().try_into()?; let mut res: Vec<u8> = Vec::new(); @@ -246,7 +248,7 @@ impl serialize::Serialize for StringList { } impl deserialize::Deserialize for StringList { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, len) = i32::parse(&b[0..4])?; let mut res: StringList = StringList::new(); @@ -264,7 +266,7 @@ impl deserialize::Deserialize for StringList { } impl qread::QRead for StringList { - fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: std::io::Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { use crate::protocol::primitive::deserialize::Deserialize; s.read(&mut b[0..4])?; diff --git a/src/protocol/primitive/mod.rs b/src/protocol/primitive/mod.rs index 42f6aae..fb843ad 100644 --- a/src/protocol/primitive/mod.rs +++ b/src/protocol/primitive/mod.rs @@ -7,31 +7,28 @@ pub use variant::*; pub mod serialize { - use crate::protocol::error::ErrorKind; - + use failure::Error; pub trait Serialize { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind>; + fn serialize(&self) -> Result<Vec<u8>, Error>; } pub trait SerializeUTF8 { - fn serialize_utf8(&self) -> Result<Vec<u8>, ErrorKind>; + fn serialize_utf8(&self) -> Result<Vec<u8>, Error>; } } pub mod deserialize { - use crate::protocol::error::ErrorKind; - + use failure::Error; pub trait Deserialize { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> where Self: std::marker::Sized ; + fn parse(b: &[u8]) -> Result<(usize, Self), Error> where Self: std::marker::Sized ; } pub trait DeserializeUTF8 { - fn parse_utf8(b: &[u8]) -> Result<(usize, Self), ErrorKind> where Self: std::marker::Sized ; + fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> where Self: std::marker::Sized ; } } pub mod qread { - use crate::protocol::error::ErrorKind; - + use failure::Error; pub trait QRead { - fn read<T: std::io::Read>(stream: &mut T, buf: &mut [u8]) -> Result<usize, ErrorKind>; + fn read<T: std::io::Read>(stream: &mut T, buf: &mut [u8]) -> Result<usize, Error>; } } diff --git a/src/protocol/primitive/variant.rs b/src/protocol/primitive/variant.rs index 8c6173f..24305ca 100644 --- a/src/protocol/primitive/variant.rs +++ b/src/protocol/primitive/variant.rs @@ -1,20 +1,22 @@ +use std::io::Read; use std::vec::Vec; use std::convert::TryInto; use std::collections::HashMap; -use std::io::Read; +use failure::Error; + use crate::util; use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; use crate::protocol::primitive::qread::QRead; use crate::protocol::primitive::{String,StringList}; -use crate::protocol::error::ErrorKind; +use crate::protocol::error::ProtocolError; use crate::protocol::primitive; pub type VariantMap = HashMap<String, Variant>; impl Serialize for VariantMap { - fn serialize<'a>(&'a self) -> Result<Vec<u8>, ErrorKind> { + fn serialize<'a>(&'a self) -> Result<Vec<u8>, Error> { let mut res: Vec<u8> = Vec::new(); for (k, v) in self { @@ -30,7 +32,7 @@ impl Serialize for VariantMap { } impl Deserialize for VariantMap { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, len) = i32::parse(&b[0..4])?; let mut pos = 4; @@ -50,7 +52,7 @@ impl Deserialize for VariantMap { } impl QRead for VariantMap { - fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { s.read(&mut b[0..4])?; @@ -70,7 +72,7 @@ impl QRead for VariantMap { pub type VariantList = Vec<Variant>; impl Serialize for VariantList { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let len: i32 = self.len().try_into()?; let mut res: Vec<u8> = Vec::new(); @@ -84,7 +86,7 @@ impl Serialize for VariantList { } impl Deserialize for VariantList { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, len) = i32::parse(&b[0..4])?; let mut res: VariantList = VariantList::new(); @@ -100,7 +102,7 @@ impl Deserialize for VariantList { } impl QRead for VariantList { - fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { s.read(&mut b[0..4])?; let (_, len) = i32::parse(&b[0..4])?; @@ -136,13 +138,13 @@ pub enum Variant { } impl Serialize for Variant { - fn serialize(&self) -> Result<Vec<u8>, ErrorKind> { + fn serialize(&self) -> Result<Vec<u8>, Error> { let unknown: u8 = 0x00; let mut res: Vec<u8> = Vec::new(); match self { Variant::Unknown => { - return Err(ErrorKind::UnknownVariant); + bail!(ProtocolError::UnknownVariant); }, Variant::VariantMap(v) => { res.extend(primitive::QVARIANTMAP.to_be_bytes().iter()); @@ -222,9 +224,10 @@ impl Serialize for Variant { } impl Deserialize for Variant { - fn parse(b: &[u8]) -> Result<(usize, Self), ErrorKind> { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, qtype) = i32::parse(&b[0..4])?; let qtype = qtype as u32; + println!("type: {:?}", &b[0..4]); #[allow(unused_variables)] let unknown: u8 = b[4]; @@ -288,14 +291,14 @@ impl Deserialize for Variant { return Ok((len+vlen, Variant::i8(value))); }, _ => { - return Err(ErrorKind::UnknownVariant); + bail!(ProtocolError::UnknownVariant); } } } } impl QRead for Variant { - fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, ErrorKind> { + fn read<T: Read>(s: &mut T, b: &mut [u8]) -> Result<usize, Error> { s.read(&mut b[0..4])?; let (_, qtype) = i32::parse(&b[0..4])?; @@ -319,7 +322,7 @@ impl QRead for Variant { primitive::INT => len += i32::read(s, &mut b[len..])?, primitive::SHORT => len += i16::read(s, &mut b[len..])?, primitive::CHAR => len += i8::read(s, &mut b[len..])?, - _ => return Err(ErrorKind::UnknownVariant) + _ => bail!(ProtocolError::UnknownVariant) } return Ok(len); |
