trade_vision 0.1.1

Unofficial API for TradingView
Documentation
use std::error::Error as err;
use std::fmt::{self, Debug};

/// Errors that can be generated by trade_vision.
pub enum Error {
    /// The API credentials are invalid or missing.
    CredentialError(String),
    /// Another form of authentication is invalid or missing.
    AuthError(String),

    /// The symbol is invalid or not supported by the API.
    InvalidSymbol(String),
    /// The market is invalid or not supported by the API.
    InvalidMarket(String),
    /// The timezone is invalid or not supported by the API.
    InvalidTimezone(String),
    /// The timeframe is invalid or not supported by the API.
    InvalidTimeframe(String),
    /// The indicator is invalid or not supported by the API.
    InvalidIndicator(String),

    /// An Unknown error has occurred, consult message for further clarification.
    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; // choose a Colour for the error message
        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)
    }
}