From 6895cde14ab237e07869b47166f41d8df2e251e4 Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Wed, 2 Jan 2019 21:45:44 -0500 Subject: [PATCH] derive EBML errors via custom_error!{} --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/ebml.rs | 48 ++++++++---------------------------------------- 3 files changed, 16 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e43db3f..4a9358a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -149,6 +149,11 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "custom_error" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "digest" version = "0.8.1" @@ -1197,6 +1202,7 @@ version = "0.2.0" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "custom_error 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.31 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1265,6 +1271,7 @@ dependencies = [ "checksum crossbeam-epoch 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "04c9e3102cc2d69cd681412141b390abd55a362afc1540965dad0ad4d34280b4" "checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b" "checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" +"checksum custom_error 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c189751d60563837f66c7ebf87f6a6e4c101e937d54521e88620e89c79b8a2ad" "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e" "checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b" diff --git a/Cargo.toml b/Cargo.toml index 8f42920..e6624a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] bytes = "0.4.12" clap = "2.33.0" +custom_error = "1.3.0" futures = "0.1.28" http = "0.1.17" hyper = "0.12.31" diff --git a/src/ebml.rs b/src/ebml.rs index 2136e00..a896ec2 100644 --- a/src/ebml.rs +++ b/src/ebml.rs @@ -1,6 +1,5 @@ use bytes::{BigEndian, ByteOrder, BufMut}; -use std::error::Error as ErrorTrait; -use std::fmt::{Display, Formatter, Result as FmtResult}; +use custom_error::custom_error; use std::io::{Cursor, Error as IoError, ErrorKind, Result as IoResult, Write, Seek, SeekFrom}; use futures::Async; @@ -8,46 +7,15 @@ pub const EBML_HEAD_ID: u64 = 0x0A45DFA3; pub const DOC_TYPE_ID: u64 = 0x0282; pub const VOID_ID: u64 = 0x6C; -#[derive(Debug, PartialEq)] -pub enum EbmlError { - CorruptVarint, - UnknownElementId, - UnknownElementLength, - CorruptPayload, -} -impl Display for EbmlError { - fn fmt(&self, f: &mut Formatter) -> FmtResult { - write!(f, "{}", self.description()) - } -} -impl ErrorTrait for EbmlError { - fn description(&self) -> &str { - match self { - &EbmlError::CorruptVarint => "EBML Varint could not be parsed", - &EbmlError::UnknownElementId => "EBML element ID was \"unknown\"", - &EbmlError::UnknownElementLength => "EBML element length was \"unknown\" for an element not allowing that", - &EbmlError::CorruptPayload => "EBML element payload could not be parsed", - } - } +custom_error!{pub EbmlError + CorruptVarint = r#"EBML Varint could not be parsed"#, + UnknownElementId = r#"EBML element ID was "unknown"#, + UnknownElementLength = r#"EBML element length was "unknown" for an element not allowing that"#, + CorruptPayload = r#"EBML element payload could not be parsed"#, } -#[derive(Debug, PartialEq)] -pub enum WriteError { - OutOfRange -} -impl Display for WriteError { - fn fmt(&self, f: &mut Formatter) -> FmtResult { - match self { - &WriteError::OutOfRange => write!(f, "EBML Varint out of range") - } - } -} -impl ErrorTrait for WriteError { - fn description(&self) -> &str { - match self { - &WriteError::OutOfRange => "EBML Varint out of range" - } - } +custom_error!{pub WriteError + OutOfRange = "EBML Varint out of range" } #[derive(Debug, PartialEq)]