diff options
Diffstat (limited to '')
| -rw-r--r-- | src/primitive/bufferinfo.rs (renamed from src/protocol/primitive/bufferinfo.rs) | 21 | ||||
| -rw-r--r-- | src/primitive/datetime.rs (renamed from src/protocol/primitive/datetime.rs) | 28 | ||||
| -rw-r--r-- | src/primitive/message.rs (renamed from src/protocol/primitive/message.rs) | 41 | ||||
| -rw-r--r-- | src/primitive/signedint.rs (renamed from src/protocol/primitive/signedint.rs) | 18 | ||||
| -rw-r--r-- | src/primitive/string.rs (renamed from src/protocol/primitive/string.rs) | 21 | ||||
| -rw-r--r-- | src/primitive/stringlist.rs (renamed from src/protocol/primitive/stringlist.rs) | 12 | ||||
| -rw-r--r-- | src/primitive/unsignedint.rs (renamed from src/protocol/primitive/unsignedint.rs) | 24 | ||||
| -rw-r--r-- | src/primitive/variant.rs (renamed from src/protocol/primitive/variant.rs) | 80 | ||||
| -rw-r--r-- | src/primitive/variantlist.rs (renamed from src/protocol/primitive/variantlist.rs) | 11 | ||||
| -rw-r--r-- | src/primitive/variantmap.rs (renamed from src/protocol/primitive/variantmap.rs) | 23 | ||||
| -rw-r--r-- | src/protocol/primitive/mod.rs | 74 |
11 files changed, 168 insertions, 185 deletions
diff --git a/src/protocol/primitive/bufferinfo.rs b/src/primitive/bufferinfo.rs index 4c69286..9cbaa2d 100644 --- a/src/protocol/primitive/bufferinfo.rs +++ b/src/primitive/bufferinfo.rs @@ -2,18 +2,24 @@ use std::vec::Vec; use failure::Error; -use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; -use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; -use crate::protocol::primitive::String; +use crate::{Deserialize, DeserializeUTF8}; +use crate::{Serialize, SerializeUTF8}; extern crate bytes; +/// The BufferInfo struct represents a BufferInfo as received in IRC +/// +/// BufferInfo is, like all other struct based types, serialized sequentially. #[derive(Clone, Debug, std::cmp::PartialEq)] pub struct BufferInfo { - pub id: i32, // a unique, sequential id for the buffer - pub network_id: i32, // NetworkId of the network the buffer belongs to + /// a unique, sequential id for the buffer + pub id: i32, + /// NetworkId of the network the buffer belongs to + pub network_id: i32, + /// The Type of the Buffer pub buffer_type: BufferType, - pub name: String, // BufferName as displayed to the user + /// BufferName as displayed to the user + pub name: String, } impl Serialize for BufferInfo { @@ -36,7 +42,7 @@ impl Deserialize for BufferInfo { let (_, network_id) = i32::parse(&b[4..8])?; let (_, buffer_type) = i16::parse(&b[8..10])?; - // There are 4 additional undocumted Bytes in the BufferInfo + // There are 4 additional undocumented Bytes in the BufferInfo // so we start at byte 14 let (size, name) = String::parse_utf8(&b[14..])?; @@ -52,6 +58,7 @@ impl Deserialize for BufferInfo { } } +/// The Type of the Buffer #[repr(i16)] #[derive(Copy, Clone, Debug, std::cmp::PartialEq)] pub enum BufferType { diff --git a/src/protocol/primitive/datetime.rs b/src/primitive/datetime.rs index 688a022..e6946b9 100644 --- a/src/protocol/primitive/datetime.rs +++ b/src/primitive/datetime.rs @@ -1,11 +1,17 @@ -use crate::protocol::primitive::deserialize::Deserialize; -use crate::protocol::primitive::serialize::Serialize; +use crate::Deserialize; +use crate::Serialize; +/// The DateTime struct represents a DateTime as received in IRC +/// +/// DateTime is, like all other struct based types, serialized sequentially. #[derive(Clone, Debug, std::cmp::PartialEq)] pub struct DateTime { - julian_day: i32, // Day in Julian calendar, unknown if signed or unsigned - millis_of_day: i32, // Milliseconds since start of day - zone: u8, // Timezone of DateTime, 0x00 is local, 0x01 is UTC + /// Day in Julian calendar, unknown if signed or unsigned + julian_day: i32, + /// Milliseconds since start of day + millis_of_day: i32, + /// Timezone of DateTime, 0x00 is local, 0x01 is UTC + zone: u8, } impl Serialize for DateTime { @@ -40,9 +46,13 @@ impl Deserialize for DateTime { } } +/// The Date struct represents a Date as received in IRC +/// +/// Date is, like all other struct based types, serialized sequentially. #[derive(Clone, Debug, std::cmp::PartialEq)] pub struct Date { - julian_day: i32, // Day in Julian calendar, unknown if signed or unsigned + /// Day in Julian calendar, unknown if signed or unsigned + julian_day: i32, } impl Serialize for Date { @@ -66,9 +76,13 @@ impl Deserialize for Date { } } +/// The Time struct represents a Time as received in IRC +/// +/// Time is, like all other struct based types, serialized sequentially. #[derive(Clone, Debug, std::cmp::PartialEq)] pub struct Time { - millis_of_day: i32, // Milliseconds since start of day + /// Milliseconds since start of day + millis_of_day: i32, } impl Serialize for Time { diff --git a/src/protocol/primitive/message.rs b/src/primitive/message.rs index 4ae895d..64b132d 100644 --- a/src/protocol/primitive/message.rs +++ b/src/primitive/message.rs @@ -2,26 +2,41 @@ use std::vec::Vec; use failure::Error; -use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; -use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; +use crate::{Deserialize, DeserializeUTF8}; +use crate::{Serialize, SerializeUTF8}; -use crate::protocol::primitive::BufferInfo; -use crate::protocol::primitive::String; +use crate::primitive::BufferInfo; extern crate bytes; +/// The Message struct represents a Message as received in IRC +/// +/// Messages are, like all other struct based types, serialized sequentially. #[derive(Clone, Debug, std::cmp::PartialEq)] pub struct Message { - pub msg_id: i32, // The unique, sequential id for the message - pub timestamp: i64, // The timestamp of the message in UNIX time (32-bit, seconds, 64-bit if LONGMESSAGE feature enabled) + /// The unique, sequential id for the message + pub msg_id: i32, + /// The timestamp of the message in UNIX time (32-bit, seconds, 64-bit if LONGMESSAGE feature enabled) + pub timestamp: i64, + /// The message type as it's own type serialized as i32 pub msg_type: MessageType, + /// The flags pub flags: i8, - pub buffer: BufferInfo, // The buffer the message belongs to, usually everything but BufferId is set to NULL - pub sender: String, // The sender as nick!ident@host - pub sender_prefixes: Option<String>, // The prefix modes of the sender - pub real_name: Option<String>, // The realName of the sender - pub avatar_url: Option<String>, // The avatarUrl of the sender, if available - pub content: String, // The message content, already stripped from CTCP formatting, but containing mIRC format codes + /// The buffer the message belongs to, usually everything but BufferId is set to NULL + pub buffer: BufferInfo, + /// The sender as nick!ident@host + pub sender: String, + /// The prefix modes of the sender. + /// Only Some when the SenderPrefix features is enabled + pub sender_prefixes: Option<String>, + /// The realName of the sender + /// Only Some when the RichMessage features is enabled + pub real_name: Option<String>, + /// The avatarUrl of the sender, if available + /// Only Some when the RichMessage features is enabled + pub avatar_url: Option<String>, + /// The message content, already stripped from CTCP formatting, but containing mIRC format codes + pub content: String, } impl Serialize for Message { @@ -155,6 +170,7 @@ pub enum MessageType { NetsplitJoin = 0x00008000, NetsplitQuit = 0x00010000, Invite = 0x00020000, + Markerline = 0x00040000, } impl From<i32> for MessageType { @@ -178,6 +194,7 @@ impl From<i32> for MessageType { 0x00008000 => MessageType::NetsplitJoin, 0x00010000 => MessageType::NetsplitQuit, 0x00020000 => MessageType::Invite, + 0x00040000 => MessageType::Markerline, _ => unimplemented!(), } } diff --git a/src/protocol/primitive/signedint.rs b/src/primitive/signedint.rs index 67ffb9d..4c21a69 100644 --- a/src/protocol/primitive/signedint.rs +++ b/src/primitive/signedint.rs @@ -8,54 +8,54 @@ use std::vec::Vec; use failure::Error; -use crate::protocol::primitive::{deserialize, serialize}; +use crate::{Deserialize, Serialize}; -impl serialize::Serialize for i64 { +impl Serialize for i64 { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok(Vec::from(self.to_be_bytes())) } } -impl deserialize::Deserialize for i64 { +impl Deserialize for i64 { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..8]); return Ok((8, rdr.read_i64::<BigEndian>()?)); } } -impl serialize::Serialize for i32 { +impl Serialize for i32 { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok(Vec::from(self.to_be_bytes())) } } -impl deserialize::Deserialize for i32 { +impl Deserialize for i32 { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..4]); return Ok((4, rdr.read_i32::<BigEndian>()?)); } } -impl serialize::Serialize for i16 { +impl Serialize for i16 { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok(Vec::from(self.to_be_bytes())) } } -impl deserialize::Deserialize for i16 { +impl Deserialize for i16 { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..2]); return Ok((2, rdr.read_i16::<BigEndian>()?)); } } -impl serialize::Serialize for i8 { +impl Serialize for i8 { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok(Vec::from(self.to_be_bytes())) } } -impl deserialize::Deserialize for i8 { +impl Deserialize for i8 { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { return Ok((1, b[0].try_into()?)); } diff --git a/src/protocol/primitive/string.rs b/src/primitive/string.rs index 470f018..86bcdec 100644 --- a/src/protocol/primitive/string.rs +++ b/src/primitive/string.rs @@ -7,11 +7,15 @@ use failure::Error; use log::trace; -use crate::protocol::primitive::{deserialize, serialize}; +use crate::{Deserialize, DeserializeUTF8, Serialize, SerializeUTF8}; use crate::util; -pub type String = std::string::String; -impl serialize::Serialize for String { +/// We Shadow the String type here as we can only use impl on types in our own scope. +/// +/// Strings are serialized as an i32 for the length in bytes, then the chars represented in UTF-16 in bytes. +/// +/// Strings can only be serialized as UTF-8 null-terminated ByteArrays with (de)serialize_utf8(). +impl Serialize for String { fn serialize(&self) -> Result<Vec<u8>, Error> { let mut res: Vec<u8> = Vec::new(); @@ -25,7 +29,7 @@ impl serialize::Serialize for String { } } -impl serialize::SerializeUTF8 for String { +impl SerializeUTF8 for String { fn serialize_utf8(&self) -> Result<Vec<u8>, Error> { let mut res: Vec<u8> = Vec::new(); res.extend(self.clone().into_bytes()); @@ -35,11 +39,11 @@ impl serialize::SerializeUTF8 for String { } } -impl deserialize::Deserialize for String { +impl Deserialize for String { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { // Parse Length let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); + trace!(target: "primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); if len == -1 { return Ok((4, "".to_string())); @@ -64,12 +68,11 @@ impl deserialize::Deserialize for String { } } -impl deserialize::DeserializeUTF8 for String { +impl DeserializeUTF8 for String { fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> { - use crate::protocol::primitive::deserialize::Deserialize; let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); + trace!(target: "primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); if len <= 0 { return Ok((4, "".to_string())); diff --git a/src/protocol/primitive/stringlist.rs b/src/primitive/stringlist.rs index d2902f2..e5d1a44 100644 --- a/src/protocol/primitive/stringlist.rs +++ b/src/primitive/stringlist.rs @@ -8,10 +8,14 @@ use failure::Error; use log::trace; -use crate::protocol::primitive::{deserialize, serialize}; +use crate::{Deserialize, Serialize}; +/// StringList are represented as a Vec of Strings +/// +/// StringLists are serialized as an i32 of the amount of elements and then each element as a String pub type StringList = Vec<String>; -impl serialize::Serialize for StringList { + +impl Serialize for StringList { fn serialize(&self) -> Result<Vec<u8>, Error> { let len: i32 = self.len().try_into()?; let mut res: Vec<u8> = Vec::new(); @@ -25,10 +29,10 @@ impl serialize::Serialize for StringList { } } -impl deserialize::Deserialize for StringList { +impl Deserialize for StringList { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::StringList", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); + trace!(target: "primitive::StringList", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); let mut res: StringList = StringList::new(); let mut pos = 4; diff --git a/src/protocol/primitive/unsignedint.rs b/src/primitive/unsignedint.rs index 5b42e3c..6e91e2a 100644 --- a/src/protocol/primitive/unsignedint.rs +++ b/src/primitive/unsignedint.rs @@ -7,10 +7,10 @@ use std::vec::Vec; use failure::Error; -use crate::protocol::error::ProtocolError; -use crate::protocol::primitive::{deserialize, serialize}; +use crate::error::ProtocolError; +use crate::{Deserialize, Serialize}; -impl serialize::Serialize for bool { +impl Serialize for bool { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok({ let i = *self as i8; @@ -18,7 +18,7 @@ impl serialize::Serialize for bool { }) } } -impl deserialize::Deserialize for bool { +impl Deserialize for bool { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { if b[0] == 0 { return Ok((1, false)); @@ -29,52 +29,52 @@ impl deserialize::Deserialize for bool { }; } } -impl serialize::Serialize for u64 { +impl Serialize for u64 { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok(Vec::from(self.to_be_bytes())) } } -impl deserialize::Deserialize for u64 { +impl Deserialize for u64 { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..8]); return Ok((8, rdr.read_u64::<BigEndian>()?)); } } -impl serialize::Serialize for u32 { +impl Serialize for u32 { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok(Vec::from(self.to_be_bytes())) } } -impl deserialize::Deserialize for u32 { +impl Deserialize for u32 { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..4]); return Ok((4, rdr.read_u32::<BigEndian>()?)); } } -impl serialize::Serialize for u16 { +impl Serialize for u16 { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok(Vec::from(self.to_be_bytes())) } } -impl deserialize::Deserialize for u16 { +impl Deserialize for u16 { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let mut rdr = Cursor::new(&b[0..2]); return Ok((2, rdr.read_u16::<BigEndian>()?)); } } -impl serialize::Serialize for u8 { +impl Serialize for u8 { fn serialize(&self) -> Result<Vec<u8>, Error> { Ok(Vec::from(self.to_be_bytes())) } } -impl deserialize::Deserialize for u8 { +impl Deserialize for u8 { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { return Ok((1, b[0])); } diff --git a/src/protocol/primitive/variant.rs b/src/primitive/variant.rs index 84150a8..71ddc4a 100644 --- a/src/protocol/primitive/variant.rs +++ b/src/primitive/variant.rs @@ -4,24 +4,31 @@ use failure::Error; use log::{error, trace}; -use crate::protocol::error::ProtocolError; -use crate::protocol::primitive; -use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; -use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; -use crate::protocol::primitive::{String, StringList}; +use crate::error::ProtocolError; +use crate::primitive; +use crate::primitive::StringList; +use crate::{Deserialize, DeserializeUTF8}; +use crate::{Serialize, SerializeUTF8}; extern crate bytes; -use bytes::BytesMut; -use crate::protocol::primitive::{ +use crate::primitive::{ BufferInfo, Date, DateTime, Message, Time, VariantList, VariantMap, }; +/// Variant represents the possible types we can receive +/// +/// Variant's are serizalized as the Type as a i32 and then the Type in it's own format +/// +/// BufferInfo and Message are UserTypes +/// but we represent them as a native Type here. +/// +/// StringUTF8 is de-/serialized as a C ByteArray. #[allow(non_camel_case_types, dead_code)] #[derive(Clone, Debug, std::cmp::PartialEq)] pub enum Variant { Unknown, - UserType(String, BytesMut), + UserType(String, Vec<u8>), BufferInfo(BufferInfo), Message(Message), Time(Time), @@ -31,7 +38,6 @@ pub enum Variant { VariantList(VariantList), String(String), StringUTF8(String), - ByteArray(String), StringList(StringList), bool(bool), u64(u64), @@ -73,12 +79,6 @@ impl Serialize for Variant { res.extend(unknown.to_be_bytes().iter()); res.extend(v.serialize_utf8()?.iter()); } - Variant::ByteArray(v) => { - res.extend(primitive::QBYTEARRAY.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.serialize_utf8()?.iter()); - res.extend(vec![0x00]); - } Variant::StringList(v) => { res.extend(primitive::QSTRINGLIST.to_be_bytes().iter()); res.extend(unknown.to_be_bytes().iter()); @@ -130,9 +130,22 @@ impl Serialize for Variant { res.extend(unknown.to_be_bytes().iter()); res.extend(v.to_be_bytes().iter()); } - Variant::UserType(_, _) => unimplemented!(), - Variant::BufferInfo(_) => unimplemented!(), - Variant::Message(_) => unimplemented!(), + Variant::UserType(name, bytes) => { + res.extend(primitive::USERTYPE.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.append(&mut name.serialize_utf8()?); + res.extend(bytes); + } + Variant::BufferInfo(v) => { + let bytes = BufferInfo::serialize(v)?; + let user = Variant::UserType("BufferInfo".to_string(), bytes); + Variant::serialize(&user).unwrap(); + } + Variant::Message(v) => { + let bytes = Message::serialize(v)?; + let user = Variant::UserType("Message".to_string(), bytes); + Variant::serialize(&user).unwrap(); + } Variant::DateTime(v) => { res.extend(primitive::QDATETIME.to_be_bytes().iter()); res.extend(unknown.to_be_bytes().iter()); @@ -165,42 +178,42 @@ impl Deserialize for Variant { let len = 5; match qtype { primitive::QVARIANTMAP => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: VariantMap"); + trace!(target: "primitive::Variant", "Parsing Variant: VariantMap"); let (vlen, value) = VariantMap::parse(&b[len..])?; return Ok((len + vlen, Variant::VariantMap(value))); } primitive::QVARIANTLIST => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: VariantList"); + trace!(target: "primitive::Variant", "Parsing Variant: VariantList"); let (vlen, value) = VariantList::parse(&b[len..])?; return Ok((len + vlen, Variant::VariantList(value))); } primitive::QSTRING => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: String"); + trace!(target: "primitive::Variant", "Parsing Variant: String"); let (vlen, value) = String::parse(&b[len..])?; return Ok((len + vlen, Variant::String(value.clone()))); } primitive::QBYTEARRAY => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: ByteArray"); + trace!(target: "primitive::Variant", "Parsing Variant: ByteArray"); let (vlen, value) = String::parse_utf8(&b[len..])?; return Ok((len + vlen, Variant::StringUTF8(value.clone()))); } primitive::QSTRINGLIST => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: StringList"); + trace!(target: "primitive::Variant", "Parsing Variant: StringList"); let (vlen, value) = StringList::parse(&b[len..])?; return Ok((len + vlen, Variant::StringList(value.clone()))); } primitive::QDATETIME => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: Date"); + trace!(target: "primitive::Variant", "Parsing Variant: Date"); let (vlen, value) = Date::parse(&b[len..])?; return Ok((len + vlen, Variant::Date(value.clone()))); } primitive::QDATE => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: Date"); + trace!(target: "primitive::Variant", "Parsing Variant: Date"); let (vlen, value) = Date::parse(&b[len..])?; return Ok((len + vlen, Variant::Date(value.clone()))); } primitive::QTIME => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: Time"); + trace!(target: "primitive::Variant", "Parsing Variant: Time"); let (vlen, value) = Time::parse(&b[len..])?; return Ok((len + vlen, Variant::Time(value.clone()))); } @@ -241,40 +254,41 @@ impl Deserialize for Variant { return Ok((len + vlen, Variant::i8(value))); } primitive::USERTYPE => { - trace!(target: "protocol::primitive::Variant", "Parsing UserType"); + trace!(target: "primitive::Variant", "Parsing UserType"); // Parse UserType name let (user_type_len, user_type) = String::parse_utf8(&b[len..])?; - trace!(target: "protocol::primitive::Variant", "Parsing UserType: {:?}", user_type); + trace!(target: "primitive::Variant", "Parsing UserType: {:?}", user_type); + // TODO implement all these types // Match Possible User Types to basic structures match user_type.as_str() { // As VariantMap "IrcUser" | "IrcChannel" | "Identity" | "NetworkInfo" | "Network::Server" => { - trace!(target: "protocol::primitive::Variant", "UserType is VariantMap"); + trace!(target: "primitive::Variant", "UserType is VariantMap"); let (vlen, value) = VariantMap::parse(&b[(len + user_type_len)..])?; return Ok((len + user_type_len + vlen, Variant::VariantMap(value))); } // As i32 "BufferId" | "IdentityId" | "NetworkId" | "MsgId" => { - trace!(target: "protocol::primitive::Variant", "UserType is i32"); + trace!(target: "primitive::Variant", "UserType is i32"); let (vlen, value) = i32::parse(&b[(len + user_type_len)..])?; return Ok((len + user_type_len + vlen, Variant::i32(value))); } // As i64 "PeerPtr" => { - trace!(target: "protocol::primitive::Variant", "UserType is i64"); + trace!(target: "primitive::Variant", "UserType is i64"); let (vlen, value) = i64::parse(&b[(len + user_type_len)..])?; return Ok((len + user_type_len + vlen, Variant::i64(value))); } "BufferInfo" => { - trace!(target: "protocol::primitive::Variant", "UserType is BufferInfo"); + trace!(target: "primitive::Variant", "UserType is BufferInfo"); let (vlen, value) = BufferInfo::parse(&b[(len + user_type_len)..])?; return Ok((len + user_type_len + vlen, Variant::BufferInfo(value))); } "Message" => { - trace!(target: "protocol::primitive::Variant", "UserType is Message"); + trace!(target: "primitive::Variant", "UserType is Message"); let (vlen, value) = Message::parse(&b[(len + user_type_len)..])?; return Ok((len + user_type_len + vlen, Variant::Message(value))); } diff --git a/src/protocol/primitive/variantlist.rs b/src/primitive/variantlist.rs index 2481b32..452b927 100644 --- a/src/protocol/primitive/variantlist.rs +++ b/src/primitive/variantlist.rs @@ -5,12 +5,15 @@ use failure::Error; use log::trace; -use crate::protocol::primitive::{deserialize::Deserialize, serialize::Serialize}; +use crate::{Deserialize, Serialize}; extern crate bytes; -use crate::protocol::primitive::Variant; +use crate::primitive::Variant; +/// VariantLists are represented as a Vec of Variants. +/// +/// They are serialized as the amount of entries as a i32 and then a Variant for each entry pub type VariantList = Vec<Variant>; impl Serialize for VariantList { @@ -30,12 +33,12 @@ impl Serialize for VariantList { impl Deserialize for VariantList { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::VariantList", "Parsing VariantList with {:?} elements", len); + trace!(target: "primitive::VariantList", "Parsing VariantList with {:?} elements", len); let mut res: VariantList = VariantList::new(); let mut pos: usize = 4; for i in 0..len { - trace!(target: "protocol::primitive::VariantList", "Parsing VariantList element: {:?}", i); + trace!(target: "primitive::VariantList", "Parsing VariantList element: {:?}", i); let (vlen, val) = Variant::parse(&b[pos..])?; res.push(val); pos += vlen; diff --git a/src/protocol/primitive/variantmap.rs b/src/primitive/variantmap.rs index 22ca0f1..4f017f3 100644 --- a/src/protocol/primitive/variantmap.rs +++ b/src/primitive/variantmap.rs @@ -5,16 +5,17 @@ use failure::Error; use log::trace; -use crate::protocol::error::ProtocolError; -use crate::protocol::primitive::deserialize::Deserialize; -use crate::protocol::primitive::serialize::Serialize; -use crate::protocol::primitive::String; +use crate::Deserialize; +use crate::Serialize; -use crate::protocol::primitive::Variant; +use crate::primitive::Variant; use crate::util; extern crate bytes; +/// VariantMaps are represented as a HashMap with String as key and Variant as value +/// +/// They are serialized as the amount of keys as an i32 then for each entry a String and a Variant. pub type VariantMap = HashMap<String, Variant>; impl Serialize for VariantMap { @@ -36,25 +37,19 @@ impl Serialize for VariantMap { impl Deserialize for VariantMap { fn parse(b: &[u8]) -> Result<(usize, Self), Error> { let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::VariantMap", "Parsing VariantMap with {:?} elements", len); + trace!(target: "primitive::VariantMap", "Parsing VariantMap with {:?} elements", len); let mut pos: usize = 4; let mut map = VariantMap::new(); for _ in 0..len { - trace!(target: "protocol::primitive::VariantMap", "Parsing entry name"); - // let (nlen, name) = Variant::parse(&b[pos..])?; + trace!(target: "primitive::VariantMap", "Parsing entry name"); let (nlen, name) = String::parse(&b[pos..])?; pos += nlen; - trace!(target: "protocol::primitive::VariantMap", "Parsing entry: {:?} with len {:?}", name, &b[(pos)..(pos + 4)]); + trace!(target: "primitive::VariantMap", "Parsing entry: {:?} with len {:?}", name, &b[(pos)..(pos + 4)]); let (vlen, value) = Variant::parse(&b[(pos)..])?; pos += vlen; - // match name { - // Variant::String(x) => map.insert(x, value), - // Variant::StringUTF8(x) => map.insert(x, value), - // _ => bail!(ProtocolError::WrongVariant), - // }; map.insert(name, value); } diff --git a/src/protocol/primitive/mod.rs b/src/protocol/primitive/mod.rs deleted file mode 100644 index 5656d71..0000000 --- a/src/protocol/primitive/mod.rs +++ /dev/null @@ -1,74 +0,0 @@ -pub mod bufferinfo; -pub mod datetime; -pub mod message; -pub mod signedint; -pub mod string; -pub mod stringlist; -pub mod unsignedint; -pub mod variant; -pub mod variantlist; -pub mod variantmap; - -pub use bufferinfo::*; -pub use datetime::*; -pub use message::*; -pub use signedint::*; -pub use string::*; -pub use stringlist::*; -pub use unsignedint::*; -pub use variant::*; -pub use variantlist::*; -pub use variantmap::*; - -// Static Type Definitions -pub const VOID: u32 = 0x00000000; -pub const BOOL: u32 = 0x00000001; -pub const QCHAR: u32 = 0x00000007; - -pub const QVARIANT: u32 = 0x00000090; -pub const QVARIANTMAP: u32 = 0x00000008; -pub const QVARIANTLIST: u32 = 0x00000009; - -pub const QSTRING: u32 = 0x0000000a; -pub const QSTRINGLIST: u32 = 0x0000000b; -pub const QBYTEARRAY: u32 = 0x0000000c; - -pub const QDATE: u32 = 0x0000000e; -pub const QTIME: u32 = 0x0000000f; -pub const QDATETIME: u32 = 0x00000010; -pub const USERTYPE: u32 = 0x0000007f; - -// Basic types -pub const LONG: u32 = 0x00000081; // int64_t -pub const INT: u32 = 0x00000002; // int32_t -pub const SHORT: u32 = 0x00000082; // int16_t -pub const CHAR: u32 = 0x00000083; // int8_t - -pub const ULONG: u32 = 0x00000084; // uint64_t -pub const UINT: u32 = 0x00000003; // uint32_t -pub const USHORT: u32 = 0x00000085; // uint16_t -pub const UCHAR: u32 = 0x00000086; // uint8_t - -pub mod serialize { - use failure::Error; - pub trait Serialize { - fn serialize(&self) -> Result<Vec<u8>, Error>; - } - pub trait SerializeUTF8 { - fn serialize_utf8(&self) -> Result<Vec<u8>, Error>; - } -} - -pub mod deserialize { - use failure::Error; - pub trait Deserialize { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> - where - Self: std::marker::Sized; - } - pub trait DeserializeUTF8 { - fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> - where - Self: std::marker::Sized; - } -} |
