ecbt_exchange/model/
interval.rs

1use std::convert::TryFrom;
2
3use serde::Deserialize;
4use serde::Serialize;
5use time::Duration;
6
7use crate::{EcbtError, Result};
8
9/// This enum represents a time interval
10#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
11pub enum Interval {
12    #[serde(rename = "1m")]
13    OneMinute,
14    #[serde(rename = "3m")]
15    ThreeMinutes,
16    #[serde(rename = "5m")]
17    FiveMinutes,
18    #[serde(rename = "15m")]
19    FifteenMinutes,
20    #[serde(rename = "30m")]
21    ThirtyMinutes,
22    #[serde(rename = "1h")]
23    OneHour,
24    #[serde(rename = "2h")]
25    TwoHours,
26    #[serde(rename = "4h")]
27    FourHours,
28    #[serde(rename = "6h")]
29    SixHours,
30    #[serde(rename = "8h")]
31    EightHours,
32    #[serde(rename = "12h")]
33    TwelveHours,
34    #[serde(rename = "1d")]
35    OneDay,
36    #[serde(rename = "3d")]
37    ThreeDays,
38    #[serde(rename = "1w")]
39    OneWeek,
40    #[serde(rename = "1mo")]
41    OneMonth,
42}
43
44impl From<Interval> for Duration {
45    fn from(i: Interval) -> Self {
46        match i {
47            Interval::OneMinute => Duration::minutes(1),
48            Interval::ThreeMinutes => Duration::minutes(3),
49            Interval::FiveMinutes => Duration::minutes(5),
50            Interval::FifteenMinutes => Duration::minutes(15),
51            Interval::ThirtyMinutes => Duration::minutes(30),
52            Interval::OneHour => Duration::hours(1),
53            Interval::TwoHours => Duration::hours(2),
54            Interval::FourHours => Duration::hours(4),
55            Interval::SixHours => Duration::hours(6),
56            Interval::EightHours => Duration::hours(8),
57            Interval::TwelveHours => Duration::hours(12),
58            Interval::OneDay => Duration::days(1),
59            Interval::ThreeDays => Duration::days(3),
60            Interval::OneWeek => Duration::weeks(1),
61            Interval::OneMonth => Duration::days(30),
62        }
63    }
64}
65
66impl Interval {
67    pub fn to_duration(self) -> Duration {
68        self.into()
69    }
70}
71
72impl TryFrom<Interval> for u32 {
73    type Error = EcbtError;
74    fn try_from(value: Interval) -> Result<Self> {
75        match value {
76            Interval::OneMinute => Ok(60),
77            Interval::FiveMinutes => Ok(300),
78            Interval::FifteenMinutes => Ok(900),
79            Interval::OneHour => Ok(3600),
80            Interval::SixHours => Ok(21600),
81            Interval::OneDay => Ok(86400),
82            _ => Err(EcbtError::MissingParameter(format!(
83                "{:?} is not supported in Coinbase",
84                value,
85            ))),
86        }
87    }
88}
89
90impl From<Interval> for &str {
91    fn from(interval: Interval) -> Self {
92        match interval {
93            Interval::OneMinute => "1m",
94            Interval::ThreeMinutes => "3m",
95            Interval::FiveMinutes => "5m",
96            Interval::FifteenMinutes => "15m",
97            Interval::ThirtyMinutes => "30m",
98            Interval::OneHour => "1h",
99            Interval::TwoHours => "2h",
100            Interval::FourHours => "4h",
101            Interval::SixHours => "6h",
102            Interval::EightHours => "8h",
103            Interval::TwelveHours => "12h",
104            Interval::OneDay => "1d",
105            Interval::ThreeDays => "3d",
106            Interval::OneWeek => "1w",
107            Interval::OneMonth => "1M",
108        }
109    }
110}