Skip to content

Commit

Permalink
Removed deprecated Error::description and fixed warnings from clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jan 10, 2020
1 parent 998d5c5 commit 8c75f7d
Show file tree
Hide file tree
Showing 23 changed files with 505 additions and 459 deletions.
40 changes: 15 additions & 25 deletions src/control/fixed_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ pub struct FixedHeader {

impl FixedHeader {
pub fn new(packet_type: PacketType, remaining_length: u32) -> FixedHeader {
debug_assert!(remaining_length <= 0x0FFFFFFF);
debug_assert!(remaining_length <= 0x0FFF_FFFF);
FixedHeader {
packet_type: packet_type,
remaining_length: remaining_length,
packet_type,
remaining_length,
}
}

Expand Down Expand Up @@ -196,38 +196,28 @@ impl From<PacketTypeError> for FixedHeaderError {

impl fmt::Display for FixedHeaderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&FixedHeaderError::MalformedRemainingLength => write!(f, "Malformed remaining length"),
&FixedHeaderError::Unrecognized(code, length) => {
match *self {
FixedHeaderError::MalformedRemainingLength => write!(f, "Malformed remaining length"),
FixedHeaderError::Unrecognized(code, length) => {
write!(f, "Unrecognized header ({}, {})", code, length)
}
&FixedHeaderError::ReservedType(code, length) => {
FixedHeaderError::ReservedType(code, length) => {
write!(f, "Reserved header ({}, {})", code, length)
}
&FixedHeaderError::PacketTypeError(ref err) => write!(f, "{}", err),
&FixedHeaderError::IoError(ref err) => write!(f, "{}", err),
FixedHeaderError::PacketTypeError(ref err) => write!(f, "{}", err),
FixedHeaderError::IoError(ref err) => write!(f, "{}", err),
}
}
}

impl Error for FixedHeaderError {
fn description(&self) -> &str {
match self {
&FixedHeaderError::MalformedRemainingLength => "Malformed remaining length",
&FixedHeaderError::Unrecognized(..) => "Unrecognized header",
&FixedHeaderError::ReservedType(..) => "Unrecognized header",
&FixedHeaderError::PacketTypeError(ref err) => err.description(),
&FixedHeaderError::IoError(ref err) => err.description(),
}
}

fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
&FixedHeaderError::MalformedRemainingLength => None,
&FixedHeaderError::Unrecognized(..) => None,
&FixedHeaderError::ReservedType(..) => None,
&FixedHeaderError::PacketTypeError(ref err) => Some(err),
&FixedHeaderError::IoError(ref err) => Some(err),
match *self {
FixedHeaderError::MalformedRemainingLength => None,
FixedHeaderError::Unrecognized(..) => None,
FixedHeaderError::ReservedType(..) => None,
FixedHeaderError::PacketTypeError(ref err) => Some(err),
FixedHeaderError::IoError(ref err) => Some(err),
}
}
}
Expand Down
34 changes: 16 additions & 18 deletions src/control/packet_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct PacketType {
}

/// Defined control types
#[cfg_attr(rustfmt, rustfmt_skip)]
#[rustfmt::skip]
#[repr(u8)]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum ControlType {
Expand Down Expand Up @@ -64,7 +64,7 @@ impl PacketType {
pub fn new(t: ControlType, flags: u8) -> PacketType {
PacketType {
control_type: t,
flags: flags,
flags,
}
}

Expand Down Expand Up @@ -97,12 +97,12 @@ impl PacketType {
}

/// To code
pub fn to_u8(&self) -> u8 {
pub fn to_u8(self) -> u8 {
(self.control_type as u8) << 4 | (self.flags & 0x0F)
}

/// From code
#[cfg_attr(rustfmt, rustfmt_skip)]
#[rustfmt::skip]
pub fn from_u8(val: u8) -> Result<PacketType, PacketTypeError> {

let type_val = val >> 4;
Expand Down Expand Up @@ -156,25 +156,23 @@ pub enum PacketTypeError {

impl fmt::Display for PacketTypeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&PacketTypeError::ReservedType(t, flag) => write!(f, "Reserved type {:?} ({:#X})", t, flag),
&PacketTypeError::InvalidFlag(t, flag) => write!(f, "Invalid flag for {:?} ({:#X})", t, flag),
&PacketTypeError::UndefinedType(t, flag) => write!(f, "Undefined type {:?} ({:#X})", t, flag),
match *self {
PacketTypeError::ReservedType(t, flag) => {
write!(f, "Reserved type {:?} ({:#X})", t, flag)
}
PacketTypeError::InvalidFlag(t, flag) => {
write!(f, "Invalid flag for {:?} ({:#X})", t, flag)
}
PacketTypeError::UndefinedType(t, flag) => {
write!(f, "Undefined type {:?} ({:#X})", t, flag)
}
}
}
}

impl Error for PacketTypeError {
fn description(&self) -> &str {
match self {
&PacketTypeError::ReservedType(..) => "Reserved type",
&PacketTypeError::UndefinedType(..) => "Undefined type",
&PacketTypeError::InvalidFlag(..) => "Invalid flag",
}
}
}
impl Error for PacketTypeError {}

#[cfg_attr(rustfmt, rustfmt_skip)]
#[rustfmt::skip]
mod value {
pub const CONNECT: u8 = 1;
pub const CONNACK: u8 = 2;
Expand Down
23 changes: 13 additions & 10 deletions src/control/variable_header/connect_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::io::{Read, Write};

use byteorder::{ReadBytesExt, WriteBytesExt};

use crate::{Decodable, Encodable};
use crate::control::variable_header::VariableHeaderError;
use crate::{Decodable, Encodable};

/// Flags for `CONNECT` packet
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
Expand Down Expand Up @@ -33,7 +33,7 @@ impl ConnectFlags {
impl Encodable for ConnectFlags {
type Err = VariableHeaderError;

#[cfg_attr(rustfmt, rustfmt_skip)]
#[rustfmt::skip]
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), VariableHeaderError> {
let code = ((self.user_name as u8) << 7)
| ((self.password as u8) << 6)
Expand All @@ -54,19 +54,22 @@ impl Decodable for ConnectFlags {
type Err = VariableHeaderError;
type Cond = ();

fn decode_with<R: Read>(reader: &mut R, _rest: Option<()>) -> Result<ConnectFlags, VariableHeaderError> {
fn decode_with<R: Read>(
reader: &mut R,
_rest: Option<()>,
) -> Result<ConnectFlags, VariableHeaderError> {
let code = reader.read_u8()?;
if code & 1 != 0 {
return Err(VariableHeaderError::InvalidReservedFlag);
}

Ok(ConnectFlags {
user_name: (code & 0b1000_0000) != 0,
password: (code & 0b0100_0000) != 0,
will_retain: (code & 0b0010_0000) != 0,
will_qos: (code & 0b0001_1000) >> 3,
will_flag: (code & 0b0000_0100) != 0,
clean_session: (code & 0b0000_0010) != 0,
})
user_name: (code & 0b1000_0000) != 0,
password: (code & 0b0100_0000) != 0,
will_retain: (code & 0b0010_0000) != 0,
will_qos: (code & 0b0001_1000) >> 3,
will_flag: (code & 0b0000_0100) != 0,
clean_session: (code & 0b0000_0010) != 0,
})
}
}
18 changes: 11 additions & 7 deletions src/control/variable_header/connect_ret_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::io::{Read, Write};

use byteorder::{ReadBytesExt, WriteBytesExt};

use crate::{Decodable, Encodable};
use crate::control::variable_header::VariableHeaderError;
use crate::{Decodable, Encodable};

pub const CONNECTION_ACCEPTED: u8 = 0x00;
pub const UNACCEPTABLE_PROTOCOL_VERSION: u8 = 0x01;
Expand All @@ -27,8 +27,8 @@ pub enum ConnectReturnCode {

impl ConnectReturnCode {
/// Get the code
pub fn to_u8(&self) -> u8 {
match *self {
pub fn to_u8(self) -> u8 {
match self {
ConnectReturnCode::ConnectionAccepted => CONNECTION_ACCEPTED,
ConnectReturnCode::UnacceptableProtocolVersion => UNACCEPTABLE_PROTOCOL_VERSION,
ConnectReturnCode::IdentifierRejected => IDENTIFIER_REJECTED,
Expand Down Expand Up @@ -69,9 +69,13 @@ impl Decodable for ConnectReturnCode {
type Err = VariableHeaderError;
type Cond = ();

fn decode_with<R: Read>(reader: &mut R, _rest: Option<()>) -> Result<ConnectReturnCode, VariableHeaderError> {
reader.read_u8()
.map(ConnectReturnCode::from_u8)
.map_err(From::from)
fn decode_with<R: Read>(
reader: &mut R,
_rest: Option<()>,
) -> Result<ConnectReturnCode, VariableHeaderError> {
reader
.read_u8()
.map(ConnectReturnCode::from_u8)
.map_err(From::from)
}
}
44 changes: 17 additions & 27 deletions src/control/variable_header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pub use self::protocol_level::ProtocolLevel;
pub use self::protocol_name::ProtocolName;
pub use self::topic_name::TopicNameHeader;

mod packet_identifier;
mod protocol_name;
pub mod protocol_level;
mod connect_flags;
mod keep_alive;
mod connect_ack_flags;
mod connect_flags;
mod connect_ret_code;
mod keep_alive;
mod packet_identifier;
pub mod protocol_level;
mod protocol_name;
mod topic_name;

/// Errors while decoding variable header
Expand Down Expand Up @@ -63,34 +63,24 @@ impl From<TopicNameError> for VariableHeaderError {

impl fmt::Display for VariableHeaderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&VariableHeaderError::IoError(ref err) => write!(f, "{}", err),
&VariableHeaderError::StringEncodeError(ref err) => write!(f, "{}", err),
&VariableHeaderError::InvalidReservedFlag => write!(f, "Invalid reserved flags"),
&VariableHeaderError::FromUtf8Error(ref err) => write!(f, "{}", err),
&VariableHeaderError::TopicNameError(ref err) => write!(f, "{}", err),
match *self {
VariableHeaderError::IoError(ref err) => write!(f, "{}", err),
VariableHeaderError::StringEncodeError(ref err) => write!(f, "{}", err),
VariableHeaderError::InvalidReservedFlag => write!(f, "Invalid reserved flags"),
VariableHeaderError::FromUtf8Error(ref err) => write!(f, "{}", err),
VariableHeaderError::TopicNameError(ref err) => write!(f, "{}", err),
}
}
}

impl Error for VariableHeaderError {
fn description(&self) -> &str {
match self {
&VariableHeaderError::IoError(ref err) => err.description(),
&VariableHeaderError::StringEncodeError(ref err) => err.description(),
&VariableHeaderError::InvalidReservedFlag => "Invalid reserved flags",
&VariableHeaderError::FromUtf8Error(ref err) => err.description(),
&VariableHeaderError::TopicNameError(ref err) => err.description(),
}
}

fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
&VariableHeaderError::IoError(ref err) => Some(err),
&VariableHeaderError::StringEncodeError(ref err) => Some(err),
&VariableHeaderError::InvalidReservedFlag => None,
&VariableHeaderError::FromUtf8Error(ref err) => Some(err),
&VariableHeaderError::TopicNameError(ref err) => Some(err),
match *self {
VariableHeaderError::IoError(ref err) => Some(err),
VariableHeaderError::StringEncodeError(ref err) => Some(err),
VariableHeaderError::InvalidReservedFlag => None,
VariableHeaderError::FromUtf8Error(ref err) => Some(err),
VariableHeaderError::TopicNameError(ref err) => Some(err),
}
}
}
38 changes: 17 additions & 21 deletions src/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ impl<'a> Encodable for &'a str {
fn encode<W: Write>(&self, writer: &mut W) -> Result<(), StringEncodeError> {
assert!(self.as_bytes().len() <= u16::max_value() as usize);

writer.write_u16::<BigEndian>(self.as_bytes().len() as u16)
.map_err(From::from)
.and_then(|_| writer.write_all(self.as_bytes()))
.map_err(StringEncodeError::IoError)
writer
.write_u16::<BigEndian>(self.as_bytes().len() as u16)
.map_err(From::from)
.and_then(|_| writer.write_all(self.as_bytes()))
.map_err(StringEncodeError::IoError)
}

fn encoded_length(&self) -> u32 {
Expand Down Expand Up @@ -78,7 +79,10 @@ impl Decodable for String {
type Err = StringEncodeError;
type Cond = ();

fn decode_with<R: Read>(reader: &mut R, _rest: Option<()>) -> Result<String, StringEncodeError> {
fn decode_with<R: Read>(
reader: &mut R,
_rest: Option<()>,
) -> Result<String, StringEncodeError> {
let len = reader.read_u16::<BigEndian>()? as usize;
let mut buf = Vec::with_capacity(len);
unsafe {
Expand Down Expand Up @@ -205,28 +209,20 @@ pub enum StringEncodeError {

impl fmt::Display for StringEncodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&StringEncodeError::IoError(ref err) => err.fmt(f),
&StringEncodeError::FromUtf8Error(ref err) => err.fmt(f),
&StringEncodeError::MalformedData => write!(f, "Malformed data"),
match *self {
StringEncodeError::IoError(ref err) => err.fmt(f),
StringEncodeError::FromUtf8Error(ref err) => err.fmt(f),
StringEncodeError::MalformedData => write!(f, "Malformed data"),
}
}
}

impl Error for StringEncodeError {
fn description(&self) -> &str {
match self {
&StringEncodeError::IoError(ref err) => err.description(),
&StringEncodeError::FromUtf8Error(ref err) => err.description(),
&StringEncodeError::MalformedData => "Malformed data",
}
}

fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
&StringEncodeError::IoError(ref err) => Some(err),
&StringEncodeError::FromUtf8Error(ref err) => Some(err),
&StringEncodeError::MalformedData => None,
match *self {
StringEncodeError::IoError(ref err) => Some(err),
StringEncodeError::FromUtf8Error(ref err) => Some(err),
StringEncodeError::MalformedData => None,
}
}
}
Expand Down
Loading

0 comments on commit 8c75f7d

Please sign in to comment.