diff options
Diffstat (limited to 'src/message/handshake')
| -rw-r--r-- | src/message/handshake/clientinit.rs | 10 | ||||
| -rw-r--r-- | src/message/handshake/clientinitack.rs | 20 | ||||
| -rw-r--r-- | src/message/handshake/clientinitreject.rs | 10 | ||||
| -rw-r--r-- | src/message/handshake/clientlogin.rs | 15 | ||||
| -rw-r--r-- | src/message/handshake/clientloginack.rs | 10 | ||||
| -rw-r--r-- | src/message/handshake/clientloginreject.rs | 10 | ||||
| -rw-r--r-- | src/message/handshake/connack.rs | 6 | ||||
| -rw-r--r-- | src/message/handshake/mod.rs | 5 | ||||
| -rw-r--r-- | src/message/handshake/sessioninit.rs | 10 | ||||
| -rw-r--r-- | src/message/handshake/types.rs | 25 |
10 files changed, 41 insertions, 80 deletions
diff --git a/src/message/handshake/clientinit.rs b/src/message/handshake/clientinit.rs index d21d6aa..32e8595 100644 --- a/src/message/handshake/clientinit.rs +++ b/src/message/handshake/clientinit.rs @@ -1,8 +1,7 @@ +use crate::error::ProtocolError; use crate::primitive::{StringList, Variant, VariantMap}; use crate::HandshakeSerialize; -use failure::Error; - /// ClientInit is the Initial message send to the core after establishing a base layer comunication. /// /// Features @@ -44,12 +43,9 @@ pub struct ClientInit { } impl HandshakeSerialize for ClientInit { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { let mut values: VariantMap = VariantMap::with_capacity(5); - values.insert( - "MsgType".to_string(), - Variant::String("ClientInit".to_string()), - ); + values.insert("MsgType".to_string(), Variant::String("ClientInit".to_string())); values.insert( "ClientVersion".to_string(), Variant::String(self.client_version.clone()), diff --git a/src/message/handshake/clientinitack.rs b/src/message/handshake/clientinitack.rs index a259f9c..610cdc0 100644 --- a/src/message/handshake/clientinitack.rs +++ b/src/message/handshake/clientinitack.rs @@ -1,8 +1,7 @@ +use crate::error::ProtocolError; use crate::primitive::{Variant, VariantList, VariantMap}; use crate::HandshakeSerialize; -use failure::Error; - /// ClientInitAck is received when the initialization was successfull #[derive(Debug, Clone)] pub struct ClientInitAck { @@ -21,17 +20,14 @@ pub struct ClientInitAck { } impl HandshakeSerialize for ClientInitAck { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { 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)); - values.insert( - "Configured".to_string(), - Variant::bool(self.core_configured), - ); + values.insert("Configured".to_string(), Variant::bool(self.core_configured)); values.insert( "StorageBackends".to_string(), Variant::VariantList(self.storage_backends.clone()), @@ -55,15 +51,9 @@ impl From<VariantMap> for ClientInitAck { // TODO make this compatible with older clients core_features: 0, core_configured: match_variant!(input.get("Configured").unwrap(), Variant::bool), - storage_backends: match_variant!( - input.get("StorageBackends").unwrap(), - Variant::VariantList - ), + storage_backends: match_variant!(input.get("StorageBackends").unwrap(), Variant::VariantList), #[cfg(feature = "authenticators")] - authenticators: match_variant!( - input.get("Authenticators").unwrap(), - Variant::VariantList - ), + authenticators: match_variant!(input.get("Authenticators").unwrap(), Variant::VariantList), feature_list: match_variant!(input.get("FeatureList").unwrap(), Variant::StringList), } } diff --git a/src/message/handshake/clientinitreject.rs b/src/message/handshake/clientinitreject.rs index d93413d..8c2fd34 100644 --- a/src/message/handshake/clientinitreject.rs +++ b/src/message/handshake/clientinitreject.rs @@ -1,8 +1,7 @@ +use crate::error::ProtocolError; use crate::primitive::{Variant, VariantMap}; use crate::HandshakeSerialize; -use failure::Error; - /// ClientInitReject is received when the initialization fails #[derive(Debug, Clone)] pub struct ClientInitReject { @@ -11,16 +10,13 @@ pub struct ClientInitReject { } impl HandshakeSerialize for ClientInitReject { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { 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.clone()), - ); + values.insert("ErrorString".to_string(), Variant::String(self.error.clone())); return HandshakeSerialize::serialize(&values); } } diff --git a/src/message/handshake/clientlogin.rs b/src/message/handshake/clientlogin.rs index dffd996..c589810 100644 --- a/src/message/handshake/clientlogin.rs +++ b/src/message/handshake/clientlogin.rs @@ -1,8 +1,7 @@ +use crate::error::ProtocolError; use crate::primitive::{Variant, VariantMap}; use crate::HandshakeSerialize; -use failure::Error; - /// Login to the core with user data /// username and password are transmitted in plain text #[derive(Debug, Clone)] @@ -12,17 +11,11 @@ pub struct ClientLogin { } impl HandshakeSerialize for ClientLogin { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { let mut values: VariantMap = VariantMap::new(); - values.insert( - "MsgType".to_string(), - Variant::String("ClientLogin".to_string()), - ); + values.insert("MsgType".to_string(), Variant::String("ClientLogin".to_string())); values.insert("User".to_string(), Variant::String(self.user.clone())); - values.insert( - "Password".to_string(), - Variant::String(self.password.clone()), - ); + values.insert("Password".to_string(), Variant::String(self.password.clone())); return HandshakeSerialize::serialize(&values); } } diff --git a/src/message/handshake/clientloginack.rs b/src/message/handshake/clientloginack.rs index 72dd6ac..c8650f9 100644 --- a/src/message/handshake/clientloginack.rs +++ b/src/message/handshake/clientloginack.rs @@ -2,15 +2,13 @@ use crate::error::ProtocolError; use crate::primitive::{Variant, VariantMap}; use crate::{HandshakeDeserialize, HandshakeSerialize}; -use failure::Error; - /// ClientLoginAck is received after the client has successfully logged in /// it has no fields #[derive(Debug, Clone)] pub struct ClientLoginAck; impl HandshakeSerialize for ClientLoginAck { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { let mut values: VariantMap = VariantMap::with_capacity(1); values.insert( "MsgType".to_string(), @@ -21,15 +19,15 @@ impl HandshakeSerialize for ClientLoginAck { } impl HandshakeDeserialize for ClientLoginAck { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; let msgtype = match_variant!(&values["MsgType"], Variant::ByteArray); if msgtype == "ClientLogin" { - return Ok((len, Self {})); + Ok((len, Self {})) } else { - bail!(ProtocolError::WrongMsgType); + Err(ProtocolError::WrongMsgType) } } } diff --git a/src/message/handshake/clientloginreject.rs b/src/message/handshake/clientloginreject.rs index 0c0fc85..964ce0c 100644 --- a/src/message/handshake/clientloginreject.rs +++ b/src/message/handshake/clientloginreject.rs @@ -1,8 +1,7 @@ +use crate::error::ProtocolError; use crate::primitive::{Variant, VariantMap}; use crate::HandshakeSerialize; -use failure::Error; - /// ClientLoginReject is received after the client failed to login /// It contains an error message as String #[derive(Debug, Clone)] @@ -11,16 +10,13 @@ pub struct ClientLoginReject { } impl HandshakeSerialize for ClientLoginReject { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { 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()), - ); + values.insert("ErrorString".to_string(), Variant::String(self.error.clone())); return HandshakeSerialize::serialize(&values); } } diff --git a/src/message/handshake/connack.rs b/src/message/handshake/connack.rs index a246679..bed0cb5 100644 --- a/src/message/handshake/connack.rs +++ b/src/message/handshake/connack.rs @@ -1,4 +1,4 @@ -use failure::Error; +use crate::error::ProtocolError; /// Data received right after initializing the connection /// @@ -30,7 +30,7 @@ impl Default for ConnAck { } impl crate::serialize::Serialize for ConnAck { - fn serialize(&self) -> Result<Vec<std::primitive::u8>, Error> { + fn serialize(&self) -> Result<Vec<std::primitive::u8>, ProtocolError> { let mut bytes: Vec<u8> = Vec::new(); bytes.append(&mut self.flags.serialize()?); @@ -42,7 +42,7 @@ impl crate::serialize::Serialize for ConnAck { } impl crate::deserialize::Deserialize for ConnAck { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let (flen, flags) = u8::parse(b)?; let (elen, extra) = i16::parse(&b[flen..])?; let (vlen, version) = i8::parse(&b[(flen + elen)..])?; diff --git a/src/message/handshake/mod.rs b/src/message/handshake/mod.rs index 029eb86..186abf0 100644 --- a/src/message/handshake/mod.rs +++ b/src/message/handshake/mod.rs @@ -24,6 +24,7 @@ pub use protocol::*; pub use sessioninit::*; pub use types::*; +use crate::error::ProtocolError; use crate::primitive::VariantMap; use crate::{HandshakeDeserialize, HandshakeSerialize}; @@ -39,7 +40,7 @@ pub enum HandshakeMessage { } impl HandshakeSerialize for HandshakeMessage { - fn serialize(&self) -> Result<Vec<u8>, failure::Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { match self { HandshakeMessage::ClientInit(inner) => inner.serialize(), HandshakeMessage::ClientInitAck(inner) => inner.serialize(), @@ -53,7 +54,7 @@ impl HandshakeSerialize for HandshakeMessage { } impl HandshakeDeserialize for HandshakeMessage { - fn parse(b: &[u8]) -> Result<(usize, Self), failure::Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let (size, res) = VariantMap::parse(b)?; let msgtype: String = (&res["MsgType"]).into(); diff --git a/src/message/handshake/sessioninit.rs b/src/message/handshake/sessioninit.rs index 04f3cff..048324d 100644 --- a/src/message/handshake/sessioninit.rs +++ b/src/message/handshake/sessioninit.rs @@ -1,9 +1,8 @@ +use crate::error::ProtocolError; use crate::message::objects::Identity; use crate::primitive::{BufferInfo, Variant, VariantMap}; use crate::HandshakeSerialize; -use failure::Error; - /// SessionInit is received along with ClientLoginAck to initialize that user Session // TODO Replace with proper types #[derive(Debug, Clone)] @@ -46,12 +45,9 @@ impl From<VariantMap> for SessionInit { } impl HandshakeSerialize for SessionInit { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { let mut values: VariantMap = VariantMap::with_capacity(4); - values.insert( - "MsgType".to_string(), - Variant::String("SessionInit".to_string()), - ); + values.insert("MsgType".to_string(), Variant::String("SessionInit".to_string())); // values.insert( // "Identities".to_string(), // Variant::VariantList( diff --git a/src/message/handshake/types.rs b/src/message/handshake/types.rs index 6d4960d..fd8c8fa 100644 --- a/src/message/handshake/types.rs +++ b/src/message/handshake/types.rs @@ -1,8 +1,6 @@ use std::result::Result; use std::vec::Vec; -use failure::Error; - use crate::error::ProtocolError; use crate::primitive::Variant; use crate::util; @@ -12,7 +10,7 @@ use crate::message::handshake::{HandshakeDeserialize, HandshakeSerialize}; use crate::primitive::VariantMap; impl HandshakeSerialize for VariantMap { - fn serialize<'a>(&'a self) -> Result<Vec<u8>, Error> { + fn serialize<'a>(&'a self) -> Result<Vec<u8>, ProtocolError> { let mut res: Vec<u8> = Vec::new(); for (k, v) in self { @@ -29,7 +27,7 @@ impl HandshakeSerialize for VariantMap { } impl HandshakeDeserialize for VariantMap { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let (_, len) = i32::parse(&b[0..4])?; let mut pos: usize = 4; @@ -45,7 +43,7 @@ impl HandshakeDeserialize for VariantMap { match name { Variant::String(x) => map.insert(x, value), Variant::ByteArray(x) => map.insert(x, value), - _ => bail!(ProtocolError::WrongVariant), + _ => return Err(ProtocolError::WrongVariant), }; } @@ -58,21 +56,18 @@ pub fn serialize_variantmap() { let mut test_variantmap = VariantMap::new(); test_variantmap.insert("Configured".to_string(), Variant::bool(true)); let bytes = [ - 0, 0, 0, 2, 0, 0, 0, 10, 0, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, - 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1, 0, 1, + 0, 0, 0, 2, 0, 0, 0, 10, 0, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, + 114, 0, 101, 0, 100, 0, 0, 0, 1, 0, 1, ] .to_vec(); - assert_eq!( - HandshakeSerialize::serialize(&test_variantmap).unwrap(), - bytes - ); + assert_eq!(HandshakeSerialize::serialize(&test_variantmap).unwrap(), bytes); } #[test] pub fn deserialize_variantmap() { let test_bytes: &[u8] = &[ - 0, 0, 0, 2, 0, 0, 0, 10, 0, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, - 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 2, 0, 0, 0, 10, 0, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, + 114, 0, 101, 0, 100, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, ]; let mut test_variantmap = VariantMap::new(); test_variantmap.insert("Configured".to_string(), Variant::bool(true)); @@ -86,8 +81,8 @@ pub fn deserialize_variantmap() { #[test] pub fn deserialize_variantmap_utf8() { let test_bytes: &[u8] = &[ - 0, 0, 0, 2, 0, 0, 0, 12, 0, 0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100, - 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 2, 0, 0, 0, 12, 0, 0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100, 0, 0, 0, 1, + 0, 1, 0, 0, 0, 1, ]; let mut test_variantmap = VariantMap::new(); test_variantmap.insert("Configured".to_string(), Variant::bool(true)); |
