From b1bae553b620f0a1d71c6be7fa98c10978662907 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Tue, 21 Jan 2020 16:12:13 +0100 Subject: le tokio --- src/protocol/primitive/basic.rs | 58 ++++++++++++++++++++------------------- src/protocol/primitive/mod.rs | 19 ++++++------- src/protocol/primitive/variant.rs | 31 +++++++++++---------- 3 files changed, 55 insertions(+), 53 deletions(-) (limited to 'src/protocol/primitive') 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(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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::()?)); } } impl qread::QRead for u64 { - fn read(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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::()?)); } } impl qread::QRead for u32 { - fn read(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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::()?)); } } impl qread::QRead for u16 { - fn read(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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::()?)); } } impl qread::QRead for i64 { - fn read(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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::()?)); } } impl qread::QRead for i32 { - fn read(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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::()?)); } } impl qread::QRead for i16 { - fn read(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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, ErrorKind> { + fn serialize(&self) -> Result, Error> { let mut res: Vec = Vec::new(); let utf16: Vec = self.encode_utf16().collect(); @@ -177,7 +179,7 @@ impl serialize::Serialize for String { } impl serialize::SerializeUTF8 for String { - fn serialize_utf8(&self) -> Result, ErrorKind> { + fn serialize_utf8(&self) -> Result, Error> { let mut res: Vec = 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(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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; impl serialize::Serialize for StringList { - fn serialize(&self) -> Result, ErrorKind> { + fn serialize(&self) -> Result, Error> { let len: i32 = self.len().try_into()?; let mut res: Vec = 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(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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, ErrorKind>; + fn serialize(&self) -> Result, Error>; } pub trait SerializeUTF8 { - fn serialize_utf8(&self) -> Result, ErrorKind>; + fn serialize_utf8(&self) -> Result, 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(stream: &mut T, buf: &mut [u8]) -> Result; + fn read(stream: &mut T, buf: &mut [u8]) -> Result; } } 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; impl Serialize for VariantMap { - fn serialize<'a>(&'a self) -> Result, ErrorKind> { + fn serialize<'a>(&'a self) -> Result, Error> { let mut res: Vec = 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(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { s.read(&mut b[0..4])?; @@ -70,7 +72,7 @@ impl QRead for VariantMap { pub type VariantList = Vec; impl Serialize for VariantList { - fn serialize(&self) -> Result, ErrorKind> { + fn serialize(&self) -> Result, Error> { let len: i32 = self.len().try_into()?; let mut res: Vec = 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(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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, ErrorKind> { + fn serialize(&self) -> Result, Error> { let unknown: u8 = 0x00; let mut res: Vec = 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(s: &mut T, b: &mut [u8]) -> Result { + fn read(s: &mut T, b: &mut [u8]) -> Result { 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); -- cgit v1.2.3