Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove error_chain crate, Remove the use of unwrap, Update to 2018 edition #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "protocol"
version = "3.1.2"
authors = ["Dylan McKay <[email protected]>"]
edition = "2018"

description = "Easy protocol definitions"
license = "MIT"
Expand All @@ -17,7 +18,6 @@ default = ["uuid"]
byteorder = "1.3"
flate2 = { version = "1.0", features = ["zlib"], default-features = false }
uuid = { version = "0.7", optional = true }
error-chain = "0.12"
num-traits = "0.2"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/enum_ty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Parcel;
use crate::Parcel;

/// An `enum` type.
pub trait Enum : Parcel {
Expand Down
72 changes: 50 additions & 22 deletions protocol/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use std::{self, fmt, error};

macro_rules! from_error {
($f: ty, $e: expr) => {
impl From<$f> for Error {
fn from(f: $f) -> Error {
$e(f)
}
}
};
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
/// Copy of [TryFromIntError](https://doc.rust-lang.org/std/num/struct.TryFromIntError.html)
Expand Down Expand Up @@ -47,31 +56,50 @@ impl error::Error for CharTryFromError {
}
}

error_chain! {
types {
Error, ErrorKind, ResultExt;
}

foreign_links {
Io(std::io::Error);
FromUtf8(std::string::FromUtf8Error);
TryFromIntError(TryFromIntError);
CharTryFromError(CharTryFromError);
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
FromUtf8(std::string::FromUtf8Error),
TryFromIntError(TryFromIntError),
CharTryFromError(CharTryFromError),
#[cfg(feature = "uuid")]
UuidParseError(::uuid::parser::ParseError),
UnknownPacketId,
UnimplementedParcel(&'static str),
}

UuidParseError(::uuid::parser::ParseError) #[cfg(feature = "uuid")];
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Io(ref err) => writeln!(fmt, "{}", err),
Error::FromUtf8(ref err) => writeln!(fmt, "{}", err),
Error::TryFromIntError(ref err) => writeln!(fmt, "{}", err),
Error::CharTryFromError(ref err) => writeln!(fmt, "{}", err),
#[cfg(feature = "uuid")]
Error::UuidParseError(ref err) => writeln!(fmt, "{}", err),
Error::UnknownPacketId => writeln!(fmt, "unknown packet identifier"),
Error::UnimplementedParcel(ref err) => writeln!(fmt, "unimplemented parcel type: {}", err),
}
}
}

errors {
UnknownPacketId {
description("unknown packet identifier")
display("unknown packet identifier")
}
from_error!(std::io::Error, Error::Io);
from_error!(std::string::FromUtf8Error, Error::FromUtf8);
from_error!(TryFromIntError, Error::TryFromIntError);
from_error!(CharTryFromError, Error::CharTryFromError);
#[cfg(feature = "uuid")]
from_error!(::uuid::parser::ParseError, Error::UuidParseError);

/// A parcel type was read that has not been implemented yet.
UnimplementedParcel(type_name: &'static str) {
description("unimplemented parcel")
display("unimplemented parcel type '{}", type_name)
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Error::Io(ref err) => Some(err),
Error::FromUtf8(ref err) => Some(err),
Error::TryFromIntError(ref err) => Some(err),
Error::CharTryFromError(ref err) => Some(err),
#[cfg(feature = "uuid")]
Error::UuidParseError(ref err) => Some(err),
_ => None,
}
}
}

}
8 changes: 4 additions & 4 deletions protocol/src/high_level.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Error, Parcel, Settings};
use hint;
use crate::{Error, Parcel, Settings};
use crate::hint;
use std::io::prelude::*;
use std::fmt;

Expand Down Expand Up @@ -58,10 +58,10 @@ use std::fmt;
/// _: &mut protocol::hint::Hints)
/// -> Result<Self, protocol::Error> {
/// match low_level.opcode {
/// 0 => Ok(Login::Success { message: String::from_utf8(low_level.payload).unwrap() }),
/// 0 => Ok(Login::Success { message: String::from_utf8(low_level.payload)? }),
/// 1 => Ok(Login::Failure {
/// code: FailureCode::MyDogAteMyHomework,
/// response: String::from_utf8(low_level.payload[1..].to_owned()).unwrap() }),
/// response: String::from_utf8(low_level.payload[1..].to_owned())? }),
/// _ => unreachable!(),
/// }
/// }
Expand Down
18 changes: 6 additions & 12 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@
//!
//! ```

pub use self::enum_ty::Enum;
pub use self::parcel::Parcel;
pub use self::errors::{Error, ErrorKind, ResultExt, CharTryFromError, TryFromIntError};
pub use self::high_level::HighLevel;
pub use self::settings::*;
pub use crate::enum_ty::Enum;
pub use crate::parcel::Parcel;
pub use crate::errors::{Error, CharTryFromError, TryFromIntError};
pub use crate::high_level::HighLevel;
pub use crate::settings::*;

mod settings;
#[macro_use]
Expand All @@ -181,15 +181,9 @@ pub mod logic;
mod parcel;
pub mod util;


extern crate byteorder;
extern crate flate2;
#[macro_use]
extern crate error_chain;

#[cfg(feature = "uuid")]
extern crate uuid;
extern crate num_traits;


/// The default byte ordering.
pub type DefaultByteOrder = ::byteorder::BigEndian;
Expand Down
6 changes: 3 additions & 3 deletions protocol/src/logic/aligned.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Error, Parcel, Settings};
use hint;
use crate::{Error, Parcel, Settings};
use crate::hint;
use std::io::prelude::*;
use std::{marker, mem};

Expand Down Expand Up @@ -87,7 +87,7 @@ impl<T, ToSizeOfType> Parcel for Aligned<T, ToSizeOfType>
settings: &Settings,
hints: &mut hint::Hints) -> Result<Self, Error> {
let inner_value = T::read_field(read, settings, hints)?;
let value_size = inner_value.raw_bytes_field(settings, hints).unwrap().len();
let value_size = inner_value.raw_bytes_field(settings, hints)?.len();
let padding_size = calculate_padding(Self::align_to_bytes(), value_size);

for _ in 0..padding_size {
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/logic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `Parcel` type wrappers that implement complex logic.

pub use self::aligned::Aligned;
pub use crate::logic::aligned::Aligned;

mod aligned;

4 changes: 2 additions & 2 deletions protocol/src/parcel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Error, Settings};
use hint;
use crate::{Error, Settings};
use crate::hint;
use std::io::prelude::*;
use std::io;

Expand Down
4 changes: 2 additions & 2 deletions protocol/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ macro_rules! impl_byte_order_helpers {
( $( $ty:ty => [ $read_name:ident : $write_name:ident ] )* ) => {
impl ByteOrder {
$(
pub fn $read_name(&self, read: &mut Read) -> Result<$ty, ::Error> {
pub fn $read_name(&self, read: &mut Read) -> Result<$ty, crate::Error> {
use byteorder as bo;

Ok(match *self {
Expand All @@ -78,7 +78,7 @@ macro_rules! impl_byte_order_helpers {
}

pub fn $write_name(&self, value: $ty,
write: &mut Write) -> Result<(), ::Error> {
write: &mut Write) -> Result<(), crate::Error> {
use byteorder as bo;

Ok(match *self {
Expand Down
6 changes: 3 additions & 3 deletions protocol/src/types/array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, Settings};
use {hint, util};
use crate::{Parcel, Error, Settings};
use crate::{hint, util};
use std::io::prelude::*;

macro_rules! impl_parcel_for_array {
Expand Down Expand Up @@ -78,7 +78,7 @@ impl_parcel_for_array!(0xffff);

#[cfg(test)]
mod test {
use {Parcel, Settings};
use crate::{Parcel, Settings};
use std::io::Cursor;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/char.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, CharTryFromError, Settings};
use hint;
use crate::{Parcel, Error, CharTryFromError, Settings};
use crate::hint;
use std::char;
use std::io::prelude::*;

Expand Down
14 changes: 7 additions & 7 deletions protocol/src/types/collections/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ macro_rules! impl_list_type {
const TYPE_NAME: &'static str = stringify!($ty<T>);

fn read_field(read: &mut ::std::io::Read,
settings: &::Settings,
hints: &mut ::hint::Hints) -> Result<Self, $crate::Error> {
let elements = ::util::read_list(read, settings, hints)?;
settings: &crate::Settings,
hints: &mut crate::hint::Hints) -> Result<Self, $crate::Error> {
let elements = crate::util::read_list(read, settings, hints)?;
Ok(elements.into_iter().collect())
}

fn write_field(&self, write: &mut ::std::io::Write,
settings: &::Settings,
hints: &mut ::hint::Hints)
settings: &crate::Settings,
hints: &mut crate::hint::Hints)
-> Result<(), $crate::Error> {
::util::write_list(self.iter(), write, settings, hints)
crate::util::write_list(self.iter(), write, settings, hints)
}
}

#[cfg(test)]
mod test
{
pub use {Parcel, Settings};
pub use crate::{Parcel, Settings};
pub use std::collections::$ty;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/collections/map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, Settings};
use hint;
use crate::{Parcel, Error, Settings};
use crate::hint;

use std::collections::{HashMap, BTreeMap};
use std::hash::Hash;
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/marker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, Settings};
use hint;
use crate::{Parcel, Error, Settings};
use crate::hint;
use std::marker::PhantomData;

use std::io::prelude::*;
Expand Down
8 changes: 3 additions & 5 deletions protocol/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! Contains newtypes over the standard library types
//! that support finer-grained serialization settings.

pub use self::numerics::Integer;
pub use self::string::String;
pub use self::unimplemented::Unimplemented;
pub use self::vec::Vec;

pub use crate::types::{
numerics::Integer, string::String, unimplemented::Unimplemented, vec::Vec
};
mod array;
mod char;
/// Definitions for the `std::collections` module.
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/numerics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, Settings};
use hint;
use crate::{Parcel, Error, Settings};
use crate::hint;

use std::io::prelude::*;

Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/option.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, Settings};
use hint;
use crate::{Parcel, Error, Settings};
use crate::hint;

use std::io::prelude::*;

Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/smart_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Settings, Error};
use hint;
use crate::{Parcel, Settings, Error};
use crate::hint;

use std::rc::Rc;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/types/string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {hint, types, util, Parcel, Error, Settings};
use crate::{hint, types, util, Parcel, Error, Settings};
use std::io::prelude::*;
use std;

Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, Settings};
use hint;
use crate::{Parcel, Error, Settings};
use crate::hint;

use std::io::prelude::*;

Expand Down
6 changes: 3 additions & 3 deletions protocol/src/types/unimplemented.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, ErrorKind, Settings};
use hint;
use crate::{Parcel, Error, Settings};
use crate::hint;

use std::io::prelude::*;

Expand All @@ -25,7 +25,7 @@ impl Parcel for Unimplemented
fn read_field(_: &mut Read,
_: &Settings,
_: &mut hint::Hints) -> Result<Self, Error> {
Err(ErrorKind::UnimplementedParcel(Self::TYPE_NAME).into())
Err(Error::UnimplementedParcel(Self::TYPE_NAME).into())
}

fn write_field(&self,
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/uuid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, Settings};
use hint;
use crate::{Parcel, Error, Settings};
use crate::hint;
use std::io::prelude::*;

use uuid::Uuid;
Expand Down
8 changes: 4 additions & 4 deletions protocol/src/types/vec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {Parcel, Error, Settings};
use {hint, types, util};
use crate::{Parcel, Error, Settings};
use crate::{hint, types, util};
use std::io::prelude::*;
use std;

Expand Down Expand Up @@ -40,8 +40,8 @@ impl<S: types::Integer, T: Parcel> Parcel for Vec<S, T>

/// Stuff relating to `std::vec::Vec<T>`.
mod std_vec {
use {Error, Parcel, Settings};
use {hint, util};
use crate::{Error, Parcel, Settings};
use crate::{hint, util};
use std::io::prelude::*;

impl<T: Parcel> Parcel for Vec<T>
Expand Down
Loading