openlimits_exchange/model/
interval.rs

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