diff --git a/Cargo.toml b/Cargo.toml index 5acaa06..77f24b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ include = [ travis-ci = { repository = "greyblake/ta-rs", branch = "master" } [dependencies] -error-chain = "0.12" serde = { version = "1.0", features = ["derive"], optional = true} [dev-dependencies] diff --git a/src/data_item.rs b/src/data_item.rs index 201e2ea..e267059 100644 --- a/src/data_item.rs +++ b/src/data_item.rs @@ -140,10 +140,10 @@ impl DataItemBuilder { }; Ok(item) } else { - Err(Error::from_kind(ErrorKind::DataItemInvalid)) + Err(TaError::DataItemInvalid) } } else { - Err(Error::from_kind(ErrorKind::DataItemIncomplete)) + Err(TaError::DataItemIncomplete) } } } diff --git a/src/errors.rs b/src/errors.rs index 31c9e98..8aa8e5d 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,7 +1,31 @@ -error_chain! { - errors { - InvalidParameter { description("invalid parameter") } - DataItemIncomplete { description("data item is incomplete") } - DataItemInvalid { description("data item is invalid") } +use std::error::Error; +use std::fmt::{Display, Formatter}; + +pub type Result = std::result::Result; + +#[derive(Debug)] +pub enum TaError { + InvalidParameter, + DataItemIncomplete, + DataItemInvalid, +} + +impl Display for TaError { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + match *self { + TaError::InvalidParameter => write!(f, "invalid parameter"), + TaError::DataItemIncomplete => write!(f, "data item is incomplete"), + TaError::DataItemInvalid => write!(f, "data item is invalid"), + } + } +} + +impl Error for TaError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match *self { + TaError::InvalidParameter => None, + TaError::DataItemIncomplete => None, + TaError::DataItemInvalid => None, + } } } diff --git a/src/indicators/efficiency_ratio.rs b/src/indicators/efficiency_ratio.rs index 1f77c06..8ad5b50 100644 --- a/src/indicators/efficiency_ratio.rs +++ b/src/indicators/efficiency_ratio.rs @@ -1,6 +1,6 @@ use std::fmt; -use crate::errors::{Error, ErrorKind, Result}; +use crate::errors::{Result, TaError}; use crate::traits::{Close, Next, Period, Reset}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -42,7 +42,7 @@ pub struct EfficiencyRatio { impl EfficiencyRatio { pub fn new(period: usize) -> Result { match period { - 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)), + 0 => Err(TaError::InvalidParameter), _ => Ok(Self { period, index: 0, diff --git a/src/indicators/exponential_moving_average.rs b/src/indicators/exponential_moving_average.rs index 90be1c0..3cfa1a3 100644 --- a/src/indicators/exponential_moving_average.rs +++ b/src/indicators/exponential_moving_average.rs @@ -1,6 +1,6 @@ use std::fmt; -use crate::errors::{Error, ErrorKind, Result}; +use crate::errors::{Result, TaError}; use crate::{Close, Next, Period, Reset}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -65,7 +65,7 @@ pub struct ExponentialMovingAverage { impl ExponentialMovingAverage { pub fn new(period: usize) -> Result { match period { - 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)), + 0 => Err(TaError::InvalidParameter), _ => Ok(Self { period, k: 2.0 / (period + 1) as f64, diff --git a/src/indicators/maximum.rs b/src/indicators/maximum.rs index fee47cf..d7a6360 100644 --- a/src/indicators/maximum.rs +++ b/src/indicators/maximum.rs @@ -1,7 +1,7 @@ use std::f64::INFINITY; use std::fmt; -use crate::errors::{Error, ErrorKind, Result}; +use crate::errors::{Result, TaError}; use crate::{High, Next, Period, Reset}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -37,7 +37,7 @@ pub struct Maximum { impl Maximum { pub fn new(period: usize) -> Result { match period { - 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)), + 0 => Err(TaError::InvalidParameter), _ => Ok(Self { period, max_index: 0, diff --git a/src/indicators/minimum.rs b/src/indicators/minimum.rs index df101f2..61047f6 100644 --- a/src/indicators/minimum.rs +++ b/src/indicators/minimum.rs @@ -1,7 +1,7 @@ use std::f64::INFINITY; use std::fmt; -use crate::errors::{Error, ErrorKind, Result}; +use crate::errors::{Result, TaError}; use crate::{Low, Next, Period, Reset}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -36,7 +36,7 @@ pub struct Minimum { impl Minimum { pub fn new(period: usize) -> Result { match period { - 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)), + 0 => Err(TaError::InvalidParameter), _ => Ok(Self { period, min_index: 0, diff --git a/src/indicators/money_flow_index.rs b/src/indicators/money_flow_index.rs index eb61278..1aafb2a 100644 --- a/src/indicators/money_flow_index.rs +++ b/src/indicators/money_flow_index.rs @@ -1,7 +1,7 @@ use std::collections::VecDeque; use std::fmt; -use crate::errors::{Error, ErrorKind, Result}; +use crate::errors::{Result, TaError}; use crate::{Close, High, Low, Next, Period, Reset, Volume}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -68,7 +68,7 @@ pub struct MoneyFlowIndex { impl MoneyFlowIndex { pub fn new(period: usize) -> Result { match period { - 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)), + 0 => Err(TaError::InvalidParameter), _ => Ok(Self { period, money_flows: VecDeque::with_capacity(period + 1), diff --git a/src/indicators/rate_of_change.rs b/src/indicators/rate_of_change.rs index b20c261..969076a 100644 --- a/src/indicators/rate_of_change.rs +++ b/src/indicators/rate_of_change.rs @@ -1,6 +1,6 @@ use std::fmt; -use crate::errors::{Error, ErrorKind, Result}; +use crate::errors::{Result, TaError}; use crate::traits::{Close, Next, Period, Reset}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -51,7 +51,7 @@ pub struct RateOfChange { impl RateOfChange { pub fn new(period: usize) -> Result { match period { - 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)), + 0 => Err(TaError::InvalidParameter), _ => Ok(Self { period, index: 0, diff --git a/src/indicators/simple_moving_average.rs b/src/indicators/simple_moving_average.rs index 4bb2f06..9edcb50 100644 --- a/src/indicators/simple_moving_average.rs +++ b/src/indicators/simple_moving_average.rs @@ -1,6 +1,6 @@ use std::fmt; -use crate::errors::{Error, ErrorKind, Result}; +use crate::errors::{Result, TaError}; use crate::{Close, Next, Period, Reset}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -52,7 +52,7 @@ pub struct SimpleMovingAverage { impl SimpleMovingAverage { pub fn new(period: usize) -> Result { match period { - 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)), + 0 => Err(TaError::InvalidParameter), _ => Ok(Self { period, index: 0, diff --git a/src/indicators/standard_deviation.rs b/src/indicators/standard_deviation.rs index e9b77ca..115e35a 100644 --- a/src/indicators/standard_deviation.rs +++ b/src/indicators/standard_deviation.rs @@ -1,6 +1,6 @@ use std::fmt; -use crate::errors::{Error, ErrorKind, Result}; +use crate::errors::{Result, TaError}; use crate::{Close, Next, Period, Reset}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -53,7 +53,7 @@ pub struct StandardDeviation { impl StandardDeviation { pub fn new(period: usize) -> Result { match period { - 0 => Err(Error::from_kind(ErrorKind::InvalidParameter)), + 0 => Err(TaError::InvalidParameter), _ => Ok(Self { period, index: 0, diff --git a/src/lib.rs b/src/lib.rs index ffb7470..7c8b295 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,9 +49,6 @@ //! * [Rate of Change (ROC)](crate::indicators::RateOfChange) //! * [On Balance Volume (OBV)](crate::indicators::OnBalanceVolume) //! -#[macro_use] -extern crate error_chain; - #[cfg(test)] #[macro_use] mod test_helper;