use std::error::Error as err;
use std::fmt::{self, Debug};
pub enum Error {
CredentialError(String),
AuthError(String),
InvalidSymbol(String),
InvalidMarket(String),
InvalidTimezone(String),
InvalidTimeframe(String),
InvalidIndicator(String),
UnknownError(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::CredentialError(_) => write!(f, "Credential Error"),
Error::AuthError(_) => write!(f, "Authentication Error"),
Error::InvalidSymbol(_) => write!(f, "Invalid Symbol"),
Error::InvalidMarket(_) => write!(f, "Invalid Market"),
Error::InvalidTimezone(_) => write!(f, "Invalid Time zone"),
Error::InvalidTimeframe(_) => write!(f, "Invalid Time frame"),
Error::InvalidIndicator(_) => write!(f, "Invalid Indicator"),
Error::UnknownError(_) => write!(f, "Unknown error has occurred"),
}
}
}
impl Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CredentialError(arg0) => f.debug_tuple("CredentialError").field(arg0).finish(),
Self::AuthError(arg0) => f.debug_tuple("AuthError").field(arg0).finish(),
Self::InvalidSymbol(arg0) => f.debug_tuple("InvalidSymbol").field(arg0).finish(),
Self::InvalidMarket(arg0) => f.debug_tuple("InvalidMarket").field(arg0).finish(),
Self::InvalidTimezone(arg0) => f.debug_tuple("InvalidTimezone").field(arg0).finish(),
Self::InvalidTimeframe(arg0) => f.debug_tuple("InvalidTimeframe").field(arg0).finish(),
Self::InvalidIndicator(arg0) => f.debug_tuple("InvalidIndicator").field(arg0).finish(),
Self::UnknownError(arg0) => f.debug_tuple("UnknownError").field(arg0).finish(),
}
}
}
pub struct APIError {
pub code: Error,
pub message: String,
}
impl fmt::Display for APIError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let colour = Colour::Red; write!(f, "{}{}:\x1b[0m {}", colour, self.code, self.message)
}
}
impl Debug for APIError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("APIError")
.field("code", &self.code)
.field("message", &self.message)
.finish()
}
}
impl err for APIError {}
#[allow(dead_code)]
pub enum Colour {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
}
impl fmt::Display for Colour {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let code = match self {
Colour::Black => 30,
Colour::Red => 31,
Colour::Green => 32,
Colour::Yellow => 33,
Colour::Blue => 34,
Colour::Magenta => 35,
Colour::Cyan => 36,
Colour::White => 37,
};
write!(f, "\x1b[{}m", code)
}
}