stat/analysis/
mod.rs

1//! Analysis contains technical indicators that try to predict the direction of
2//! future values using the past data.
3use std::error::Error;
4use std::fmt;
5use std::result;
6
7pub mod momentum;
8pub mod trend;
9
10/// A specialised `Result` type for analysis operations.
11///
12/// This is used across `stat::analysis` for any operation
13/// which may cause an error.
14pub type Result<T> = result::Result<T, AnalysisError>;
15
16/// A list specifying types of analysis errors.
17#[derive(Debug)]
18pub enum AnalysisError {
19	/// Gain must be positive.
20	GainLessThanZero,
21	/// Loss must be positive.
22	LossLessThanZero,
23	/// Close must be less than or equal to high.
24	CloseGreaterThanHigh,
25	/// Close must be greater than or equal to low.
26	CloseLessThanLow,
27	/// High must be greater than or equal to low.
28	HighLessThanLow,
29	/// Slice must not be empty.
30	SliceIsEmpty,
31}
32
33impl fmt::Display for AnalysisError {
34	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35		write!(f, "error: {}", self.description())
36	}
37}
38
39impl Error for AnalysisError {
40	fn description(&self) -> &str {
41		match *self {
42			AnalysisError::GainLessThanZero => "gain < 0",
43			AnalysisError::LossLessThanZero => "loss < 0",
44			AnalysisError::CloseGreaterThanHigh => "close > high",
45			AnalysisError::CloseLessThanLow => "close < low",
46			AnalysisError::HighLessThanLow => "high < low",
47			AnalysisError::SliceIsEmpty => "slice is empty",
48		}
49	}
50
51	fn cause(&self) -> Option<&Error> {
52		None
53	}
54}