Skip to content

Commit

Permalink
Merge pull request greyblake#45 from tommady/issue-44-get-rid-of-erro…
Browse files Browse the repository at this point in the history
…r-chain-dependency

Issue 44 get rid of error chain dependency
  • Loading branch information
greyblake authored Feb 28, 2021
2 parents 1fb533c + 5cabd66 commit 1a083fd
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 27 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/data_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
34 changes: 29 additions & 5 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, TaError>;

#[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,
}
}
}
4 changes: 2 additions & 2 deletions src/indicators/efficiency_ratio.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct EfficiencyRatio {
impl EfficiencyRatio {
pub fn new(period: usize) -> Result<Self> {
match period {
0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
0 => Err(TaError::InvalidParameter),
_ => Ok(Self {
period,
index: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/indicators/exponential_moving_average.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -65,7 +65,7 @@ pub struct ExponentialMovingAverage {
impl ExponentialMovingAverage {
pub fn new(period: usize) -> Result<Self> {
match period {
0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
0 => Err(TaError::InvalidParameter),
_ => Ok(Self {
period,
k: 2.0 / (period + 1) as f64,
Expand Down
4 changes: 2 additions & 2 deletions src/indicators/maximum.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -37,7 +37,7 @@ pub struct Maximum {
impl Maximum {
pub fn new(period: usize) -> Result<Self> {
match period {
0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
0 => Err(TaError::InvalidParameter),
_ => Ok(Self {
period,
max_index: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/indicators/minimum.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -36,7 +36,7 @@ pub struct Minimum {
impl Minimum {
pub fn new(period: usize) -> Result<Self> {
match period {
0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
0 => Err(TaError::InvalidParameter),
_ => Ok(Self {
period,
min_index: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/indicators/money_flow_index.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct MoneyFlowIndex {
impl MoneyFlowIndex {
pub fn new(period: usize) -> Result<Self> {
match period {
0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
0 => Err(TaError::InvalidParameter),
_ => Ok(Self {
period,
money_flows: VecDeque::with_capacity(period + 1),
Expand Down
4 changes: 2 additions & 2 deletions src/indicators/rate_of_change.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -51,7 +51,7 @@ pub struct RateOfChange {
impl RateOfChange {
pub fn new(period: usize) -> Result<Self> {
match period {
0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
0 => Err(TaError::InvalidParameter),
_ => Ok(Self {
period,
index: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/indicators/simple_moving_average.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -52,7 +52,7 @@ pub struct SimpleMovingAverage {
impl SimpleMovingAverage {
pub fn new(period: usize) -> Result<Self> {
match period {
0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
0 => Err(TaError::InvalidParameter),
_ => Ok(Self {
period,
index: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/indicators/standard_deviation.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct StandardDeviation {
impl StandardDeviation {
pub fn new(period: usize) -> Result<Self> {
match period {
0 => Err(Error::from_kind(ErrorKind::InvalidParameter)),
0 => Err(TaError::InvalidParameter),
_ => Ok(Self {
period,
index: 0,
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1a083fd

Please sign in to comment.