From 4ec62bbac872bbfd95413af2f00c3f4a36c70630 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 4 Apr 2020 13:48:56 +0200 Subject: switch from quick_error to thiserror thiserror seems to be the more modern approach that also works with the new Error trait from std. --- Cargo.toml | 2 +- src/lib.rs | 24 +++++++++-------------- src/raw/parser.rs | 52 +++++++++++++++++++------------------------------- src/statistics/math.rs | 8 +++++++- src/statistics/mod.rs | 15 +++++---------- 5 files changed, 42 insertions(+), 59 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f4697a2..b6c1c90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] num-traits = "0.2" num-derive = "0.3" -quick-error = "1.2.1" +thiserror = "1.0" byteorder = "1" fnv = "1.0.3" getset = "0.1" diff --git a/src/lib.rs b/src/lib.rs index 6f3fc49..931ee61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,11 +18,12 @@ //! module](raw/parser/index.html#buffering)) #![feature(try_trait, stmt_expr_attributes, never_type)] #[macro_use] -extern crate quick_error; -#[macro_use] extern crate num_derive; #[macro_use] extern crate getset; + +use thiserror::Error; + pub mod raw; mod event; @@ -55,19 +56,12 @@ macro_rules! matches { ); } -quick_error! { - #[derive(Debug)] - pub enum EvtcError { - InvalidData { - description("invalid data has been provided") - } - Utf8Error(err: ::std::string::FromUtf8Error) { - from() - description("utf8 decoding error") - display("UTF-8 decoding error: {}", err) - cause(err) - } - } +#[derive(Error, Debug)] +pub enum EvtcError { + #[error("invalid data has been provided")] + InvalidData, + #[error("utf8 decoding error: {0}")] + Utf8Error(#[from] std::string::FromUtf8Error), } /// The type of an agent. diff --git a/src/raw/parser.rs b/src/raw/parser.rs index fa3b36c..0e91f78 100644 --- a/src/raw/parser.rs +++ b/src/raw/parser.rs @@ -65,6 +65,7 @@ use byteorder::{LittleEndian, ReadBytesExt, LE}; use num_traits::FromPrimitive; +use thiserror::Error; use std::io::{self, ErrorKind, Read}; use super::*; @@ -107,38 +108,25 @@ pub struct PartialEvtc { pub skills: Vec, } -quick_error! { - #[derive(Debug)] - pub enum ParseError { - Io(err: io::Error) { - from() - description("io error") - display("I/O error: {}", err) - cause(err) - } - Utf8Error(err: ::std::string::FromUtf8Error) { - from() - description("utf8 decoding error") - display("UTF-8 decoding error: {}", err) - cause(err) - } - InvalidData { - from(::std::option::NoneError) - description("invalid data") - } - MalformedHeader { - description("malformed header") - } - UnknownRevision(rev: u8) { - description("unknown revision") - display("revision number not known: {}", rev) - } - InvalidZip(err: ::zip::result::ZipError) { - from() - description("zip error") - display("Archive error: {}", err) - cause(err) - } +#[derive(Error, Debug)] +pub enum ParseError { + #[error("IO error: {0}")] + Io(#[from] io::Error), + #[error("utf8 decoding error: {0}")] + Utf8Error(#[from] std::string::FromUtf8Error), + #[error("invalid data")] + InvalidData, + #[error("malformed header")] + MalformedHeader, + #[error("unknown revision: {0}")] + UnknownRevision(u8), + #[error("invalid archive: {0}")] + InvalidZip(#[from] zip::result::ZipError), +} + +impl From for ParseError { + fn from(_: std::option::NoneError) -> Self { + ParseError::InvalidData } } diff --git a/src/statistics/math.rs b/src/statistics/math.rs index b7dd6ac..a0849a3 100644 --- a/src/statistics/math.rs +++ b/src/statistics/math.rs @@ -25,7 +25,8 @@ pub trait Monoid: Semigroup { } #[derive(Debug, Clone)] -struct Record { +#[doc(hidden)] +pub struct Record { x: X, tag: T, data: D, @@ -63,6 +64,11 @@ where RecordFunc { data: Vec::new() } } + #[doc(hidden)] + pub fn data(&self) -> &[Record] { + &self.data + } + /// Insert a data point into the record func. /// /// Note that you should supply the *increment*, not the *absolute value*! diff --git a/src/statistics/mod.rs b/src/statistics/mod.rs index eccc3ee..3e42d9c 100644 --- a/src/statistics/mod.rs +++ b/src/statistics/mod.rs @@ -1,7 +1,7 @@ //! This module aids in the creation of actual boss statistics. use super::*; use std::collections::HashMap; -use std::error::Error; +use thiserror::Error; pub mod boon; pub mod damage; @@ -17,15 +17,10 @@ use self::trackers::{RunnableTracker, Tracker}; pub type StatResult = Result; -quick_error! { - #[derive(Debug)] - pub enum StatError { - TrackerError(err: Box) { - description("tracker error") - display("tracker returned an error: {}", err) - cause(&**err) - } - } +#[derive(Error, Debug)] +pub enum StatError { + #[error("tracker returned an error: {0}")] + TrackerError(#[from] Box), } macro_rules! try_tracker { -- cgit v1.2.3